5

I am taking a line from a file only if that file doen't have a specific pattern.. and i want to take from that line the last 3 chars... my code is:

        while (!line.Contains(pattern))
        {
             String num = line.Substring((line.Length - 3), (line.Length - 2));
             System.Console.WriteLine(num);
        }

but i get an error..

Index and length must refer to a location within the string. Parameter name: length

why i get that? i am starting the new string 3 chars before the end of the line and i stop 2 chars before.. :\

5
  • Presumably your string is less than 3 characters long. Commented Nov 11, 2011 at 17:25
  • 1
    Starting 3 chars before, stopping 2 chars before makes 1 character. Do you want 3 characters or 1 character? Commented Nov 11, 2011 at 17:25
  • How long is your line? If you don't check the length before substring you will certainly create issues. Commented Nov 11, 2011 at 17:26
  • i dont know the lines length cause it is not the same all the times... i just want the last 3 chars... Commented Nov 11, 2011 at 17:28
  • Just tossing this out, but there's probably a nifty RegEx way to just parse all the lines at once and match the last 3 chars on each line. Commented Nov 11, 2011 at 17:40

6 Answers 6

9

Substring takes an offset and then a number of characters to return:

http://msdn.microsoft.com/en-us/library/aa904308%28v=VS.71%29.aspx

So:

String num = line.Substring((line.Length - 3), 3);

This of course assumes that line.Length > 3. You could check with:

String num = (line.Length < 3) ? line : line.Substring((line.Length - 3), 3);
Sign up to request clarification or add additional context in comments.

5 Comments

+1 - this Q was always going to attract a flood of answers but you got in there first ;-) Some other answers seem to think that 4 should be subtracted from the length, which is clearly incorrect.
That's incorrect. Arrays are indexed starting from 0, so there is no char which index is line.Length
@rotman, think of a string with length 3. If I take length -3 then I get zero which is the first character.
@rotman: could you than please give an example when this code fails?
Yea when I was writing my answer, I almost typed - 4 and actually had to draw out the array on paper to make sure I was right :)
2

Second argument of Substring is how many chars it have to take starting from first argument. It should just look like that: String num = line.Substring(line.Length - 3, 3);

2 Comments

Ah, the old no-comment answer edit. So now I have to comment on my comment so I don't look like an idiot. Thank you for changing the -4 to a -3.
My mistake once again. Sorry mate, got to remember about that.
1

This is dangerous. What if the length of the line is < 3? You should probably check this otherwise you will get an exception.

In addition you should use the substring method as depicted here :

String num = line.Substring((line.Length - 3), 3);

1 Comment

@AdamRalph: What a finely worded and insightful comment. ;)
1

The problem is that you try to get more characters then your array have.

Extensions are best for problems like this one ;) Mine have some dirty name but everyone know what it would do - this is exception safe substring:

public static string SubstringNoLongerThenSource(this string source, int startIndex, int maxLength)
{
    return source.Substring(startIndex, Math.Min(source.Length - startIndex, maxLength));
}

So in your exact problem it should be like that:

String num = line.SubstringNoLongerThenSource((line.Length - 3), 3);
System.Console.WriteLine(num);

So num will have max 3 letters if the string you provide to function have enough letters :)

Comments

0
String num = line.Substring(line.Length - 3)

2 Comments

Explaining your snippet would be much more helpful.
If you specify only the starting position of the substring, the substring is taken as a whole after it. Since this case must be followed by last three letters, (Length-3) is sufficient to specify.
-1

This is happening because the last parameter of Substring() should be the length of the string to extract.

In your case it should be 3

, and not line.Length - 2

The first parameter should also be:

line.Length - 3

1 Comment

You're correct, no idea why I put that. I have edited my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.