CamelCased String Split Algorithm

Pretty simple, I'm just curious how others can implement this algorithm and would like to see if there are any smart tricks to optimize the algorithm ... I just needed to implement this for the project I'm working on.

Given that there is a line in CamelCase, how would you “place” it?

eg. given by FooBarGork I want Foo Bar Gork back.

Here is my algorithm in C #:


static void Main(string[] args)
{
    Console.WriteLine(UnCamelCase("FooBarGork"));
}
public static string UnCamelCase(string str)
{
    StringBuilder sb = new StringBuilder();
    for (int i =  0; i < str.Length; i++)
    {
        if (char.IsUpper(str, i) && i > 0) sb.Append(" ");
        sb.Append(str[i]);
    }
    return sb.ToString();
}

Since you should visit each character every time, I think O (n) is the best case. How do you implement this?

+3
source share
12 answers

, .

public static string UnCamelCase(string str)
{
    return Regex.Replace(str, "([a-z])([A-Z])", "$1 $2");
}

( , , .)

, , ( )

private static Regex _unCamelRegex = new Regex("([a-z])([A-Z])", RegexOptions.Compiled);

public static string UnCamelCase(string str)
{
    return _unCamelRegex.Replace(str, "$1 $2");
}

, ( , HTTPRequest):

private static Regex _unCamelRegex1 = new Regex("([a-z])([A-Z])", RegexOptions.Compiled);
private static Regex _unCamelRegex2 = new Regex("([A-Z]+)([A-Z])([a-z])", RegexOptions.Compiled);

public static string UnCamelCase(string str)
{
    return _unCamelRegex2.Replace(_unCamelRegex1.Replace(str, "$1 $2"), "$1 $2$3");
}

HTTPRequestFOOBarGork HTTP Request FOO Bar Gork


, OPs ( "start at 1 skip the > 0 check" ) ( Regex). , Regex. 2 ( FooBarGork):

: 00: 00: 00.80
Regex: 00: 00: 06.71

, , . OPs ( , ), :

public static string UnCamelCaseIterative(string str)
{
    if (String.IsNullOrEmpty(str))
        return str;

    /* Note that the .ToString() is required, otherwise the char is implicitly
     * converted to an integer and the wrong overloaded ctor is used */
    StringBuilder sb = new StringBuilder(str[0].ToString());
    for (int i = 1; i < str.Length; i++)
    {
        if (char.IsUpper(str, i))
            sb.Append(" ");
        sb.Append(str[i]);
    }
    return sb.ToString();
}
+18

1?

&& i > 0 ...

+2

, "HTTPRequest" "HTTP-", " H T T P", -.

+1

PHP

function spacify($str) {
  return preg_replace('/([a-z])([A-Z])/', "\1 \2", $str);
}
+1

, , - ( ). for, , if char.IsUpper sb.Append(" "). . , O (n) .

, , RegEx replace , . RegEx, .

0

, , , , , :

str=str.replace(str[i], " "+str[i]);

, .

0

...

public static string UnCamelCase(string str) {
    Regex reg = new Regex("([A-Z])");

    return reg.Replace(str, " $1").Trim();
}
0

"\ u" ( ) "\ U" ( ). :

(?<=\U)(?=\u)

. , , :

(?<=[a-z])(?=[A-Z])   // replace with a single space again

: . CamelCasedWords - , .

CamelCasedWord
    ^^   ^^           // match occurs between the ^
0

- (Python)?

>>> s = 'FooBarGork'
>>> s[0] + re.sub(r'([A-Z])', r' \1', s[1:])
'Foo Bar Gork'
0

, :

    public static string UnCamelCase(string str)
    {
        StringBuilder sb = new StringBuilder();

        foreach (char c in str.ToCharArray())
        {
            if (System.Convert.ToInt32(c) <= 90) sb.Append(" ");
            sb.Append(c);
        }
        return sb.ToString().Trim();
    }


        //Console.WriteLine(System.Convert.ToInt32('a')); // 97
        //Console.WriteLine(System.Convert.ToInt32('z')); // 122
        //Console.WriteLine(System.Convert.ToInt32('A')); // 65
        //Console.WriteLine(System.Convert.ToInt32('Z')); // 90
0

javascript mootools ( "", .

/*
Property: hyphenate
    Converts a camelCased string to a hyphen-ated string.

Example:
    >"ILikeCookies".hyphenate(); //"I-like-cookies"
*/

hyphenate: function(){
    return this.replace(/\w[A-Z]/g, function(match){
        return (match.charAt(0) + '-' + match.charAt(1).toLowerCase());
    });
},
0
echo "FooBarGork" | sed -r 's/([A-Z])/ \1/g;s/^ //'
0

All Articles