Ya Wang
Ya Wang

Reputation: 1808

Annoying String Substring & IndexOf

I think I solved my main issue although I do not understand it enough so I shall ask someone here to point me to a good explanation or explain to me like I am 5.

Consider the following...

 String myString = "something that thing, another thing"; 
 Console.WriteLine(myString.SubString(0, myString.Length)); 

The above will just print out the whole string, that's fine. I didn't make any real changes to myString.

Continuing, with the same myString

Console.WriteLine(myString.SubString(0, myString.LastIndexOf(','))); 

That would give me "something that thing" as expected.

Now here comes the silly part, because to my understanding I never changed myString by using LastIndexOf() or .Length

Console.WriteLine(myString.SubString(myString.LastIndexOf(','), myString.length)); 

That will throw an ArgumentOutOfIndexExpcetion. Because apparently I changed myString... So that now... the proper way to get the half of the string after the ',' is by doing...

Console.WriteLine(myString.SubString(myString.LastIndexOf(','), (myString.length - myString.LastIndexOf(',')))); 

Main question

Why the hell did I need to reformat my index if I never changed myString to begin with... Aren't all methods within the String class made so that the String never changes.

SO the Java subString is different? since its beginindex to endindex... http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#substring(int,%20int)

Upvotes: 1

Views: 843

Answers (5)

scubasteve623
scubasteve623

Reputation: 647

Yes, I find myself doing this a lot

text = text.substring(text.indexOf(something),text.indexOf(somethingElse));

Whereas C# is

text = text.Substring(text.IndexOf(something),text.Length - text.IndexOf(somethingElse));

Java has it's own stuff that annoys me too though. Why string == is by reference and not by value, I'll never understand.

Upvotes: 0

Jonathan Dickinson
Jonathan Dickinson

Reputation: 9218

The problem is that that style of Substring is only performed by JS/Java, so far as I know. The signature of Substring in .Net is:

string Substring(int index, int length);

That means .Net (and most other standard libraries) will grab the string from index until index + length. index + length is past the end of the string and hence you receive the exception. What you are possibly looking for is this:

Console.WriteLine(myString.Substring(myString.LastIndexOf(',') + 1));

Which will grab everything after the last occurrence of ','. If ',' does not occur it will give you the whole string. If you don't specify the length argument, Substring will give you everything from the index you specify up until the end of the string.

Upvotes: 0

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186668

Probably you're looking for

  Console.WriteLine(myString.SubString(myString.LastIndexOf(','))); 

which means: "start from myString.LastIndexOf(',') and up to the end of the string".

Upvotes: 3

Joel Etherton
Joel Etherton

Reputation: 37533

You aren't reformatting your index, you're reformatting the length of the string you're taking. The SubString method accepts a starting index and a length. So in your code:

Console.WriteLine(myString.SubString(myString.LastIndexOf(','), myString.length)); 

You're starting at the last index of the character ',', but you're still telling it to take the entire length of the string. It can't do that. You're telling it to start at the 21st character and take the next 35. Your string would have to have 56 characters in order to do that.

Upvotes: 2

Anton Gogolev
Anton Gogolev

Reputation: 115731

It's a really simple error. The Substring(int startIndex, int length) is guaranteed to throw ArgumentOutOfRangeException when

startIndex plus length indicates a position not within this instance.

Which is exactly what you're seeing: last index of "," plus the total length of the string exceed the total length of the string you're Substringing from.

Upvotes: 2

Related Questions