0

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))
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Troy Mitchel
  • 1,790
  • 11
  • 50
  • 88
  • 2
    Do you want the text after the *third* comma, or after *the last* comma (which happens to be the same in your given sample)? If you have the input `"One, two, three, four, five and six"`, what would be your expected result? `"four, five and six"` or `"five and six"`? – Fredrik Mörk Apr 20 '11 at 12:48
  • yes after third comma. i sould of put a fourth one in there, i edited it for clarity. – Troy Mitchel Apr 20 '11 at 13:00

6 Answers6

3

Heres my two cents:

string.Join(",", "aaa,bbb,ccc,ddd,eee".Split(',').Skip(2));
Craig
  • 6,869
  • 3
  • 32
  • 52
2

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.

Chris Snowden
  • 4,982
  • 1
  • 25
  • 34
1

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")
Chandu
  • 81,493
  • 19
  • 133
  • 134
0

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)
detaylor
  • 7,112
  • 1
  • 27
  • 46
0

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;
        }
    }
Community
  • 1
  • 1
Dipti Mehta
  • 537
  • 3
  • 13
0

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)
Subhash Lama
  • 433
  • 2
  • 12