PKHeX/PKHeX.Core/Util/Util.cs

42 lines
1.6 KiB
C#
Raw Normal View History

2016-07-09 22:30:12 +00:00
using System;
using System.Linq;
namespace PKHeX.Core
2016-07-09 22:30:12 +00:00
{
public static partial class Util
2016-07-09 22:30:12 +00:00
{
public static int ToInt32(string value)
2016-07-09 22:30:12 +00:00
{
string val = value?.Replace(" ", "").Replace("_", "").Trim();
2016-07-22 05:45:20 +00:00
return string.IsNullOrWhiteSpace(val) ? 0 : int.Parse(val);
2016-07-09 22:30:12 +00:00
}
public static uint ToUInt32(string value)
2016-07-09 22:30:12 +00:00
{
string val = value?.Replace(" ", "").Replace("_", "").Trim();
2016-07-22 05:45:20 +00:00
return string.IsNullOrWhiteSpace(val) ? 0 : uint.Parse(val);
2016-07-09 22:30:12 +00:00
}
public static uint GetHexValue(string s)
2016-07-09 22:30:12 +00:00
{
string str = GetOnlyHex(s);
2016-07-22 05:45:20 +00:00
return string.IsNullOrWhiteSpace(str) ? 0 : Convert.ToUInt32(str, 16);
2016-07-09 22:30:12 +00:00
}
private static bool IsHex(char c) => (c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f');
private static string TitleCase(string word) => char.ToUpper(word[0]) + word.Substring(1, word.Length - 1).ToLower();
/// <summary>
/// Filters the string down to only valid hex characters, returning a new string.
/// </summary>
/// <param name="str">Input string to filter</param>
public static string GetOnlyHex(string str) => string.IsNullOrWhiteSpace(str) ? string.Empty : string.Concat(str.Where(IsHex));
/// <summary>
/// Returns a new string with each word converted to its appropriate title case.
/// </summary>
/// <param name="str">Input string to modify</param>
public static string ToTitleCase(string str) => string.IsNullOrWhiteSpace(str) ? string.Empty : string.Join(" ", str.Split(' ').Select(TitleCase));
2016-07-09 22:30:12 +00:00
}
}