I want to find the last character of a string and then put in an if
stating that if the last character is equal to "A", "B" or "C" then to do a certain action. How do I get the last character?
Use the endswith
method of strings:
if (string.EndsWith("A") || string.EndsWith("B")) { //do stuff here }
Heres the MSDN article explaining this method:
http://msdn.microsoft.com/en-us/library/system.string.endswith(v=vs.71).aspx
I assume you don't actually want the last character position (which would be yourString.Length - 1
), but the last character itself. You can find that by indexing the string with the last character position:
yourString[yourString.Length - 1]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With