如何通过检测逗号从索引中获取最后一个字符串

本文关键字:获取 最后一个 字符串 索引 何通过 检测 | 更新日期: 2023-09-27 18:10:16

如何通过检测逗号从索引中获取最后一个字符串?例子:字符串I = a,b,c,d或I = ab, cd, ef, gh(I can的字符串是动态的)那么我如何从I中得到最后一个字符串??(我需要c#的答案,并尽可能使用低消耗性能的方式)

result: a = ad, cd, efB = gh

如何通过检测逗号从索引中获取最后一个字符串

您可以使用String。子字符串和字符串。LastIndexOf方法(Char)获取最后一个逗号

的索引
string last = str.Substring(str.LastIndexOf(',')+1)

您也可以试试这个:

string[] strArr = inputStr.Split(',');
Array.Reverse(strArr);
Debug.Print(strArr[0]);

注意:即使字符串没有,(逗号),这段代码也不会崩溃。

希望有帮助!