string str = "part1.abc.part2.abc.part3.abc"; int ix1 = str.LastIndexOf('.'); int ix2 = ix1 > 0 ? str.LastIndexOf('.', ix1 - 1) : -1;
There are always fans of Regexes (and jQuery), so I will give a Regex solution (for a jQuery solution you have to wait :-)):
var match = Regex.Match(str, @"\.[^\.]*\.", RegexOptions.RightToLeft); int ix = match.Success ? match.Index : -1;
(note that I am a hater for regular expressions, I give it to you so that you have enough rope to hang yourself if you so wish).
Keep in mind that I'm using the RegexOptions.RightToLeft
parameter RegexOptions.RightToLeft
that the RegexOptions.RightToLeft
with the last character.
source share