How to extract a specific character in a string in c # by index

In C ++, strings are like an array, when you write str [i] you can acsess i + 1 element of the array, there is something like this in C # I don't need the indexOf method, because it's different. I need something to cast characters to a string by their index

+4
source share
2 answers

Yes, you can refer to string characters using the same syntax as C ++, for example:

string myString = "dummy"; char x = myString[3]; 

Note: x will be assigned to m .

You can also iterate with a for loop, for example:

 char y; for (int i = 0; i < myString.Length; i ++) { y = myString[i]; } 

Finally, you can use the foreach to get the value already added to the char , for example:

 foreach(char z in myString) { // z is already a char so you can just use it here, no need to cast } 
+3
source

This is the same in C #: s[n] gets the character number n string s .

+8
source

Source: https://habr.com/ru/post/1497541/


All Articles