Pretty basic, I'm just curious how others can implement this algorithm and would like to see if there is any clever move Optimizing the algorithm ... I had to implement it for the project on which I am working.
Looking at a string in Camel Cass, how would you be able to "spying" it?
Like FooBarGork should return me Foo Bar Gork
Here's my algorithm in C #:
Fixed zero main (string [] args) {Console.WriteLine (UnCamelCase ("FooBarGork")); } Public static string UnCamelCase (string str) {StringBuilder sb = New StringBuilder (); For (int i = 0; i
Since you have to go through each character once, I believe that the best case is (N). How can you apply it?
I can already feel a sense of fire, but I like the type of reggae stuff .. Public static string UnCamelCase (string str) {returning Regex.Replace (str, "([az]) ([AZ]),", " $ 1 $ 2 "); }
(This can not be faster than your implementation, but me is more obvious.)
And of course, It will be (at runtime) much faster (may be)
private static reggaes _unCamelRegex = new Regex ("([az]) ([AZ ]) ", Regex Option compiled); Public static string UnCamelCase (string str) {return _unCamelRegex.Replace (str, "$ 1 $ 2"); }
This will handle the issue brought by Pete Kirkham (such as the HTTPRequest, such as a camel-sealed wire):
Private Stable Regex _unCamelRegex1 = New Regex ("([az]) ([AZ])", RegexOptions.Compiled); Private Static Reggae _unCamelRegex2 = New Reggae ("([A-Z]) ([A-Zed]) ([A-Zed])", Reggae Options Compact; Public static string UnCamelCase (string str) {return _unCamelRegex2.Replace (_unCamelRegex1.Replace (str, "$ 1 $ 2"), "$ 1 $ 2 $ 3"); }
This takes a HTTPRequestFOOBarGork
and returns the HTTP request FOO bar gork
OP The method of running the regular expression method with implementation ('start with 1 and leave 0 check') and my second answer (with static compiled reggae object). Note that the compilation time of regesx does not include the result. 2 million calls (using the same phonergor input):
Eaterative: 00: 00: 00.80
Reggae: 00:00: 06.71
So it is clear that the recurrence approach very is more efficient, I included a definitive version of OP implementation (by Jason Poonon, any credit should go to them) , Which also takes into account an empty or empty argument:
public static stream C UnCamelCaseIterative (String str) {if (String.IsNullOrEmptE (str)) return str; / * Note that ostring () is required, otherwise the characters are completely converted into integers and incorrectly loaded CTOR * / stringbilder sb = new stringbirder (str [0] .tostring ()); For (int i = 1; i & lt; str.Length; i ++) {if (char.IsUpper (str, i)) sb.Append (""); Sb.Append (str [i]); } Return sb.ToString (); }
Comments
Post a Comment