Referencing row indices
Each character in a string has a number (called
index), and indexing in the C# programming language starts from zero. That is, the first character has index 0, the second - 1, and so on.
String characters can be accessed by indexes, which are indicated in square brackets
s[i]
.
Example
String S |
H |
e |
l |
l |
o |
Index |
S[0] |
S[1] |
S[2] |
S[3] |
S[4] |
Important!
When accessing a string element at index
i
, the type
s[i]
is not
string
but
char
. This means that this is not a string, but a character that corresponds to some integer in the character code table. In particular, when you add two elements of type
char
, you get an integer - the addition of the codes of these elements. However, if you display
s[i]
, the
i
-th character of the string will be displayed, not the number corresponding to it.