Solved

How to get a specific value from a string

Posted on 2007-11-28
4
231 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
powershell try catch  and $ErrorActionPreference = continue 6 71
SSIS Column mapping 5 42
Read top line from CSV file 14 60
Make borderless form movable by user 2 27
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

751 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