Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make my string compare not sensitive to (ignore) minor differences in white space?

I have some tests that check strings that are displayed to the user.

I don’t wish the test to fail to due to changes in the indentations or line breaks etc. So I am looking for something like a string compare that.

  • Ignore while space as the start of the string (easy use string.trim)
  • When any number of any type of white space that is next to each other matches in the same way as if it was a single space.

I could start to create a regex, but someone else may have a better solution hence this question.

like image 653
Ian Ringrose Avatar asked Jul 28 '11 12:07

Ian Ringrose


People also ask

How do you compare strings without case-sensitive?

strcmpi compares string1 and string2 without sensitivity to case. All alphabetic characters in the two arguments string1 and string2 are converted to lowercase before the comparison. The function operates on null-ended strings.

Are string comparisons case-sensitive?

operators differs from string comparison using the String. CompareTo and Compare(String, String) methods. They all perform a case-sensitive comparison.

What are the 3 ways to compare two string objects?

There are three ways to compare String in Java: By Using equals() Method. By Using == Operator. By compareTo() Method.


3 Answers

You can use CompareOptions:

String.Compare(a, b, CultureInfo.CurrentCulture, CompareOptions.IgnoreSymbols);

Extract from MSDN:

Indicates that the string comparison must ignore symbols, such as white-space characters, punctuation, currency symbols, the percent sign, mathematical symbols, the ampersand, and so on.

like image 55
PVitt Avatar answered Nov 11 '22 20:11

PVitt


Writing a custom compare would be tricky if you need it just to do this for whitespace. I would suggest using regex to normalize, i.e.

private static readonly Regex normalizeSpace =
        new Regex(@"\s+", RegexOptions.Compiled);
...
string s = normalizeSpace.Replace(input, " ");

Obviously normalize both operands and then test for equality as normal.

like image 38
Marc Gravell Avatar answered Nov 11 '22 18:11

Marc Gravell


You can also use the following custom function

        public static string ExceptChars(this string str, IEnumerable<char> toExclude)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < str.Length; i++)
            {
                char c = str[i];
                if (!toExclude.Contains(c))
                    sb.Append(c);
            }
            return sb.ToString();
        }

        public static bool SpaceInsenstiveComparision(this string stringa, string stringb)
        {
            return stringa.ExceptChars(new[] { ' ', '\t', '\n', '\r' }).Equals(stringb.ExceptChars(new[] { ' ', '\t', '\n', '\r' }));
        }

And then use it following way

"Te  st".SpaceInsenstiveComparision("Te st");
like image 32
Zain Ali Avatar answered Nov 11 '22 19:11

Zain Ali