-
Notifications
You must be signed in to change notification settings - Fork 7
/
StringExtensions.cs
33 lines (31 loc) · 1.01 KB
/
StringExtensions.cs
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
using System;
using System.Globalization;
namespace Platform.Collections
{
public static class StringExtensions
{
public static string CapitalizeFirstLetter(this string str)
{
if (string.IsNullOrWhiteSpace(str))
{
return str;
}
var chars = str.ToCharArray();
for (var i = 0; i < chars.Length; i++)
{
var category = char.GetUnicodeCategory(chars[i]);
if (category == UnicodeCategory.UppercaseLetter)
{
return str;
}
if (category == UnicodeCategory.LowercaseLetter)
{
chars[i] = char.ToUpper(chars[i]);
return new string(chars);
}
}
return str;
}
public static string Truncate(this string str, int maxLength) => string.IsNullOrEmpty(str) ? str : str.Substring(0, Math.Min(str.Length, maxLength));
}
}