有谁知道为什么:

"  <exception>".LastIndexOf("<",0) returns -1 (wrong)


"  <exception>".LastIndexOf("<") returns 2 (right)


"<exception>".LastIndexOf("<",0) returns 0 (right)

这是错误还是我误解了LastIndexOf-Method?

最佳答案

您误解了LastIndexOf方法的特定重载。

docs声明以下内容:



请注意,它说backward。因此,如果从位置0开始,则该位置或该位置之前没有"<"子字符串,因此结果为-1

相反,如果您使用仅接受子字符串的重载,则搜索将从字符串的末尾开始,因此可以正确找到所指示的子字符串。

关于c# - string.LastIndexOf()错误?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12421160/

10-11 04:38