aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Model/Extensions/StringHelper.cs
blob: 8ffa3c4ba6621559d2033f9f783a56189269a2d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
namespace MediaBrowser.Model.Extensions
{
    /// <summary>
    /// Helper methods for manipulating strings.
    /// </summary>
    public static class StringHelper
    {
        /// <summary>
        /// Returns the string with the first character as uppercase.
        /// </summary>
        /// <param name="str">The input string.</param>
        /// <returns>The string with the first character as uppercase.</returns>
        public static string FirstToUpper(string str)
        {
            if (str.Length == 0)
            {
                return str;
            }

            if (char.IsUpper(str[0]))
            {
                return str;
            }

#if NETSTANDARD2_0
            char[] a = str.ToCharArray();
            a[0] = char.ToUpperInvariant(a[0]);
            return new string(a);
#else
            return string.Create(
                str.Length,
                str,
                (chars, buf) =>
                {
                    chars[0] = char.ToUpperInvariant(buf[0]);
                    for (int i = 1; i < chars.Length; i++)
                    {
                        chars[i] = buf[i];
                    }
                });
#endif
        }
    }
}