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);
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/
ReplyDeleteThanks 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!
ReplyDeletehttp://www.fadeplex.com
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!
ReplyDeletebar sets with stools
Very helpful indeed. I stumbled over the exact same thing and am glad to know why.
ReplyDeleteGreat 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!
ReplyDeleteNice post!..good information,it is really helpful..it really impressed me alot and i just loved it.Thanks for posting such an informative content:
ReplyDeletefleeingthecomplexgame.com Warwings.net Flipmaster.co
Avriq MAP
ReplyDeleteAvriq
CCTV Camera
Pest control services
PC Optimization
CP Plus
Termite control services
Thanks for sharing the information. Keep the good work going. clash royale mod hungry shark evolution mod my talking angela apk mod
ReplyDeleteIndia's only Digital News Channel with True Voice of India.
ReplyDeleteFinding #Championsofchange2018 #rahulshivkant
the universal post teams
the universal post youtube
loksabha election 2018
top trending news
top viral news
best trending news in india
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