改变方法使他们的搜索变得不敏感

时间:2017-03-08 21:38:30

标签: c# csv dictionary string-comparison keyvaluepair

我需要修复两种方法,以便当用户搜索“Javascript”时,如果拼写为“Javascript”或类似“JavaScript”,则方法将返回值 - 大小写无关紧要。已经使用LoadData()从csv文件导入数据(方法执行搜索的位置)。我可以找到我正在使用的三个文件(JobData.cs,Program.cs,job_data.csv)的完整代码here

我需要解决的第一个方法就是这个方法(found in JobData.cs, line 143)

 public static List<Dictionary<string, string>> FindByValue(string searchTerm)
    {
        LoadData();
        //set up a list of jobs that we're going to use to return from this method
        List<Dictionary<string, string>> jobs = new List<Dictionary<string, string>>();
        //row is a Dictionary<string, string>
        foreach (Dictionary<string, string> row in AllJobs)
        {
            //item is a KeyValuePair
            foreach (KeyValuePair<string, string> field in row)
            {
                string aValue = field.Value;
                if (aValue.Contains(searchTerm))
                {
                    jobs.Add(row);
                    break;
                }
            }
        }
        return jobs;
    }  

也许一种方法是将searchTermvalue分解,以便在用户搜索时自动变为小写。这样,即使用户键入JAVAScript,它也会自动转换为javascript,它将匹配字符串field中的字符,这些字符也将变为小写。当然,我们仍会在字段中返回原始字符串,无论是“Javascript”还是“JavaScript”。

另一种方法是自动转换searchTerm不区分大小写,以便它与field.Value匹配,而不管大小写。

这样做会是这样的吗?

  public static List<Dictionary<string, string>> FindByValue(string searchTerm)
    {
        LoadData();
        //set up a list of jobs that we're going to use to return from this method
        List<Dictionary<string, string>> jobs = new List<Dictionary<string, string>>();
        //row is a Dictionary<string, string>
        foreach (Dictionary<string, string> row in AllJobs)
        {
            //item is a KeyValuePair
            foreach (KeyValuePair<string, string> field in row)
            {
                string aValue = field.Value;
                //create new, case-insensitive searchTerm

                culture.CompareInfo.IndexOf(searchTerm, aValue, CompareOptions.IgnoreCase) >= 0

                if (aValue.Contains(searchTerm))
                {
                    jobs.Add(row);
                    break;
                }
            }
        }
        return jobs;
    }   

我正在尝试使用this example不区分大小写的字符串比较。但是使用该行会给出错误消息:

The name "culture" does not exist in the current context
The name "CompareOptions" does not exist in the current context

searchTerms进行比较后,如何解决field.aValue不区分大小写的其他任何想法?

3 个答案:

答案 0 :(得分:1)

让我们首先说你不能比较字符串,不要让它们区分大小写。它们总是因为大写和小写字母具有不同的Unicode值。这是您的解决方案:

如果要对每个字符串进行全面检查,可以使用ToUpper()ToLower()方法。例如:

string s1 = "JavaScript";
string s2 = "Javascript";

if (s1.ToLower() == s2.ToLower())
{
    //Do something
}

它与ToUpper()的作用相同,因为:

//s1.ToLower() == "javascript";
//s1.ToUpper() == "JAVASCRIPT";

因此,因为它与您的情况有关,假设您的Dictionary填充了所有小写字符串,您可以简单地说:

if (aValue.Contains(searchTerm.ToLower()))
{
    //Do something
}

有关这些功能的更多信息,请查看

https://msdn.microsoft.com/en-us/library/e78f86at(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/system.string.toupper(v=vs.110).aspx

答案 1 :(得分:1)

多年来我一直在玩这个问题,而我能够提出的最强大的解决方案是创建一个using StringOrdinalIgnoreCase = JDanielSmith.System.String<JDanielSmith.System.OrdinalIgnoreCase>; [TestMethod] public void StringOrdinalIgnoreCaseDictionary() { var d = new Dictionary<StringOrdinalIgnoreCase, int>() { { "abc", 1 }, { "def", 2 } }; Assert.IsTrue(d.ContainsKey("ABC")); try { d.Add("DEF", 2); Assert.Fail(); } catch (ArgumentException) { } } - 就像包裹一样不区分大小写的方式。虽然根据你的需要,这可能会过度杀人......

