StringHelper.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. namespace MediaBrowser.Model.Extensions
  2. {
  3. /// <summary>
  4. /// Helper methods for manipulating strings.
  5. /// </summary>
  6. public static class StringHelper
  7. {
  8. /// <summary>
  9. /// Returns the string with the first character as uppercase.
  10. /// </summary>
  11. /// <param name="str">The input string.</param>
  12. /// <returns>The string with the first character as uppercase.</returns>
  13. public static string FirstToUpper(string str)
  14. {
  15. if (str.Length == 0)
  16. {
  17. return str;
  18. }
  19. if (char.IsUpper(str[0]))
  20. {
  21. return str;
  22. }
  23. #if NETSTANDARD2_0
  24. char[] a = str.ToCharArray();
  25. a[0] = char.ToUpperInvariant(a[0]);
  26. return new string(a);
  27. #else
  28. return string.Create(
  29. str.Length,
  30. str,
  31. (chars, buf) =>
  32. {
  33. chars[0] = char.ToUpperInvariant(buf[0]);
  34. for (int i = 1; i < chars.Length; i++)
  35. {
  36. chars[i] = buf[i];
  37. }
  38. });
  39. #endif
  40. }
  41. }
  42. }