"SamIAm" <sa****@rubbachicken.com> wrote in message
news:Od**************@TK2MSFTNGP10.phx.gbl...
How do I extract the last 3 characters of a string i.e.
string s = "000001" I want "001"
Probably overkill for your current problem, but you can use an Regular
Expression [RE] to select the part of the string that you want, in this
case, it would be @".{3}$". You would be amazed at what can be accomplished
with RE's ! Below is some sample code, though I'd recommend you consult the
relevant documentation and a tutorial or two :) !
I hope this helps.
Anthony Borla
using System;
using System.Text.RegularExpressions;
public class RegexExample
{
public static void Main()
{
String str = "000001";
Regex re = new Regex(@".{3}$", RegexOptions.None);
MatchCollection mlist = re.Matches(str);
if (mlist.Count > 0)
Console.WriteLine("str = {0}, match = {1}", str, mlist[0].Value);
else
Console.WriteLine("No match!");
String substr = mlist[0].Value.ToString();
}
}