Get Second to last character position from string

前端 未结 10 1827
再見小時候
再見小時候 2020-12-19 08:11

I have a dynamically formed string like - part1.abc.part2.abc.part3.abc

In this string I want to know position of second to last \".\" so that i can split string as

10条回答
  •  别那么骄傲
    2020-12-19 09:00

    Here is another solution:

             string aString = "part1.abc.part2.abc.part3.abc";
            // Getting string until last dot
            var untilLastDot = aString.Substring(0, aString.LastIndexOf("."));
            // Now we have string until last dot and last dot here will be last but one 
            // and getting text from last but one dot to end
            string lastWordButOne = aString.Substring(untilLastDot.LastIndexOf(".") + 1);
            // Result: part3.abc
    

    hope helps, Thanks!

提交回复
热议问题