How do I get the rightmost part of a string, as with the VB Right function?

How do I get the rightmost part of a string, as with the VB Right function?

  • Comments 10

Use String.Substring. Assuming that x is a string of length at least n, to get the last n characters, you would use x.Substring(x.Length-n).

Note that the above assumes that the string is at least n characters long. For a more robust version, you might use something like: x.Length < n ? x.Substring(x.Length-n) : x.

[Author: Jon Skeet]

Leave a Comment
  • Please add 2 and 7 and type the answer here:
  • Post
Page 1 of 1 (10 items)