views:

703

answers:

8

How can I extract a substring which is composed of the rightmost six letters from another string?

Ex: my string is "PER 343573". Now I want to extract only "343573".

How can I do this?

+4  A: 
string SubString = MyString.Substring(MyString.Length-6);
Vilx-
This aproach does not work correctly if the string is not as long as the number of characters required.
Stevo3000
also the question is "right most n letters" yours will only ever work if you need to extract the 6. A bit pernickity, but a fact all the same :)
James
Depending on what exactly the OP wants one might also throw regular expressions at this. If s?he only wants the number at the end of the string, that's definitely the most painless solution, especially when the number of digits may vary in some later software upgrade.
Joey
@Johannes Rössel - I'm a big fan of regular expressions (see my answers), but I'd never recomend them for a simple situation such as this. Any of the answers that use a function wrapper are better suited to code maintanance than a regular expression. An extension method (or standard function if .NET 2.0) is the best solution.
Stevo3000
+3  A: 

Use this:

String text = "PER 343573";
String numbers = text;
if (text.Length > 6)
{
    numbers = text.Substring(text.Length - 6);
}
ck
What if `text` is null?
Stevo3000
+2  A: 

Probably nicer to use an extension method:

public static class StringExtensions
{
    public static string Right(this string str, int length)
    {
        return str.SubString(str.Length - length, length);
    }
}

Usage

string myStr = "PER 343573";
string subStr = myStr.Right(6);
James
Oooh! May I suggest creating an IRighteous interface as well? :D
Vilx-
Left() and Right() are so common that having extentions like this in your personal library is more than fair.
Aramis wyler
What if str is null?
Stevo3000
Then it would throw a NullReferenceException just like it would if you tried to use any method on a null string...
James
+5  A: 

MSDN

String mystr = "PER 343573";
String number = mystr.Substring(mystr.Length-6);

EDIT: too slow...

PoweRoy
+1 for the link... ;)
Philip Wallace
+4  A: 

Write an extansion method to express the Right(n); function. The function should deal with null or empty strings returning an empty string, strings shorter than the max length returning the original string and strings longer than the max length returning the max length of rightmost characters.

public static string Right(this string sValue, int iMaxLength)
{
  //Check if the value is valid
  if (string.IsNullOrEmpty(sValue))
  {
    //Set valid empty string as string could be null
    sValue = string.Empty;
  }
  else if (sValue.Length > iMaxLength)
  {
    //Make the string no longer than the max length
    sValue = sValue.Substring(sValue.Length - iMaxLength, iMaxLength);
  }

  //Return the string
  return sValue;
}
Stevo3000
Start index cannot be less than 0 either.
James
@James - It won't be as `sValue.Length > iMaxLength` before a substring is called!
Stevo3000
A: 

This isn't exactly what you are asking for, but just looking at the example, it appears that you are looking for the numeric section of the string.

If this is always the case, then a good way to do it would be using a regular expression.

var regex= new Regex("\n+");
string numberString = regex.Match(page).Value;
chills42
+1 for a different aproach.
Stevo3000
-1 regular expressions are a little overkill for something like this, especially when there are already built in methods for doing it.
James
I'm not arguing for using this method if you really do only need the last 6, but if your goal is to extract a number (such as an id) that may change to 5 or 7 digits at some point in the future, this is a better way.
chills42
A: 

Guessing at your requirements but the following regular expression will yield only on 6 alphanumerics before the end of the string and no match otherwise.

string result = Regex.Match("PER 343573", @"[a-zA-Z\d]{6}$").Value;
Wade
Does this solution not reasonably meet the vague requirements? If not, please explain your down voting.
Wade
A: 

Without resorting to the bit converter and bit shifting (need to be sure of encoding) this is fastest method I use as an extension method 'Right'.

string myString = "123456789123456789";

if (myString > 6) {

    char[] cString = myString.ToCharArray();
    Array.Reverse(myString);
    Array.Resize(ref myString, 6);
    Array.Reverse(myString);
    string val = new string(myString);

}

Terry Carvin