Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract the last word from a string using C#

Tags:

string

c#

.net

My string is like this:

string input = "STRIP, HR 3/16 X 1 1/2 X 1 5/8 + API"; 

Here actually I want to extract the last word, 'API', and return.

What would be the C# code to do the above extraction?

like image 488
Pradeep Avatar asked Jan 05 '11 12:01

Pradeep


People also ask

How do you find the last word in a string?

To get the last word of a string:Call the split() method on the string, passing it a string containing an empty space as a parameter. The split method will return an array containing the words in the string. Call the pop() method to get the value of the last element (word) in the array.

What is the last element of string in c?

Strings are actually one-dimensional array of characters terminated by a null character '\0'. Thus a null-terminated string contains the characters that comprise the string followed by a null. The following declaration and initialization create a string consisting of the word "Hello".

How do you find the last word in an array?

Once you have the array, you are retrieving the last element by taking the value at the last array index (found by taking array length and subtracting 1, since array indices begin at 0).

How does c know the end of a string?

Strings in C are represented by arrays of characters. The end of the string is marked with a special character, the null character , which is simply the character with the value 0.


2 Answers

First:

using System.Linq; // System.Core.dll 

then

string last = input.Split(' ').LastOrDefault();  // or  string last = input.Trim().Split(' ').LastOrDefault();  // or  string last = input.Trim().Split(' ').LastOrDefault().Trim(); 
like image 32
abatishchev Avatar answered Oct 02 '22 22:10

abatishchev


Well, the naive implementation to that would be to simply split on each space and take the last element.

Splitting is done using an instance method on the String object, and the last of the elements can either be retrieved using array indexing, or using the Last LINQ operator.

End result:

string lastWord = input.Split(' ').Last(); 

If you don't have LINQ, I would do it in two operations:

string[] parts = input.Split(' '); string lastWord = parts[parts.Length - 1]; 

While this would work for this string, it might not work for a slightly different string, so either you'll have to figure out how to change the code accordingly, or post all the rules.

string input = ".... ,API"; 

Here, the comma would be part of the "word".

Also, if the first method of obtaining the word is correct, that is, everything after the last space, and your string adheres to the following rules:

  • Will always contain at least one space
  • Does not end with one or more spaces (in case of this you can trim it)

Then you can use this code that will allocate fewer objects on the heap for GC to worry about later:

string lastWord = input.Substring(input.LastIndexOf(' ') + 1); 

However, if you need to consider commas, semicolons, and whatnot, the first method using splitting is the best; there are fewer things to keep track of.

like image 99
Lasse V. Karlsen Avatar answered Oct 02 '22 21:10

Lasse V. Karlsen