unity-atoms/Packages/Core/Editor/Extensions/StringExtensions.cs

48 lines
1.7 KiB
C#
Raw Normal View History

using System;
using System.Text;
2019-10-01 11:27:22 -04:00
namespace UnityAtoms
{
2019-10-15 13:19:44 -04:00
/// <summary>
/// Internal extension class for strings.
/// </summary>
internal static class StringExtensions
{
2019-10-15 13:19:44 -04:00
/// <summary>
/// Tries to parse a string to an int.
/// </summary>
/// <param name="str">The string to parse.</param>
/// <param name="def">The default value if not able to parse the provided string.</param>
/// <returns>Returns the string parsed to an int. If not able to parse the string, it returns the default value provided to the method.</returns>
public static int ToInt(this string str, int def)
{
int num;
return int.TryParse(str, out num) ? num : def;
}
2019-10-15 13:19:44 -04:00
/// <summary>
/// Repeats the string X amount of times.
/// </summary>
/// <param name="str">The string to repeat.</param>
/// <param name="times">The number of times to repeat the provided string.</param>
/// <returns>The string repeated X amount of times.</returns>
public static string Repeat(this string str, int times)
=> times == 1 ? str : new StringBuilder(str.Length * times).Insert(0, str, times).ToString();
2020-03-01 15:32:52 -05:00
/// <summary>
/// Capitalize the provided string.
/// </summary>
/// <param name="str">The string to capitalize.</param>
/// <returns>A capitalized version of the string provided.</returns>
public static string Capitalize(this string str)
{
if (string.IsNullOrEmpty(str))
return string.Empty;
char[] a = str.ToCharArray();
a[0] = char.ToUpper(a[0]);
return new string(a);
}
}
}