[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

C# String Manipulation: get last value in comma separated string

Posted on 2012-08-23
5
Medium Priority
?
1,273 Views
Last Modified: 2012-08-23
Let's say we have this string:

string s = "john,mary,jane";

How do we get the last value in this comma-separated list? (i.e. "jane")
0
Comment
Question by:adskarcox
  • 3
  • 2
5 Comments
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 2000 total points
ID: 38326534
Lots of ways to do it...here's one via Split():
            string s = "john,mary,jane";
            string[] values = s.Split(',');
            string lastValue = values[values.GetUpperBound(0)];
            Console.WriteLine("lastValue = " + lastValue);

Open in new window

0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 38326541
Another using LastIndexOf():
            string s = "john,mary,jane";
            int index = s.LastIndexOf(",");
            if (index != -1)
            {
                string lastValue = s.Substring(index + 1);
                Console.WriteLine("lastValue = " + lastValue);
            }

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38326550
I'm rather fond of the substring approach to negate an unnecessary split  ; )

string jane = s.Substring(s.LastIndexOf(',') + 1);

Open in new window


...with the assumption that there will be a comma in the string.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38326552
Haha. Too fast for me Idle  : )
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 38326563
High-ya!...fingers of fury.  My keyboard hates me...  ;)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month18 days, 11 hours left to enroll

834 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