![lastindexof](https://i-blog.csdnimg.cn/blog_migrate/4206b3c2c2c3a126fb9b3922a5f78b48.png)
lastindexof
Often, in the scope of writing massively scalable enterprise systems that manage your finances, I parse strings. Mostly in my spare time.
通常,在编写管理您的财务的可大规模扩展的企业系统的范围内,我会解析字符串。 主要是在业余时间。
What's happening here? I was reminded today that the start index passed to LastIndexOf() was from the ass-end of the string. I then lamented the obviousness of this and was then humbled by the silent-but-deadly odor my own brain fart.
这里发生了什么事? 今天提醒我,传递给LastIndexOf()的起始索引来自字符串的ass-end。 然后我为此感叹不已,然后被我自己的大脑放屁中无声但致命的气味所羞辱。
I'm glad you're that much more confident in my abilities after reading this post. Up with TDD! Down with ADD!
很高兴您阅读本文后对自己的能力更加自信。 上TDD! 拒绝添加!
public static void Main()
{
int newlineIndex = -1;
string x = "bob\neric\nfred\nted";
string z = "bob\r\neric\r\nfred\r\nted";
newlineIndex = x.LastIndexOf('\n',0);
Console.WriteLine(newlineIndex); //Expect 13, always get -1
newlineIndex = z.LastIndexOf('\n',0);
Console.WriteLine(newlineIndex); //Expect 16, always get -1
newlineIndex = x.LastIndexOf('\n');
Console.WriteLine(newlineIndex); //Expect 13, get 13
newlineIndex = z.LastIndexOf('\n');
Console.WriteLine(newlineIndex); //Expect 16, get 16
//Fart realized...
newlineIndex = x.LastIndexOf('\n',x.Length-1);
Console.WriteLine(newlineIndex); //Expect 13, always get 13
newlineIndex = z.LastIndexOf('\n',z.Length-1);
Console.WriteLine(newlineIndex); //Expect 13, always get 13
}
公共静态无效Main()
{
int newlineIndex = -1;
字符串x = “ bob \ neric \ nfred \ nted” ;
字符串z = “ bob \ r \ neric \ r \ nfred \ r \ nted” ;
newlineIndex = x.LastIndexOf('\ n',0);
Console.WriteLine(newlineIndex); //期望13,总为-1
newlineIndex = z.LastIndexOf('\ n',0);
Console.WriteLine(newlineIndex); //期望16,总为-1
newlineIndex = x.LastIndexOf('\ n');
Console.WriteLine(newlineIndex); //期望13,得到13
newlineIndex = z.LastIndexOf('\ n');
Console.WriteLine(newlineIndex); //期望16,得到16
//放屁了...
newlineIndex = x.LastIndexOf('\ n',x.Length-1);
Console.WriteLine(newlineIndex); //期望13,总得到13
newlineIndex = z.LastIndexOf('\ n',z.Length-1);
Console.WriteLine(newlineIndex); //期望13,总得到13
}
翻译自: https://www.hanselman.com/blog/the-daily-brain-fart-lastindexof-broken-oh-no-tis-me
lastindexof