Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to get a specific value from a string

Posted on 2007-11-28
4
Medium Priority
?
234 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 2000 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 64

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

715 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