Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cut a string with a known start & end index

Tags:

string

c#

When I have a string that I want to cut into a new string from a certain Index to a certain Index, which function do I use?

If the string was:

ABCDEFG

This would mean retrieving BCD when the two indexes specified were 1 and 3.

like image 715
Jan W Avatar asked Feb 27 '12 16:02

Jan W


People also ask

How do you find substring with start and end index?

You call the Substring(Int32, Int32) method to extract a substring from a string that begins at a specified character position and ends before the end of the string. The starting character position is a zero-based; in other words, the first character in the string is at index 0, not index 1.

How do you slice a string upto a certain character?

You can extract a substring from a string before a specific character using the rpartition() method. rpartition() method partitions the given string based on the last occurrence of the delimiter and it generates tuples that contain three elements where.

How do you extract part of a string?

The substr() method extracts a part of a string. The substr() method begins at a specified position, and returns a specified number of characters. The substr() method does not change the original string. To extract characters from the end of the string, use a negative start position.

How do you get a string before a specific substring?

Use the substring() method to get the substring before a specific character, e.g. const before = str. substring(0, str. indexOf('_')); . The substring method will return a new string containing the part of the string before the specified character.


2 Answers

If endIndex points to the last character that you want to have included in the extracted substring:

int length = endIndex - startIndex + 1; string extracted = s.Substring(startIndex, length); 

If endIndex points to the first character following the desired substring (i.e. to the start of the remaining text):

int length = endIndex - startIndex; string extracted = s.Substring(startIndex, length); 

See String.Substring Method (Int32, Int32) for the official description on Microsoft Docs.


Since C# 8.0, in .NET Core and .NET 5+ only, you can use Indices and ranges

string extracted = s[startIndex..endIndex]; 

where the position at endIndex is excluded. This corresponds to my second example with Substring where endIndex points to the first character following the desired substring (i.e. to the start of the remaining text).

If endIndex is intended to point to the last character that you want to have included, just add one to endIndex:

string extracted = s[startIndex..(endIndex + 1)]; 
like image 195
Olivier Jacot-Descombes Avatar answered Sep 20 '22 13:09

Olivier Jacot-Descombes


This becomes possible with the new Range feature of C# 8.0.

An extension method on string that uses Range to achieve this is:

public static class StringExtensions {     public static string SubstringByIndexes(this string value, int startIndex, int endIndex)     {         var r = Range.Create(startIndex, endIndex + 1);         return value[r];         /*         // The content of this method can be simplified down to:          return value[startIndex..endIndex + 1];          // by using a 'Range Expression' instead of constructing the Range 'long hand'         */     } } 

Note: 1 is added to endIndex when constructing the Range that's used as the end of the range is exclusive, rather than inclusive.

Which can be called like this:

var someText = "ABCDEFG";  var substring = someText.SubstringByIndexes(1, 3); 

Giving a value of BCD in substring.

like image 31
Rob Avatar answered Sep 18 '22 13:09

Rob