Files

39 lines
1.1 KiB
C#
Raw Permalink Normal View History

2025-12-31 09:26:31 -05:00
using System;
2018-12-27 18:27:57 -05:00
namespace MediaBrowser.Model.Extensions
{
/// <summary>
2020-01-09 17:07:13 +01:00
/// Helper methods for manipulating strings.
2018-12-27 18:27:57 -05:00
/// </summary>
public static class StringHelper
{
/// <summary>
2020-01-16 23:22:42 +01:00
/// Returns the string with the first character as uppercase.
2018-12-27 18:27:57 -05:00
/// </summary>
2020-01-09 17:07:13 +01:00
/// <param name="str">The input string.</param>
2020-01-16 23:23:06 +01:00
/// <returns>The string with the first character as uppercase.</returns>
2020-01-09 17:07:13 +01:00
public static string FirstToUpper(string str)
2018-12-27 18:27:57 -05:00
{
if (str.Length == 0)
2018-12-27 18:27:57 -05:00
{
return str;
2018-12-27 18:27:57 -05:00
}
2021-06-06 19:30:43 +02:00
// We check IsLower instead of IsUpper because both return false for non-letters
2021-06-05 13:32:22 +02:00
if (!char.IsLower(str[0]))
2020-01-09 17:07:13 +01:00
{
return str;
}
2018-12-27 18:27:57 -05:00
2020-01-09 17:07:13 +01:00
return string.Create(
str.Length,
2025-12-31 09:26:31 -05:00
str.AsSpan(),
2020-01-09 17:07:13 +01:00
(chars, buf) =>
{
chars[0] = char.ToUpperInvariant(buf[0]);
2025-12-31 09:26:31 -05:00
buf.Slice(1).CopyTo(chars.Slice(1));
2020-01-09 17:07:13 +01:00
});
2018-12-27 18:27:57 -05:00
}
}
}