First time here? Check out the site's "greatest hits" or read a post from the archives. Feel free to leave a comment or ask a question, and consider subscribing to the latest posts via RSS or e-mail. Thanks for visiting!
« CSI: ASP.NET - The one where a double HT... | Main | A Hanselman Review: Doom 3 vs. FarCry vs... »

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.

I'm glad you're that much more confident in my abilities after reading this post.  Up with TDD! Down with ADD!

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
}



Monday, January 31, 2005 5:49:34 PM (Pacific Standard Time, UTC-08:00)
Good post, butnd ADD or copy coding gave the wrong comment for the last WriteLine.
Allan Wolff
Saturday, March 26, 2005 6:49:50 AM (Pacific Standard Time, UTC-08:00)
Cassini is great for hosting the HttpRuntime in your own application (will this make Winforms history?) but I have discovered a serious problem with Cassini. It crashes with any sort of plugin on the page like Flash, etc. Does anyone know why??? - Pras, Toronto
Comments are closed.

Contact

Sponsors

Hosting By

On this page...

Tags

Calendar

<January 2009>
SunMonTueWedThuFriSat
28293031123
45678910
11121314151617
18192021222324
25262728293031
1234567

Archives

Google Ads