❔ I'm very new, still learning, I don't know understand what's wrong with this
This is what I'm trying to do:
"This is an example!" ==> "sihT si na !elpmaxe"
27 Replies
string str = "This is an example!";
// using the method
String[] strlist = str.Split();
string r = "";
foreach (String s in strlist)
{
char[] charArray = s.ToCharArray();
string m = (Array.Reverse(charArray));
Console.WriteLine(s);
Console.WriteLine(new string(charArray));
//Console.WriteLine(s);
}
//Console.WriteLine(strlist);
Array.Reverse()
reverses the array in-place
It doesn't return anythingAngius
REPL Result: Success
Result: int[]
Compile: 487.132ms | Execution: 55.592ms | React with ❌ to remove this embed.
void
means that a method doesnt return a value.
Array.Reverse()
takes as parameter an array, and modifies it, but does not return anything
so string m = (Array.Reverse(charArray));
would be string m = void;
, and because these are different types, it complainsThis code doesn't compile does it?
Split without any parameters is invalid
to store the reverse char array as string, u need to create a string from it after reversing it.
later u already create a string from the already reversed array:
Console.WriteLine(new string(charArray));
so to store it in the variable m
u have to assign it: string m = new string(charArray);
cap5lut
REPL Result: Success
Result: string[]
Compile: 502.128ms | Execution: 29.162ms | React with ❌ to remove this embed.
It doens't
oh
ohhhh
it isnt exactly invalid because it goes into the
string.Split(params char[]?)
method,
but its probably not the expected behaviour, because the whole string will just be the first and sole array elementThere is no overload for split that takes no parameters
String.Split Method (System)
Returns a string array that contains the substrings in this instance that are delimited by elements of a specified string or Unicode character array.
yeah it goes into the varargs method with an empty array
I don't understand what you mean by the last line
"so to store it in the variable m u have to assign it: string m = new string(charArray);"
what does new string do?
by "it" im revering to the string u create with
new string(charArray)
Ok its not obvious from the listed method signatures. On my phone 🙂
To turn a
char[]
into a string, you need to pass it as a parameter to the new string()
constructorIn my mind calling Split with no parameters is nonsensical
I don't think I properly understand what "new" does
Creates a new instance of... something
What kind of parameters should I put?
new SomeType(...)
creates always a new object of the given type, in ur case as it is new string(...)
, it creates a new string
to use that new string u have to store it in a variable/assign it to a variable
so the whole line means "create a new string from the given charArray and store it in the string variable m"The delimiter on which you want to split
So it only exists for that line?
It doesn't fully change it right?
?
The instance exists beyond that line
And no, it doesn't change anything
So like, pass in a comma character to split the string into multiple tokens where they are separated by commas
OH
Okay, yeah
Makes sense
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.