Free pascal split string4/10/2023 ![]() It's true the non-regex version is faster, but I have a hard time imagining a case where you need to do camelCase splitting in under 2.9 microseconds. But simpler *means* it's easier to read, and errors are more likely to be caught in development. ![]() You're right that your regex approach is simpler. How confident are you that the (additional) bugs in the non-regex version would *all* be syntax errors and type errors? That is a bet I would not want to take! The non-regex one is 10 times longer so it probably has 10 times more bugs. The C# compiler will only stop on syntax errors and type errors, not logic bugs (like off-by-one errors, which are common with arrays). "All compiled code, so errors are more likely to be caught in development" NET standard library? If not, how far do you take this? Not everyone knows System.Collections, so should I write my own collections classes? If they're a C# programmer, shouldn't they be able to learn a (very common) class in the. "Easier for other programmers to read - not everyone knows RegEx" To anyone who finds it useful or helpful as I did, James' link for the Pascal Camel Case method works perfectly in. Replace(String, String, MatchEvaluator, RegexOptions)īut replacement works sometime and sometimes not in production environment but the same code works on local and dev environment This code is great I have also used such type of code like Private static string InlineRegExTest(string input, int iterations) If you change the function "InlineRegExTest" same like the following, you will find it's faster than "DelegateRegExTest": I'd recommend the following update to your regular expression: Well, mine is faster and has published performance numbers.Ĭan you do it the other way around? From "these words want camel" to "TheseWordsWantCamel"? My code isn't as good as the code Leon posted, and his was an example of lame code. Otherwise you need more advanced logic to split up the words then a 1 liner can handle.įunny. This is a quick way to do this (for whatever reason), but it would be best to not lose those spaces between the words in the first place. This method won't even seperate words correctly. Haacked: Obviously it woun't seperate the numbers. Plus if it is a title then prepositions such as 'the', 'a', or 'an' would remain lower case. When would a function like this be needed? I can imagine problems with Irish/Scotish/Italian names such as McDonalds, MaCarthy, DeSando and also upper case trademarks/abbreviations like IBM, ASP, etc. Simpler, but above all, much more elegant. Should be matching on '(?)' rather than just '([A-Z)' since I'm referring to the match by name.But, Delegate Replace doesn't work in your example. Words.Add( new String(letters, wordStartIndex, letters.Length - wordStartIndex)) Words.Add( new String(letters, wordStartIndex, i - wordStartIndex)) Grab everything before the current index. StringCollection words = new StringCollection() Format("Output from Inline RegEx approach: //Return empty array. Parse(Console.ReadLine()) Ĭonsole.WriteLine( string. (updated - fixed a code error with delegate method)Ĭonsole.WriteLine("Enter CamelCase text to split (defaults to SampleSplitText):") Ĭonsole.WriteLine("Enter number of operations ( enter 0 to quit):") For my very simple test, it was just about as fast for anything under 100000 repetitions. For high repetitions, I'd consider using a RegEx replace with a MatchEvaluator delegate (see the code below). For under a thousand iterations, I'd definitely go with the RegEx replace. The only reason for calling this out is to show the exceptionally slow performance of the RegEx replace method for high iterations. So, let's compare performance. Now, this is mostly academic since this kind of function would likely be called less than 25 times, but still worth a look. Here are the sample string "SampleSplitText": Approach
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |