find a location for property in a new city

Tuesday, 17 April 2012

What to do with LastIndexOf unexpected ArgumentOutOfRangeException behaviour

Using string.LastIndexOf(char value, int startIndex, int count); started giving me some behaviour I wasn't expecting when it started giving me an ArgumentOutOfRangeException Exception with a description of: "Count must be positive and count must refer to a location within the string/array/collection."

Let me give you an example:

var message = ".NET is unusually faultless!";
var maxSearch = 16;
var count = message.LastIndexOf('l', 0, maxSearch);
var newMsg = message.Substring(0, count);

This should work right? What could be wrong with this? Well according to IntelliSense "Reports the index position of the last occurrence of the specified Unicode character in a substring within this instance. The search starts at a specified character position and examines a specified number of character positions." Hmm.. Okay. And the count parameter that I am getting wrong? "The number of character positions to examine." Seems legit.

I decided to disassemble .NET to have a peep at what's going on internally... Have I found a bug in .NET?! Can't tell because the method is extern, so I cannot look at it. I instead did some experimenting and worked it out.

How to properly use LastIndexOf

Turns out I was just using it incorrectly and blaming my tools. Although I am convinced some blame lies with IntelliSense for forgetting to mention that LastIndexOf searches backwards through the string! Should I have known that? I don't know. Anyway this is what I should have written:

var message = ".NET is unusually faultless!";
var maxSearch = 16;
var count = message.LastIndexOf('l', maxSearch, maxSearch);
var newMsg = message.Substring(0, count);

The startIndex parameter should be at the last point you want to search backwards from. The count then specifies the amount of chars for your search to go back through.

Maybe I'm just being stupid or maybe it is just that Console.WriteLine(newMsg);

Follow britishdev on Twitter

