lastindexof_每日大脑屁-LastIndexOf坏了吗? 哦,不,'是我。

lastindexof

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值