当前位置: 首页>>代码示例>>C#>>正文


C# StringComparison.GetCorrespondingComparer方法代码示例

本文整理汇总了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;
		}
开发者ID:nerai,项目名称:nibbler,代码行数:61,代码来源:Util.cs

示例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;
        }
开发者ID:0x0mar,项目名称:Xploit,代码行数:56,代码来源:StringComparisonExtensions.cs


注:本文中的System.StringComparison.GetCorrespondingComparer方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。