Solved

How to get a specific value from a string

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

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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project

830 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