Feeling dumb, I’m trying to remove a sub string after the last occurrence of a “.”. The code is as follows:
Dim dotIndex As Integer = fileNameCopy.LastIndexOf(".")
Dim dummy As Integer = fileNameCopy.Length - 1
fileNameCopy = fileNameCopy.Remove(dotIndex, dummy)
When I debug, I get an argument out of range exception for the second, counter, parameter; dummy in this case. I’m not sure why; the total length of my test string is 72, when debugging, the dotIndex is 68 and the length is 71, so I’m within the bounds of the string, I’m not sure why I’m getting this error, any help is appreciated.
The second parameter is not the last index of the substring your want to remove but rather the number of characters to be removed after your starting index.
This should work:
You can also just do
which will remove all characters after the one at dotIndex position.
Or you can go an even simpler way. Judging by your variable names, you’re just trying to remove the extension from a filename. Try this: