Reputation: 157
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.
Upvotes: 1
Views: 11347
Reputation: 959
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);
Upvotes: 2
Reputation: 91724
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.
Upvotes: 1
Reputation: 112915
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();
Upvotes: 9
Reputation: 1570
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();
}
Upvotes: 0
Reputation: 7911
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();
}
Upvotes: 0
Reputation: 8666
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;
}
Upvotes: 1
Reputation: 31206
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
Upvotes: 0
Reputation: 21897
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
}
Upvotes: 0