❔ 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"
No description
27 Replies
palaashdwivedi
palaashdwivedi10mo ago
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);
Angius
Angius10mo ago
Array.Reverse() reverses the array in-place It doesn't return anything
MODiX
MODiX10mo ago
Angius
REPL Result: Success
var arr = new[]{ 1, 2, 3, 4, 5 };
Array.Reverse(arr);
arr
var arr = new[]{ 1, 2, 3, 4, 5 };
Array.Reverse(arr);
arr
Result: int[]
[
5,
4,
3,
2,
1
]
[
5,
4,
3,
2,
1
]
Compile: 487.132ms | Execution: 55.592ms | React with ❌ to remove this embed.
cap5lut
cap5lut10mo ago
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 complains
mtreit
mtreit10mo ago
This code doesn't compile does it? Split without any parameters is invalid
cap5lut
cap5lut10mo ago
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);
MODiX
MODiX10mo ago
cap5lut
REPL Result: Success
"hello".Split()
"hello".Split()
Result: string[]
[
"hello"
]
[
"hello"
]
Compile: 502.128ms | Execution: 29.162ms | React with ❌ to remove this embed.
palaashdwivedi
palaashdwivedi10mo ago
It doens't oh ohhhh
cap5lut
cap5lut10mo ago
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 element
mtreit
mtreit10mo ago
There is no overload for split that takes no parameters
mtreit
mtreit10mo ago
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.
cap5lut
cap5lut10mo ago
yeah it goes into the varargs method with an empty array
palaashdwivedi
palaashdwivedi10mo ago
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?
cap5lut
cap5lut10mo ago
by "it" im revering to the string u create with new string(charArray)
mtreit
mtreit10mo ago
Ok its not obvious from the listed method signatures. On my phone 🙂
Angius
Angius10mo ago
To turn a char[] into a string, you need to pass it as a parameter to the new string() constructor
mtreit
mtreit10mo ago
In my mind calling Split with no parameters is nonsensical
palaashdwivedi
palaashdwivedi10mo ago
I don't think I properly understand what "new" does
Angius
Angius10mo ago
Creates a new instance of... something
palaashdwivedi
palaashdwivedi10mo ago
What kind of parameters should I put?
cap5lut
cap5lut10mo ago
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
string m = new string(charArray);
^ ^ ^--- creates a new string
| +---- assignment operator, store whats on the right side in the left side
+---- declares a new variable with the name m and the type string
string m = new string(charArray);
^ ^ ^--- creates a new string
| +---- assignment operator, store whats on the right side in the left side
+---- declares a new variable with the name m and the type string
so the whole line means "create a new string from the given charArray and store it in the string variable m"
mtreit
mtreit10mo ago
The delimiter on which you want to split
palaashdwivedi
palaashdwivedi10mo ago
So it only exists for that line? It doesn't fully change it right?
Angius
Angius10mo ago
? The instance exists beyond that line And no, it doesn't change anything
mtreit
mtreit10mo ago
So like, pass in a comma character to split the string into multiple tokens where they are separated by commas
palaashdwivedi
palaashdwivedi10mo ago
OH Okay, yeah Makes sense
Accord
Accord10mo ago
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.