Replace the character in the line with the top line of the next in the line (Pascal Casing)

I want to remove all underscores from the uppercase string of the character following the underscore. So, for example: _my_string_becomes: MyStringsimilarly: my_stringbecomesMyString

Is there an easier way to do this? I currently have the following (if no input has two consecutive underscores):

        StringBuilder sb = new StringBuilder();
        int i;
        for (i = 0; i < input.Length - 1; i++)
        {
            if (input[i] == '_')
                sb.Append(char.ToUpper(input[++i]));
            else if (i == 0)
                sb.Append(char.ToUpper(input[i]));
            else
                sb.Append(input[i]);
        }
        if (i < input.Length && input[i] != '_')
            sb.Append(input[i]);

        return sb.ToString();

Now I know that this is not completely related, but I decided to run some numbers in the implementations presented in the answers, and here are the results in milliseconds for each implementation using the 1000000 iterationsstring "_my_string_121_a_"::

Achilles: 313
Raj: 870
Damian: 7916
Dmitry: 5380
Equalsk: 574
Method used

:

        Stopwatch stp = new Stopwatch();
        stp.Start();
        for (int i = 0; i < 1000000; i++)
        {
            sb = Test("_my_string_121_a_");
        }
        stp.Stop();
        long timeConsumed= stp.ElapsedMilliseconds;

In the end, I think I will go with the Raj implementation, because it is just very simple and easy to understand.

+4
6

ToTitleCase System.Globalization

static string toCamel(string input)
{        
    TextInfo info = CultureInfo.CurrentCulture.TextInfo;
    input= info.ToTitleCase(input).Replace("_", string.Empty);
    return input;
} 

enter image description here

+7

( ), , ( ):

  string source = "_my_string_123__A_";

  // MyString123A  
  string result = Regex
     // _ + lower case Letter -> upper case letter (thanks to Wiktor Stribiżew)
    .Replace(source, @"(_+|^)(\p{Ll})?", match => match.Groups[2].Value.ToUpper())
     // all the other _ should be just removed
    .Replace("_", "");
+3

.

public string GetNewString(string input)
{
    var convert = false;
    var sb = new StringBuilder();

    foreach (var c in input)
    {
        if (c == '_')
        {
            convert = true;
            continue;
        }

        if (convert)
        {
            sb.Append(char.ToUpper(c));
            convert = false;
            continue;
        }

        sb.Append(c);
    }

    return sb.ToString().First().ToString().ToUpper() + sb.ToString().Substring(1);
}

:

GetNewString("my_string");
GetNewString("___this_is_anewstring_");
GetNewString("___this_is_123new34tring_");

:

MyString
ThisIsAnewstring
ThisIs123new34tring

+3

:

var regex = new Regex("^[a-z]|_[a-z]?");
var result = regex.Replace("my_string_1234", x => x.Value== "_" ? "" : x.Value.Last().ToString().ToUpper());

my_string   -> MyString
_my_string  -> MyString
_my_string_ -> MyString
+2

, ,

  var x ="_my_string_".Split(new[] {"_"}, StringSplitOptions.RemoveEmptyEntries)
                .Select(s => char.ToUpperInvariant(s[0]) + s.Substring(1, s.Length - 1))
                .Aggregate(string.Empty, (s1, s2) => s1 + s2);

x = MyString

enter image description here

+1
    static string toCamel(string input)
    {
        StringBuilder sb = new StringBuilder();
        int i;
        for (i = 0; i < input.Length; i++)
        {
            if ((i == 0) || (i > 0 && input[i - 1] == '_'))
                sb.Append(char.ToUpper(input[i]));
            else
                sb.Append(char.ToLower(input[i]));
        }
        return sb.ToString();
    }
+1

Source: https://habr.com/ru/post/1664411/


All Articles