60 comments:

  1. This is such an informative article and very clearly written. Every single thought and idea is direct to the point. Perfectly laid out. Thank you for taking your time sharing this to you readers. http://scarprin.biz/

    ReplyDelete
  2. Thanks for the post and information! I think education is important for us so we must prepare the best education for our generation by sharing such great information with each other!
    http://www.fadeplex.com

    ReplyDelete
  3. This has to be one of my favorite posts! And on top of thats its also very helpful topic for newbies. Thanks a lot for informative information!
    bar sets with stools

    ReplyDelete
  4. Very helpful indeed. I stumbled over the exact same thing and am glad to know why.

    ReplyDelete
  5. Great post! I am so happy I have found your site. I already have the best essays site ever and now I finally have the best developing site ever as well! Thank you for posting!

    ReplyDelete
  6. Nice post!..good information,it is really helpful..it really impressed me alot and i just loved it.Thanks for posting such an informative content:
    fleeingthecomplexgame.com Warwings.net Flipmaster.co

    ReplyDelete
  7. Motifz Designer Lawn. 1, 2 & 3 Piece Unstitched Premium Embroidered Lawn 2019, Premium Lawn, Lawn 2019, Best lawn collection, Motifz, Best Lawn in Pakistan, Shipping worldwide. Stitching option available.

    ReplyDelete
  8. original article here are the details of How hair follice drug test work

    ReplyDelete
  9. Wholesale printed custom perfume boxes with the highest quality and affordable prices. Free worldwide Delivery and free Custom Perfume Boxes design.

    ReplyDelete
  10. Can you guess how much these celebrities are worth? Test your knowledge with Celebrity net worth.

    ReplyDelete
  11. Buy online wholesale printed custom cbd boxes and packaging at easy prices and shipping worldwide, with 24/7 customer support. Get a free custom quote now!!

    ReplyDelete
  12. Buy online wholesale printed custom candle boxes and packaging at easy prices and shipping worldwide, with 24/7 customer support. Get a free custom quote now!!

    ReplyDelete
  13. Blackbird packaging provide you with any size, shape design, lipstick boxes sooner than any other packaging competitor in the market

    ReplyDelete
  14. Packaging town is a one stop solution for the short-run cbd face oil boxes, Wholesale Custom Boxes needs and supplies. At affordable prices. Request a free quote now.

    ReplyDelete
  15. mylar label bags in stock now. Ready made Cali pack designs. Fast Shipping at Your Supplies. Custom designs available

    ReplyDelete
  16. Packaging town is a one stop solution for the short-run cream boxes needs and supplies. At affordable prices. Request a free quote now.

    ReplyDelete
  17. The information you have posted is very useful. The sites you have referred was good. Thanks for sharing.
    Reference:
    Web Design Minneapolis

    ReplyDelete
  18. Great Article
    Cloud Computing Projects




    Networking Projects



    Final Year

    Projects for CSE





    JavaScript Training in Chennai



    JavaScript

    Training in Chennai




    The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals,

    Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer.

    Angular Training

    ReplyDelete
  19. Blackbird packaging provide you with any size, shape design, custom cbd cream boxes sooner than any other packaging competitor in the market

    ReplyDelete
  20. Packaging town is a one stop solution for the short-run kraft business card boxes needs and supplies. At affordable prices. Request a free quote now.

    ReplyDelete
  21. Blackbird packaging provide you with any size, shape design, custom wholesale cookie boxes sooner than any other packaging competitor in the market

    ReplyDelete
  22. Blackbird packaging provide you with any size, shape design, custom cbd tincture boxes sooner than any other packaging competitor in the market

    ReplyDelete
  23. Packaging town is a one stop solution for the short-run custom printed apparel boxes needs and supplies. At affordable prices. Request a free quote now.

    ReplyDelete
  24. MATLAB, which stands for Matrix Laboratory, is a high-performance language. You might find yourself looking for
    MATLAB assignment help online if you’re not familiar with its functionalities. In this article, we’ll look at some of the necessary toolboxes that MATLAB offers. If you familiarise yourself with these, then finalising those assignments will be a piece of cake.

    Neural network toolbox
    A neural network toolbox is a string of algorithms that try to establish a connection between data, similar to how the human brain works. You must have read about neurons when searching for your science essay homework help online. The word ‘neural’ comes from ‘neuron’. You can use this tool to perform functions like classification, regression, weather forecasting, and many more.

    https://cooljonny.com/blog/clarasmith/Five-Major-Toolboxes-Available-in-MATLAB

    ReplyDelete
  25. Blackbird packaging company can provide you with any size, shape design, and cbd bath bombs wholesale sooner than any other packaging competitor in the market

    ReplyDelete
  26. Buy online sweatshirts from wild goat clothing. get free shipping

    ReplyDelete
  27. buy electronics online from alfatah at best prices. get free shipping

    ReplyDelete
  28. buy custom soap boxes online from packaging town at best prices. Get Free shipping

    ReplyDelete
  29. buy business card boxes wholesale from packaging town at best prices. Get free shipping.

    ReplyDelete
  30. Shop from a wide variety of Local and Imported hair masks online available at Al-Fatah. Shop hair masks and hair moisturizers in wide variety from favorite brands today available under one roof at Al-Fatah. Shop the best for your Hair. Avail Free Delivery on order above 1000pkr in selected cities only.

    ReplyDelete
  31. Blackbird packaging company can provide you with any size, shape design, and bath bomb packaging sooner than any other packaging competitor in the market

    ReplyDelete
  32. Buy lawn collection 2021 online from Ilaha Clothing at best prices.get free shipping.

    ReplyDelete
  33. Blackbird packaging company can provide you with any size, shape design, and bath bomb boxes sooner than any other packaging competitor in the market

    ReplyDelete
  34. buy pret collection 2021 online from Ilaha clothing at best prices. Get free shipping.

    ReplyDelete
  35. Blackbird packaging provide you with any size, shape design, wholesale cookie boxes wholesale sooner than any other packaging competitor in the market

    ReplyDelete
  36. Packaging town is a one stop solution for the short-run custom pillow boxes needs and supplies. At affordable prices. Request a free quote now.

    ReplyDelete
  37. insulins online online from Medisense.pk. Get free shipping.

    ReplyDelete
  38. Thanks for sharing information with us…satisfied after visiting your blog…. Learn the latest trends & their practical application by experts with us…you can contact us click on
    Visit ISMT India

    ReplyDelete
  39. buy cosmetics online from alfatah at best prices delivered at your doorstep. get free delivery

    ReplyDelete
  40. Buy mens gym bottoms from dshred at easy prices delivered at your doorstep. Get free shipping.

    ReplyDelete
  41. buy gym wear online pakistan online from dshred at easy prices delivered at your doorstep. Get free shipping

    ReplyDelete
  42. buy active wear online pakistan online from dshred at best prices delivered at your doorstep. Get free shipping

    ReplyDelete
  43. buy reflective vest online from dshred at easy prices delivered at your doorstep. get free shipping

    ReplyDelete
  44. NDURE brings you its wide range of Men's t-shirt. Now you can buy your favorites with just a tap. You can find our t-shirts online by logging into our site. Our shirts are made with pure fabric that makes you stand out. So don't wait around and shop from our collection of online t-shirts.

    ReplyDelete
  45. buy sports bra pakistan online from Dshred at easy prices delivered at your doorstep. Get free shipping

    ReplyDelete
  46. buy leggings for girls online from dshred at easy prices delivered at your doorstep. Get free shipping

    ReplyDelete
  47. Buy gym trousers for men online from Dshred at best prices delivered at your doorstep. Get free shipping

    ReplyDelete
  48. Now You can get best quality multivitamins in pakistan online from medisense where all kind of supplements and multivitamins are available. Quick delivery.

    ReplyDelete
  49. Buy online lactogen price in Pakistan from medisense where all kind of medicine and baby foods are available. Quick delivery.

    ReplyDelete
  50. Buy best quality blood pressure machine in Pakistan online at affordable prices from medisense. Quick Delivery

    ReplyDelete
  51. Buy Online certeza nebulizer price in Pakistan from medisense where all kind of nebulizer and steamers are available. Quick delivery.

    ReplyDelete
  52. Buy mens gym trousers online from Dshred at best prices delivvered at your doorstep. Get free shipping

    ReplyDelete
  53. Buy men for shorts online from ndure at best prices delivered at your doorstep. Get free shipping.

    ReplyDelete
  54. Buy women accessories online from Al-Fatah at best prices delivered at your doorstep. Get free shipping

    ReplyDelete
  55. Buy grocery online in Pakistan at best prices delivered at your doorstep. Get free shipping

    ReplyDelete
  56. buy tissue boxes online from Al-Fatah at best prices delivered at your doorstep. Get free shipping.

    ReplyDelete