70 lines
1.5 KiB
C#
70 lines
1.5 KiB
C#
using System;
|
|
using System.Globalization;
|
|
using System.Text.RegularExpressions;
|
|
using UnityEngine;
|
|
|
|
namespace BitStrap
|
|
{
|
|
public static class StringExtensions
|
|
{
|
|
public static DateTime ToDateTime(this string self)
|
|
{
|
|
try
|
|
{
|
|
return DateTime.ParseExact(self, "yyyy-MM-dd HH:mm:ss zzz", CultureInfo.InvariantCulture);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return DateTime.MinValue;
|
|
}
|
|
}
|
|
|
|
public static string SeparateCamelCase(this string self, string separator = " ")
|
|
{
|
|
return Regex.Replace(self, "(?<=[a-z])([A-Z])", separator + "$1").Trim();
|
|
}
|
|
|
|
public static int Distance(this string source, string target)
|
|
{
|
|
if (string.IsNullOrEmpty(source))
|
|
{
|
|
if (string.IsNullOrEmpty(target))
|
|
{
|
|
return 0;
|
|
}
|
|
return target.Length;
|
|
}
|
|
if (string.IsNullOrEmpty(target))
|
|
{
|
|
return source.Length;
|
|
}
|
|
if (source.Length > target.Length)
|
|
{
|
|
string text = target;
|
|
target = source;
|
|
source = text;
|
|
}
|
|
int length = target.Length;
|
|
int length2 = source.Length;
|
|
int[,] array = new int[2, length + 1];
|
|
for (int i = 1; i <= length; i++)
|
|
{
|
|
array[0, i] = i;
|
|
}
|
|
int num = 0;
|
|
for (int j = 1; j <= length2; j++)
|
|
{
|
|
num = j & 1;
|
|
array[num, 0] = j;
|
|
int num2 = num ^ 1;
|
|
for (int k = 1; k <= length; k++)
|
|
{
|
|
int num3 = ((target[k - 1] != source[j - 1]) ? 1 : 0);
|
|
array[num, k] = Mathf.Min(Mathf.Min(array[num2, k] + 1, array[num, k - 1] + 1), array[num2, k - 1] + num3);
|
|
}
|
|
}
|
|
return array[num, length];
|
|
}
|
|
}
|
|
}
|