0

Why I'm getting:

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

when I compile this code: http://pastebin.com/CW4EcCM8

some part of it:

    public string findFileEnding(string file)
    {
        int index1 = file.IndexOf('.');
        file = file.Substring(index1, file.Length);
        return file;
    }

Thanks;)

4
  • There's no check that index1 is > -1 (That there was even a . in the string). If you do a substring w/ index:-1 it throws that error too... Commented Nov 4, 2011 at 1:53
  • 1
    Not an answer to your question, but a more reliable way to find a file extension is to use the Path class: msdn.microsoft.com/en-us/library/system.io.path.aspx Commented Nov 4, 2011 at 1:58
  • @Rikon Same error, but with a different message... Commented Nov 4, 2011 at 2:05
  • To expand on @CoreyOgburn's comment, you'd want to look at the GetExtension method. Commented Nov 4, 2011 at 2:09

4 Answers 4

4

I am thinking there is a chance Path.GetExtension could be something OP might want instead.

notice that it returns extension with . like .exe

http://msdn.microsoft.com/en-us/library/system.io.path.getextension.aspx

Sign up to request clarification or add additional context in comments.

Comments

2

The second argument to Substring, if present, is the desired length of the substring. So you're asking for a string the same length as file but starting at a position possibly different from 0. This would make the end of your substring to be past the end of file.

Assuming you want to get all of file starting at position index1, you can just leave out the second argument altogether:

file = file.Substring(index1); 

To make this robust, you'll want to put in a few more checks:

  1. file may be null.
  2. The return value of IndexOf may be -1. This would happen if file does not contain a dot.

1 Comment

May want to mention that the OP should also check for IndexOf returning -1.
0

That's not a compiler error, that's a runtime error.

Note the documentation for String.Substring(int, int):

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

So the substring will have the specified length. Therefore, there must be enough characters starting at startIndex to return a substring of the specified length. Therefore, the following inequalities must be satisfied for String.Substring to succeed on an instance s of string:

startIndex >= 0
length >= 0
length > 0 implies startIndex + length <= s.Length

Note that if you just want a substring from index to the end of the string, you can say

s.Substring(index);

Here, the only constraint is

startIndex>= 0
startIndex < s.Length

Comments

0

You would want to do something like this:

public string FindFileEnding(string file)
{
    if (string.IsNullOrEmpty(file))
    {
        // Either throw exception or handle the file here
        throw new ArgumentNullException();
    }
    try
    {
        return file.Substring(file.LastIndexOf('.'));
    }
    catch (Exception ex)
    {
        // Handle the exception here if you want, or throw it to the calling method
        throw ex;
    }
}

3 Comments

I assume you meant string.IsNullOrEmpty(file) for you the first if-statement, as what you have is not valid unless you've defined a custom extension method. You should also check LastIndexOf's return value. Rethrowing that exception would not makes sense.
@CodeNaked Thanks for the code check, hard to do just in the browser. There any sites that validate C# for you? Its not meant to be the complete answer, just a starting point. And if the last index isn't valid then you'll have to handle it either before or after. I originally had it with int index = file.LastIndexOf('.') but removed it as there was nothing stating how they wanted to handle errors or bad values
I'm not aware of any sites that do it, but there are some lightweight tools.

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.