Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to split string by last occurrence of character?

Tags:

string

c#

split

Let's say I need to split string like this:

Input string: "My. name. is Bond._James Bond!" Output 2 strings:

  1. "My. name. is Bond"
  2. "_James Bond!"

I tried this:

int lastDotIndex = inputString.LastIndexOf(".", System.StringComparison.Ordinal); string firstPart = inputString.Remove(lastDotIndex); string secondPart= inputString.Substring(lastDotIndex + 1, inputString.Length - firstPart.Length - 1); 

Can someone propose more elegant way?

like image 769
user2706838 Avatar asked Feb 12 '14 16:02

user2706838


People also ask

How do you split the last character of a string?

To split a string on the last occurrence of a substring:, use the lastIndexOf() method to get the last index of the substring and call the slice() method on the string to get the portions before and after the substring you want to split on.

How do you find the last occurrence of a character in a string is?

strrchr() — Locate Last Occurrence of Character in String The strrchr() function finds the last occurrence of c (converted to a character) in string . The ending null character is considered part of the string . The strrchr() function returns a pointer to the last occurrence of c in string .

How do you split a string at the last occurrence of a character with Python?

Python provides a method that split the string from rear end of the string. The inbuilt Python function rsplit() that split the string on the last occurrence of the delimiter. In rsplit() function 1 is passed with the argument so it breaks the string only taking one delimiter from last.

How do you split a string on the first occurrence of certain characters?

To split a JavaScript string only on the first occurrence of a character, call the slice() method on the string, passing it the index of the character + 1 as a parameter. The slice method will return the portion of the string after the first occurrence of the character.


2 Answers

Updated Answer (for C# 8 and above)

C# 8 introduced a new feature called ranges and indices, which offer a more concise syntax for working with strings.

string s = "My. name. is Bond._James Bond!"; int idx = s.LastIndexOf('.');  if (idx != -1) {     Console.WriteLine(s[..idx]); // "My. name. is Bond"     Console.WriteLine(s[(idx + 1)..]); // "_James Bond!" } 

Original Answer (for C# 7 and below)

This is the original answer that uses the string.Substring(int, int) method. It's still OK to use this method if you prefer.

string s = "My. name. is Bond._James Bond!"; int idx = s.LastIndexOf('.');  if (idx != -1) {     Console.WriteLine(s.Substring(0, idx)); // "My. name. is Bond"     Console.WriteLine(s.Substring(idx + 1)); // "_James Bond!" } 
like image 106
Phil K Avatar answered Sep 27 '22 21:09

Phil K


You can also use a little bit of LINQ. The first part is a little verbose, but the last part is pretty concise :

string input = "My. name. is Bond._James Bond!";  string[] split = input.Split('.'); string firstPart = string.Join(".", split.Take(split.Length - 1)); //My. name. is Bond string lastPart = split.Last(); //_James Bond! 
like image 29
Pierre-Luc Pineault Avatar answered Sep 27 '22 21:09

Pierre-Luc Pineault