Solved

How to get a specific value from a string

Posted on 2007-11-28
4
226 Views
Last Modified: 2013-12-16
I have a string value of

string test = "99999\tAAAAAA\tOH\tX1 \tCentral\t754"
"99999\BBBB\tOH\tX1 \tCentral\t1234"
"99999\tfasdfjhsjf\tWV\tX1 \tCentral\t12345"

for int code , I want the value after the last "\t" from the above string values. How do I do that?
0
Comment
Question by:pratikshahse
4 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 total points
ID: 20369870
int i = test.LastIndexOf("\t");
string value = test.Substring(i+1);
0
 
LVL 22

Expert Comment

by:JimBrandley
ID: 20369884
Have you tried this:
int index = test.LastIndexOf("\t");
int code = 0;
if (index > -1)
   code = int.Parse(test.Substring(index + 1));

Jim
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 20369902
Hi pratikshahse;

This will do it.

    // Test Data
    string test = @"99999\tAAAAAA\tOH\tX1 \tCentral\t75499999\BBBB\tOH\tX1 \tCentral\t123499999\tfasdfjhsjf\tWV\tX1 \tCentral\t12345";

    // The needed data
    string data = test.Substring(test.LastIndexOf("\\") + 1);

Fernando
0
 
LVL 29

Expert Comment

by:Göran Andersson
ID: 20369904
This will pick out the digits between the last \t and the end of the string, and convert it to an integer:

Match match = Regex.Match(test, @"\t(\d+)$");

string digits = match.Groups[0].Captures[0].Value;

int value = int.Parse(digits);

Open in new window

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now