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

33 lines
1.2 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();
}
}