本文整理汇总了C#中System.StringComparison.GetCorrespondingComparer方法的典型用法代码示例。如果您正苦于以下问题:C# StringComparison.GetCorrespondingComparer方法的具体用法?C# StringComparison.GetCorrespondingComparer怎么用?C# StringComparison.GetCorrespondingComparer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.StringComparison
的用法示例。
在下文中一共展示了StringComparison.GetCorrespondingComparer方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: LooseSelect
/// <summary>
/// Loose string comparison. Returns the best match using increasingly inaccurate comparisons.
/// Also makes sure there is a sole match at that level of accuracy.
///
/// Spaces in the select string are ignored.
///
/// The levels are:
/// <list>
/// <item>Perfect match (abcd in abcd)</item>
/// <item>Prefix match (ab in abcd)</item>
/// <item>Containing match (bc in abcd)</item>
/// <item>Matching ordered sequence of characters (bd in abcd)</item>
/// </list>
/// </summary>
public static string LooseSelect (
IEnumerable<string> options,
string find,
StringComparison sc)
{
find = find.Replace (" ", "");
var ec = sc.GetCorrespondingComparer ();
var matches = new List<string> ();
int bestQuality = 0;
foreach (var s in options) {
int quality = -1;
if (s.Equals (find, sc)) {
quality = 10;
}
else if (s.StartsWith (find, sc)) {
quality = 8;
}
else if (s.Contains (find, sc)) {
quality = 6;
}
else if (StringContainsSequence (s, find, sc)) {
quality = 3;
}
if (quality >= bestQuality) {
if (quality > bestQuality) {
bestQuality = quality;
matches.Clear ();
}
matches.Add (s);
}
}
if (matches.Count == 1) {
return matches[0];
}
if (matches.Count > 1) {
Console.WriteLine ("Identifier not unique: " + find);
}
else {
Console.WriteLine ("Could not find identifier: " + find);
}
return null;
}
示例2: LooseSelect
/// <summary>
/// Loose string comparison. Returns the best match using increasingly inaccurate comparisons.
/// Also makes sure there is a sole match at that level of accuracy.
///
/// Spaces in the select string are ignored.
///
/// The levels are:
/// <list>
/// <item>Perfect match (abcd in abcd)</item>
/// <item>Prefix match (ab in abcd)</item>
/// <item>Containing match (bc in abcd)</item>
/// <item>Matching ordered sequence of characters (bd in abcd)</item>
/// </list>
/// </summary>
public static string LooseSelect(this string select, IEnumerable<string> source, StringComparison sc)
{
select = select.Replace(" ", "");
StringComparer ec = sc.GetCorrespondingComparer();
List<string> matches = new List<string>();
int bestQuality = 0;
foreach (var s in source)
{
int quality = -1;
if (s.Equals(select, sc)) { quality = 10; }
else if (s.StartsWith(select, sc)) { quality = 8; }
else if (s.Contains(select, sc)) { quality = 6; }
else if (StringContainsSequence(s, select)) { quality = 3; }
if (quality >= bestQuality)
{
if (quality > bestQuality)
{
bestQuality = quality;
matches.Clear();
}
matches.Add(s);
}
}
if (matches.Count == 1)
{
return matches[0];
}
//if (matches.Count > 1)
//{
// Console.WriteLine("Identifier not unique: " + select);
//}
//else
//{
// Console.WriteLine("Could not find identifier: " + select);
//}
return null;
}