473,324 Members | 2,239 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

How do I: Extract the last 3 characters of a string

How do I extract the last 3 characters of a string i.e.

string s = "000001" I want "001"

Thanks,

S

Nov 15 '05 #1
8 31469
Hi SamIAm,

Something like this:

string last3 = s.Substring(s.Length-3);

Joe
--
http://www.csharp-station.com

"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"
Thanks,
S
Nov 15 '05 #2
SamIAm, you can use s.SubString() to extract any portion of a string.
You'll also probably want to use s.Length - 3 for your start position.

--
Greg Ewing [MVP]
http://www.citidc.com
"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"

Thanks,

S


Nov 15 '05 #3
s=s.Substring(3,3);
-----Original Message-----
How do I extract the last 3 characters of a string i.e.

string s = "000001" I want "001"

Thanks,

S


Nov 15 '05 #4

"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();
}
}
Nov 15 '05 #5
Thanks for all the replies
"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"

Thanks,

S

Nov 15 '05 #6
In article <OZ**************@TK2MSFTNGP09.phx.gbl>,
sa****@rubbachicken.com says...

string temp = "";
string s = "000001";

for (int i = s.Length - 3; i < s.Length ; i++)
{
temp += s[i]; // the three characters you're looking for
// are now in the string 'temp';
}

"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"


Hope that helps...Jeff

- --
Jeff Green
Greentrees - All pigs fed and ready to fly
je****@tpg.com.au
Nov 15 '05 #7
string sLastThreeChar=s.Substring(s.Lenght-3);

"Jeff Green" <je****@tpg.com.au> schrieb im Newsbeitrag
news:MP************************@news.tpg.com.au...
In article <OZ**************@TK2MSFTNGP09.phx.gbl>,
sa****@rubbachicken.com says...

string temp = "";
string s = "000001";

for (int i = s.Length - 3; i < s.Length ; i++)
{
temp += s[i]; // the three characters you're looking for
// are now in the string 'temp';
}

"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"


Hope that helps...Jeff

- --
Jeff Green
Greentrees - All pigs fed and ready to fly
je****@tpg.com.au

Nov 15 '05 #8
Jeff Green <je****@tpg.com.au> wrote:
string temp = "";
string s = "000001";

for (int i = s.Length - 3; i < s.Length ; i++)
{
temp += s[i]; // the three characters you're looking for
// are now in the string 'temp';
}


There's no advantage in doing that over using substring. It's
inefficient (admittedly in a way which probably isn't going to matter
in the slightest) but above all it's harder to understand (IMO) than:

string foo = s.Substring (s.Length-3);

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Fuochi | last post by:
hello, I have Visual Studio .Net 2002 C++. I have migrated a code created under VS 6.0 that contains a call to the method CreateField. This method fail in release mode if the string parameter...
3
by: zek2005 | last post by:
Hi friends! I have a varchar field in my DB with numeric values separates by spaces. I need to extract the numbers to create an array. Example 1: 1820 1823 1825 --> need to be transform into ...
6
by: giloosh | last post by:
Hello, how can i extract a websites html into a string. also, is there a limit to how many chars a string can hold? something like: $string = extracted_html($url); thanks for any help!
3
by: deko | last post by:
I'm sure someone has passed this way before... I want to check to see is a domain name is contained in a string, and if one is, I want to extract it. In these strings, domains are always...
5
markmcgookin
by: markmcgookin | last post by:
Hi Folks, I am writing a program to analyse an html page in java, I am connecting to a website, then going to extract ALL the links from it. I think the best way to do this is using the <a...
15
by: nagar | last post by:
I need to split a string whenever a separator string is present (lets sey #Key(val) where val is a variable) and rejoin it in the proper order after doing some processing. Is there a way to use...
2
by: robin1983 | last post by:
HI guys, i have a table which has many fiield, actually my problem is that, i want to retrieved the last updated value of a particular column. as for example. what will be condition. suppose, i have...
1
by: GS | last post by:
I need to extract sections out of a long string of about 5 to 10 KB, change any date format of dd Mmm yyyy to yyyy-mm-dd, then further from each section extract columns of tables. what is the...
2
by: rajesh0303 | last post by:
I want to extract substring from string and replace them by character. .ex:In "lphrd" , I want to extract and ,and want them to replace be replaced by Ä and É. so, that the result string...
3
by: JP Romano | last post by:
Hi... I need some help with either a formula or vba routine that will extract last names for a comparison. The names can come into my spreadsheet as any of the following John Doe John J Doe Jonh...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.