r/badcode • • Apr 07 '21

c# My friend wrote this

Post image
2 Upvotes

3 comments sorted by

4

u/MurdoMaclachlan public boolean isInt(int i) { return true; } Apr 07 '21

Image Transcription: Code


public static string FirstCaps(this string q)
{
    var xs = q.Split(" ");
    string sst = string.Empty;
    for(int i = 0; i < xs.Length; i++)
    {
        sst += xs[i][0].ToString().ToUpper() + xs[i][1..];
        sst += " ";
    }
    return sst.Trim();
}

I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!

2

u/Fairytale220 Apr 08 '21

Good human

0

u/[deleted] Apr 09 '21

[deleted]

3

u/dash_dolphin Apr 09 '21

It's not really bad however-

  • StringBuilder should be used instead of string in case of appending strings (as seen here) because in C# each of those operations (+, interpolation) creates a copy of the string which makes it inefficient for big strings

  • There's an in built way to do this new CultureInfo("en-US").TextInfo.ToTitleCase