1

This is my code:

private string title(string pth) //I'm passing a path
{
    pth = System.IO.Path.GetFileNameWithoutExtension(pth); // I need an exact filename with no extension
    return pth.Substring(pth.IndexOf('-')+1, pth.Length).Trim(); // trying to return everything after '-'
}

It throws an exception. I have no idea why. It's an extremely easy way to get the title from filename, but it's not working.

I've tried pth.Length-1, but it's not working either.

6
  • 3
    What is the exception? also, what path are you passing into it? Commented Nov 26, 2012 at 17:52
  • 8
    First check for the existence of - ? Commented Nov 26, 2012 at 17:53
  • it's: "system.argumentoutofrangeexception index and length must refer to a location within the string" Commented Nov 26, 2012 at 17:55
  • @SrinivasThatiparthy - yeah, I added "if" statement, but it's not the reason, because every file has "-" in its name, but yeah - it's a good tip, thank you :) Commented Nov 26, 2012 at 17:56
  • @SrinivasThatiparthy wouldn't mater since -1 + 1 = 0 Commented Nov 26, 2012 at 17:58

8 Answers 8

9

You're using the version of the String.Substring method that allows you to specify the number of characters you wish to extract.

However, you're providing the length parameter as the entire length of the string itself -- hence the ArgumentOutOfRangeException.

If you use this version of String.Substring, you can provide a single parameter (startIndex) and you'll automatically get the rest of the string, starting at the index you provide.

So you can change your code from this:

return pth.Substring(pth.IndexOf('-')+1, pth.Length).Trim();

To this:

return pth.Substring(pth.IndexOf('-')+1).Trim();
Sign up to request clarification or add additional context in comments.

Comments

2

Substring(int index, int length), the length should be the length of the substring, not the length of the entire string.

You want:

int index = pth.IndexOf('-');
return pth.Substring(index + 1, pth.Length - index - 1);

Comments

1

the problem is that you are trying to retrieve the substring that is shorter than the length you specified. Also, if the character '-'is at the end of the string, you will get the exception because the index+1 will be outside of the string. This will help:

private string title(string pth) //I'm passing a path
    {
        pth = System.IO.Path.GetFileNameWithoutExtension(pth); // I need an exact filename with no extension
        string retStr = string.Empty;
        if(pth.IndexOf('-')<pth.Length-1)
        {
              retStr = pth.Substring(pth.IndexOf('-')+1).Trim(); // trying to return everything after '-'
        }
        return retStr;
    }

Comments

1

I'd recommend using a Regular Expression in this case. Something like:

private static string title(string pth)
{
   pth = System.IO.Path.GetFileNameWithoutExtension(pth); // I need an exact filename with no extension
   Match m = Regex.Match(pth, @".*\-(?<suffix>.*)$");

   Group suffix = m.Groups["suffix"];
   return suffix.Success ? suffix.Value : pth;
}

Much cleaner.

2 Comments

I wouldn't call this cleaner. Also if this should work as in question you should return pth rather than null if does not exist.
Well, definitely a lot less checking of various lengths and worrying about string offsets. I guess if you're not used to regular expressions, it could be more convoluted. Good point about returning null.
0

I don't know what your exception is, but I'm assuming that - doesn't exist in your string.

If you review the documention for String.IndexOf here, you'll see:

The zero-based index position of value if that string is found, or -1 if it is not

When you do a subString with a -1 start index, it will throw an exception.

I would check for the existence of - first, then if found do your substring:

if(pth.IndexOf('-') != -1)
{
    //Substring code
}

Comments

0

First off, you should tell us what your exception is. that will help

pth.Substring(pth.IndexOf('-')+1, pth.Length)  

looks like it will throw an exception because it will try to take a substring beyond the length.

try

pth.Substring(pth.IndexOf('-')+1)

instead

Comments

0

The second parameter to the String.Substring method is the length of the substring. The length of the substring in this case should always be less than the length of the pth string. You probably meant to do this:

private string title(string pth) //I'm passing a path
{
    pth = System.IO.Path.GetFileNameWithoutExtension(pth);
    return pth.Substring(pth.IndexOf('-')+1, pth.Length - pth.IndexOf('-') - 1).Trim();
}

Comments

0

you need to change your code like this:

private string title(string pth) //I'm passing a path
{
  pth = System.IO.Path.GetFileNameWithoutExtension(pth);
  var indexOfDash = pth.IndexOf('-') + 1; // Add this line
  return pth.Substring(indexOfDash, pth.Length - indexOfDash).Trim();
}

Comments

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.