Solved

How to get a specific value from a string

Posted on 2007-11-28
4
227 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

930 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

15 Experts available now in Live!

Get 1:1 Help Now