-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringExtensions.cs
More file actions
138 lines (128 loc) · 3.87 KB
/
StringExtensions.cs
File metadata and controls
138 lines (128 loc) · 3.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public static class StringExtensions
{
public static int LevenshteinDistance(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)
{
var temp = target;
target = source;
source = temp;
}
var m = target.Length;
var n = source.Length;
var distance = new int[2, m + 1];
for (var j = 1; j <= m; j++) distance[0, j] = j;
var currentRow = 0;
for (var i = 1; i <= n; ++i)
{
currentRow = i & 1;
distance[currentRow, 0] = i;
var previousRow = currentRow ^ 1;
for (var j = 1; j <= m; j++)
{
var cost = (target[j - 1] == source[i - 1] ? 0 : 1);
distance[currentRow, j] = Mathf.Min(Mathf.Min(
distance[previousRow, j] + 1,
distance[currentRow, j - 1] + 1),
distance[previousRow, j - 1] + cost);
}
}
return distance[currentRow, m];
}
public static string GetMostlyMatch(this string[] all, string compare)
{
List<int> matchList = new List<int>();
foreach (string Track in all)
{
int matchCount = 0;
for (int i = 0; i < compare.Length; i++)
{
if (Track.Length > i)
{
if (compare[i] == Track[i])
{
matchCount++;
}
else
{
break;
}
}
else
{
break;
}
}
matchList.Add(matchCount);
}
string match = all.ToList().ElementAt(matchList.IndexOf(matchList.Max()));
return match;
}
public static string[] GetAllWithPrefix(this string[] all, string compare)
{
List<int> matchList = new List<int>();
foreach (string Track in all)
{
int matchCount = 0;
for (int i = 0; i < compare.Length; i++)
{
if (Track.Length > i)
{
if (compare[i] == Track[i])
{
matchCount++;
}
else
{
break;
}
}
else
{
break;
}
}
matchList.Add(matchCount);
}
int maxMatchCount = int.MinValue;
for (int i = 0; i < matchList.Count; i++)
{
if (matchList[i] > maxMatchCount)
{
maxMatchCount = matchList[i];
}
}
var indexes = new List<int>();
for (int i = 0; i < matchList.Count; i++)
{
if (matchList[i] == maxMatchCount)
{
indexes.Add(i);
}
}
var result = new List<string>();
for (int i = 0; i < indexes.Count; i++)
{
string match = all[indexes[i]];
result.Add(match);
}
return result.ToArray();
}
public static string FirstCharToUpper(this string input) =>
input switch
{
null => throw new System.ArgumentNullException(nameof(input)),
"" => throw new System.ArgumentException($"{nameof(input)} cannot be empty", nameof(input)),
_ => input[0].ToString().ToUpper() + input.Substring(1)
};
}