473,785 Members | 2,777 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 31497
Hi SamIAm,

Something like this:

string last3 = s.Substring(s.L ength-3);

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

"SamIAm" <sa****@rubbach icken.com> wrote in message
news:Od******** ******@TK2MSFTN GP10.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****@rubbach icken.com> wrote in message
news:Od******** ******@TK2MSFTN GP10.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****@rubbach icken.com> wrote in message
news:Od******** ******@TK2MSFTN GP10.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.Reg ularExpressions ;

public class RegexExample
{
public static void Main()
{
String str = "000001";

Regex re = new Regex(@".{3}$", RegexOptions.No ne);

MatchCollection mlist = re.Matches(str) ;

if (mlist.Count > 0)
Console.WriteLi ne("str = {0}, match = {1}", str, mlist[0].Value);
else
Console.WriteLi ne("No match!");

String substr = mlist[0].Value.ToString ();
}
}
Nov 15 '05 #5
Thanks for all the replies
"SamIAm" <sa****@rubbach icken.com> wrote in message news:Od******** ******@TK2MSFTN GP10.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****@rubbachi cken.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****@rubbach icken.com> wrote in message
news:Od******** ******@TK2MSFTN GP10.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.L enght-3);

"Jeff Green" <je****@tpg.com .au> schrieb im Newsbeitrag
news:MP******** *************** *@news.tpg.com. au...
In article <OZ************ **@TK2MSFTNGP09 .phx.gbl>,
sa****@rubbachi cken.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****@rubbach icken.com> wrote in message
news:Od******** ******@TK2MSFTN GP10.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.co m>
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
1122
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 contains exactly 8 characters, but not in debug mode. does anyone know if it exists a patch for VS .Net IDE
3
3528
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 1820 1823 1825
6
1843
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
18381
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 preceded by "http://" or "http : //www" (without the spaces). in pseudo code, I thought it might look like this: if (eregi("http: //", $mystring)) {
5
17955
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 href... /a> tags as a guideline. I have the code.... String data1; DataInputStream webadd = null;
15
6003
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 the Regex.Split function to split the string whenever the #Key(val) occurrs but that keeps the #Key(val) occurrences to that I can reconstruct the final string after doing certain operations on each token (I need to basically convert each string...
2
1568
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 a table total, in this table, there is a columns call available. i want to access the last updated value of particular item date item quantity balance 2007-11-12 book 4 5 2007-11-12 pen 6 ...
1
1623
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 best approach in using regex for this? I can see match and replace the dates, extract section with regex, and then for each section extract again with the right regex the tables....
2
1638
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 is ÄlprÉd. Need help...
3
1416
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 Jason Doe John Jason Lee Doe I've already stripped out all of the suffixes (Jr., Sr, CFA, MD, etc) and am using the following formula. The problem is, for some reason some of the results are blank. I copied this formula from another sheet, but am...
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10151
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10092
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8973
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.