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

Published 12 March 04 02:28 AM

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]

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# khurram@sharpcoders.net said on March 12, 2004 5:44 AM:
I think, one need to check the n, it shouldn't be more than given string's length. If it is, then one might return the whole string. (I guess this is what VB6 Right() does?)
# Jon Skeet said on March 13, 2004 12:17 AM:
Not sure about VB.NET, but yes, you would need to check that. I'll make that clear in the article.
# Bulent Mericli said on April 14, 2004 7:28 AM:
The more robust version needs a correction. It should be "x.Length > n ? x.Substring(x.Length-n) : x"

instead of "x.Length < n ? x.Substring(x.Length-n) : x"
# dmbb said on May 17, 2004 10:30 PM:
think, one need to check the n, it shouldn't be more than given string's length
http://www.iwo100.com
# jlanng said on May 25, 2004 8:55 AM:
Worthy of a mention is string's useful EndsWith(string) function
# Thong Nguyen said on July 6, 2004 4:33 AM:
Why is Left and Right not included in the string class?

What I'd like to see is a Left(Predicate<char>) and Right(Predicate<char>) function.
# dawn said on July 8, 2004 2:11 AM:
I set up indexes to read the string from the body of an outlook email msg. However, I sometimes run into a series (a range of model no) and wanted to select Left of the hyphen for one string value and Right of the hyphen for the second string value and then enumerate to get the whole model no series.

How do I write that part in C#?
# ok said on July 18, 2004 12:27 AM:
my web:
http://www.sj55.com/pic_sort http://www.zw88.com/paopaotang.htm http://www.zw88.com/sj.htm http://www.zw88.com/sm/
http://www.zw88.com/sms/ http://www.zw88.com/zw.htm http://www.resou.com/8888.htm http://www.sj55.com/pic/pic_1130.htm
# 成人论坛 said on August 5, 2004 4:34 AM:
http://www1.12322.com
http://www.caobb.com
# RebelGeekz said on December 28, 2004 4:54 AM:
[http://itpeixun.51.net/][http://aissl.51.net/][http://kukuxz003.freewebpage.org/][http://kukuxz001.51.net/][http://kukuxz003.51.net/][http://kukuxz005.51.net/][http://kukuxz002.51.net/][http://kukuxz004.freewebpage.org/][http://kukuxz007.51.net/][http://kukuxz001.freewebpage.org/][http://kukuxz006.51.net/][http://kukuxz002.freewebpage.org/][http://kukuxz004.51.net/][http://kukuxz008.51.net/][http://kukuxz009.51.net/][http://kukuxz005.freewebpage.org/][http://kukuxz006.freewebpage.org/][http://kukuxz007.freewebpage.org/][http://kukuxz009.freewebpage.org/]

Leave a Comment

(required) 
(optional)
(required) 

  
Enter Code Here: Required

This Blog

Syndication

Page view tracker