38

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.

4
  • 10
    Did you search for this before asking? What did you try so far? Feb 27, 2012 at 17:00
  • 17
    I don't know why this has downvotes and it's closed, as it's a real question not found in MSDN documentation. He is not looking for startIndex + count, but startIndex + endIndex.
    – Jorjon
    Mar 28, 2014 at 3:59
  • 6
    Not a real question???? Yeah it is! I found it since I had the same question. Dear casperOne: WT???
    – steve
    Jun 17, 2016 at 15:49
  • This is something that C# 8 ranges provides a solution for (dotnetcoretutorials.com/2018/12/09/range-type-in-c-8), about half way down the page under "A New Substring"
    – Rob
    Jan 10, 2019 at 11:11

4 Answers 4

95

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)];
6

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.

1
  • 1
    Strings can be indexed by Range directly. var substring = someText[1..4].
    – GSerg
    Dec 18, 2020 at 16:32
3

Unfortunately, C# doesn't natively have what you need. C# offers Substring(int startIndex, int length) instead. To achieve Substring(int startIndex, int endIndex), you will need custom implementation. Following extension method can make reusability easier/cleaner:

public static class Extensions
{
    public static string Substring2(this string value, int startIndex, int endIndex)
    {
        return value.Substring(startIndex, (endIndex - startIndex + 1));
    }
}
0

There is two way to substring string..

1 )

public string Substring(
    int startIndex
)

Retrieves a substring from this instance. The substring starts at a specified character position.

2)

public string Substring(
    int startIndex,
    int length
)

Retrieves a substring from this instance. The substring starts at a specified character position and has a specified length.

1
  • 8
    He wants to provide startIndex and endIndex. Substring requires start and length.
    – Chet
    Nov 7, 2016 at 17:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.