Troy Mitchel
Troy Mitchel

Reputation: 1810

Substring starting at specific character count

How would you select the last part of a string starting at a specific character count. For example I would like to get all text after the 3rd comma. but I get an error saying "StartIndex cannot be less than zero."

Dim testString As String = "part, description, order, get this text, and this text"
Dim result As String = ""
result = testString.Substring(testString.IndexOf(",", 0, 3))

Upvotes: 0

Views: 11466

Answers (6)

Craig
Craig

Reputation: 7076

Heres my two cents:

string.Join(",", "aaa,bbb,ccc,ddd,eee".Split(',').Skip(2));

Upvotes: 3

Subhash Lama
Subhash Lama

Reputation: 433

I think this is what you are looking for

    Dim testString As String = "part, description, order, get this text"
    Dim resultArray As String() = testString.Split(New Char() {","c}, 3)
    Dim resultString As String = resultArray(2)

Upvotes: 0

Dipti Mehta
Dipti Mehta

Reputation: 547

The IndexOf function only finds the "First" of the specified character. The last parameter (in your case 3) specifies how many characters to examine and not the occurence.

Refer to Find Nth occurrence of a character in a string

The function specified here finds the Nth occurance of a character. Then use the substring function on the occurance returned.

Alternative , you can also use regular expression to find the nth occurance.

public static int NthIndexOf(this string target, string value, int n)
    {
        Match m = Regex.Match(target, "((" + value + ").*?){" + n + "}");

        if (m.Success)
        {
            return m.Groups[2].Captures[n - 1].Index;
        }
        else
        {
             return -1;
        }
    }

Upvotes: 0

Chandu
Chandu

Reputation: 82903

Alternatives(I assume you want all the text after last comma):

Using LastIndexOf:

' You can add code to check if the LastIndexOf returns a positive number
Dim result As String = testString.SubString(testString.LastIndexOf(",")+1)

Regular Expressions:

Dim result As String = Regex.Replace(testString, "(.*,)(.*)$", "$2")

Upvotes: 1

Chris Snowden
Chris Snowden

Reputation: 5002

The code "testString.IndexOf(",", 0, 3)" does not find the 3rd comma. It find the first comma starting at position 0 looking at the first 3 positions (i.e. character positions 0,1,2).

If you want the part after the last comma use something like this:

Dim testString As String = "part, description, order, get this text"
Dim result As String = ""
result = testString.Substring(testString.LastIndexOf(",") + 1)

Note the +1 to move to the character after the comma. You should really also find the index first and add checks to confirm that the index is not -1 and index < testString.Length too.

Upvotes: 2

detaylor
detaylor

Reputation: 7280

The third argument of indexOf is the number of charcters to search. You are searching for , starting at 0 for 3 characters - that is searching the string par for a comma which does not exist so the returned index is -1, hence your error. I think that you would need to use some recursion:

Dim testString As String = "part, description, order, get this text"
Dim index As Int32 = 0

For i As Int32 = 1 To 3    
  index = testString.IndexOf(","c, index + 1)
  If index < 0 Then
    ' Not enough commas. Handle this.
  End If
Next
Dim result As String = testString.Substring(index + 1)

Upvotes: 0

Related Questions