AyCode.Core/AyCode.Utils/Extensions/StringExtensions.cs

41 lines
1.2 KiB
C#

using System.Diagnostics.CodeAnalysis;
using JetBrains.Annotations;
namespace AyCode.Utils.Extensions
{
public static class StringExtensions
{
//[ContractAnnotation("str:null => true; str:notnull <= false")]
[ContractAnnotation("str:null => true; str:notnull <= false")]
public static bool IsNullOrEmpty([NotNullWhen(returnValue: false)] this string str) => str == null || str.Length == 0;
[ContractAnnotation("str:null => true;")]
public static bool IsNullOrWhiteSpace([NotNullWhen(returnValue: false)] this string str)
{
if (str == null || str.Length == 0) return true;
if (!char.IsWhiteSpace(str[0])) return false;
for (var i = 1; i < str.Length; i++)
{
if (!char.IsWhiteSpace(str[i]))
return false;
}
return true;
}
public static string FirstLetterToUpper(string str)
{
if (str.IsNullOrWhiteSpace())
return str;
if (str.Length == 1)
return str.ToUpper();
str = str.ToLower();
return char.ToUpper(str[0]) + str[1..];
}
}
}