完整的解决方案,单元测试位于https://github.com/JDanielSmith/CaseInsenstiveString,但 cs 文件本身并不大,所以我也会在此处包含它。< / p>

用法(取自其中一个单元测试)

using System;

using CodeAnalysis = System.Diagnostics.CodeAnalysis;

namespace JDanielSmith.System
{
    /// <summary>
    /// Provide a case-insensitive wrapper around System.String.
    /// 
    /// This is especially useful when using strings as keys in collections, where the key is something like a Windows file-system pathname;
    /// it can be easy to forget to pass an IEqualityComparer<> in the constructor.
    /// 
    /// Some hints from: http://stackoverflow.com/questions/33039324/how-can-system-string-be-properly-wrapped-for-case-insensitivy
    /// </summary>
    [CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1716:IdentifiersShouldNotMatchKeywords", MessageId = "String")]
    [CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1036:OverrideMethodsOnComparableTypes")]
    public sealed class String<TComparerAndComparison> : IComparable, ICloneable,
        IComparable<String<TComparerAndComparison>>, IEquatable<String<TComparerAndComparison>>,
        IComparable<String>, IEquatable<String>
        where TComparerAndComparison : StringComparerAndComparison, new()
    {
        static readonly StringComparerAndComparison _comparerAndComparison = new TComparerAndComparison();
        static readonly StringComparer _comparer = _comparerAndComparison.Comparer;
        static readonly StringComparison _comparisonType = _comparerAndComparison.Comparison;

        public string Value { get; }

        public String(string value)
        {
            // matching the behavior of System.String is more straight-forward if "Value" is never null
            Value = value ?? String.Empty;
        }

        // easily convert to/from System.String
        public static implicit operator String<TComparerAndComparison>(string source) => new String<TComparerAndComparison>(source);
        public static implicit operator string(String<TComparerAndComparison> source) => source?.Value;

        #region Equals, IEquatable
        public override bool Equals(object obj)
        {
            if (Object.ReferenceEquals(obj, null))
                return false; // this != null

            var other = obj as String<TComparerAndComparison>;
            if (!Object.ReferenceEquals(other, null))
                return Equals(other); // call Equals(String<TStringComparerAndComparison>)

            var s_other = obj as string;
            if (!Object.ReferenceEquals(s_other, null))
                return Equals(s_other); // call Equals(string)

            return _comparer.Equals(obj);
        }
        public bool Equals(String<TComparerAndComparison> other)
        {
            if (Object.ReferenceEquals(other, null))
                return false; // this != null
            return Equals(other.Value); // call Equals(string)
        }
        public bool Equals(string other) => _comparer.Equals(Value, other);

        public override int GetHashCode()
        {
            return _comparer.GetHashCode(Value);
        }
        #endregion

        public override string ToString() => Value;

        public object Clone() => new String<TComparerAndComparison>(Value);

        #region IComparable
        public int CompareTo(object obj)
        {
            // https://msdn.microsoft.com/en-us/library/4d7sx9hd(v=vs.110).aspx
            if (Object.ReferenceEquals(obj, null))
                return 1; // If other is not a valid object reference, this instance is greater.

            // obj must be either StringOrdinalIgnoreCase or String
            var other = obj as String<TComparerAndComparison>;
            if (Object.ReferenceEquals(other, null))
            {
                var s_other = obj as string;
                if (Object.ReferenceEquals(s_other, null))
                    throw new ArgumentException("Object must be of type " + nameof(String<TComparerAndComparison>) + " or String.");

                return CompareTo(s_other); // call CompareTo(string)
            }

            return CompareTo(other); // call CompareTo(StringOrdinalIgnoreCase)
        }
        public int CompareTo(String<TComparerAndComparison> other)
        {
            // https://msdn.microsoft.com/en-us/library/4d7sx9hd(v=vs.110).aspx
            if (Object.ReferenceEquals(other, null))
                return 1; // If other is not a valid object reference, this instance is greater.

            if (Object.ReferenceEquals(Value, other.Value))
                return 0;

            return CompareTo(other.Value); // call CompareTo(string)
        }
        public int CompareTo(string other)
        {
            // https://msdn.microsoft.com/en-us/library/4d7sx9hd(v=vs.110).aspx
            if (Object.ReferenceEquals(other, null))
                return 1; // If other is not a valid object reference, this instance is greater.

            return _comparer.Compare(Value, other);
        }

        public static bool operator ==(String<TComparerAndComparison> x, String<TComparerAndComparison> y)
        {
            if (Object.ReferenceEquals(x, null))
                return Object.ReferenceEquals(y, null); // null == null, null != something
            return x.Equals(y); // know x != null
        }
        public static bool operator ==(String<TComparerAndComparison> x, string y)
        {
            if (Object.ReferenceEquals(x, null))
                return Object.ReferenceEquals(y, null); // null == null, null != something
            return x.Equals(y); // know x != null
        }
        public static bool operator ==(string x, String<TComparerAndComparison> y) => y == x; // == is commutative, x == y
        public static bool operator !=(String<TComparerAndComparison> x, String<TComparerAndComparison> y) => !(x == y);
        public static bool operator !=(string x, String<TComparerAndComparison> y) => !(x == y);
        public static bool operator !=(String<TComparerAndComparison> x, string y) => !(x == y);
        #endregion

        #region IndexOf, LastIndexOf, StartsWith, EndsWith
        public bool EndsWith(string value) => Value.EndsWith(value, _comparisonType);
        public int IndexOf(string value) => Value.IndexOf(value, _comparisonType);
        public int IndexOf(string value, int startIndex) => Value.IndexOf(value, startIndex, _comparisonType);
        public int IndexOf(string value, int startIndex, int count) => Value.IndexOf(value, startIndex, count, _comparisonType);
        public int LastIndexOf(string value) => Value.LastIndexOf(value, _comparisonType);
        public int LastIndexOf(string value, int startIndex) => Value.LastIndexOf(value, startIndex, _comparisonType);
        public int LastIndexOf(string value, int startIndex, int count) => Value.LastIndexOf(value, startIndex, count, _comparisonType);
        public bool StartsWith(string value) => Value.StartsWith(value, _comparisonType);
        #endregion

    }
}

String.cs

using System;

using StringComparer = System.StringComparer;
using StringComparison = System.StringComparison;

namespace JDanielSmith.System
{
    /// <summary>
    /// Pass around System.StringComparer and System.StringComparison together.
    /// Also, provides a base class for generics.
    /// </summary>
    public abstract class StringComparerAndComparison
    {
        internal StringComparer Comparer { get; }
        internal StringComparison Comparison { get; }
        internal StringComparerAndComparison(StringComparer comparer, StringComparison comparison)
        {
            if (comparer == null) throw new ArgumentNullException(nameof(comparer));

            Comparer = comparer;
            Comparison = comparison;
        }
    }

    public sealed class CurrentCulture : StringComparerAndComparison
    {
        public CurrentCulture() : base(StringComparer.CurrentCulture, StringComparison.CurrentCulture) { }
    }

    public sealed class CurrentCultureIgnoreCase : StringComparerAndComparison
    {
        public CurrentCultureIgnoreCase() : base(StringComparer.CurrentCultureIgnoreCase, StringComparison.CurrentCultureIgnoreCase) { }
    }

    public sealed class InvariantCulture : StringComparerAndComparison
    {
        public InvariantCulture() : base(StringComparer.InvariantCulture, StringComparison.InvariantCulture) { }
    }

    public sealed class InvariantCultureIgnoreCase : StringComparerAndComparison
    {
        public InvariantCultureIgnoreCase() : base(StringComparer.InvariantCultureIgnoreCase, StringComparison.InvariantCultureIgnoreCase) { }
    }

    public sealed class Ordinal : StringComparerAndComparison
    {
        public Ordinal() : base(StringComparer.Ordinal, StringComparison.Ordinal) { }
    }

    public sealed class OrdinalIgnoreCase : StringComparerAndComparison
    {
        public OrdinalIgnoreCase() : base(StringComparer.OrdinalIgnoreCase, StringComparison.OrdinalIgnoreCase) { }
    }
}

<强> StringComparerAndComparison.cs

{{1}}

答案 2 :(得分:1)

您使用IndexOf代替Contains的想法是正确的,您只需要使用正确的重载(the one that takes a StringComparison option)。对不区分大小写的比较有多种选择。我使用OrdinalIgnoreCase,但你可以使用最适合你的那个。

而不是:

aValue.Contains(searchTerm)

写下这个:

aValue.IndexOf(searchTerm, StringComparison.OrdinalIgnoreCase) >= 0