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.
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.
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.
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.
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.
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)];
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With