+ 1

Can someone explain what's happening here?

So this is the final question of the chapter "Working with Strings": Fill in the blanks to assign the last character of the string to the x variable: string s = "Hello"; char x; x = _ [s.Length-1 _; I get that you have to close the brackets and I guessed that the other space was "s" . But i literally guessed it. That last line of code is messing me up. Can someone explain to me why this code ( x = s [ s.Length-1]; ) is correct? Thanks and sorry for the mediocre english.

1st Aug 2018, 11:53 PM
Ninja
Ninja - avatar
2 Réponses
+ 2
x =s[s.Length - 1]; This is the easy answer
8th Sep 2021, 5:51 AM
Jobayrul Hasan
Jobayrul Hasan - avatar
+ 1
Thank you! I was at a loss and now i feel really dumb. That was a really nice explanation.
3rd Aug 2018, 12:13 AM
Ninja
Ninja - avatar