Hello friends, I have a small problem and I need your help/hint...

I want to compare two strings with ignoring cases but my program not works as I want...

The program should compare if two strings contains same characters ignoring lower/upper and their order...

For example, how should works my program:

string x, y;
x = "daniweb";
y = "bewinad";
// true

x = "daniweb";
y = "DaNiWeB";
// true

x = "daniweb";
y = "daniwebd";
// false

I tried with:

string.Compare(x, y, true)
// and
x.Equals(y, StringComparison.OrdinalIgnoreCase)

// But I got wrong result...
bool b = "HoWdY".Equals("howdy", StringComparison.OrdinalIgnoreCase);
// I got true

bool b = "HoWdY".Equals("yhowd", StringComparison.OrdinalIgnoreCase);
// I got false (should be true)

Someone to help me ?
Thanks

Try the following:

private void TestStrings(string p, string p_2)
        {
            // Change the case of the two strings to be the same, either upper or lower
            p = p.ToLower();
            p_2 = p_2.ToLower();

            // Convert string to char array
            char[] charArrayP = p.ToCharArray();
            char[] charArrayP2 = p_2.ToCharArray();

            // Sort the chars in the array
            Array.Sort(charArrayP);
            Array.Sort(charArrayP2);

            // Store the char arrays back as strings
            p = new string(charArrayP);
            p_2 = new string(charArrayP2);

            // Test the strings for equality
            if (p.Equals(p_2))
            {
                MessageBox.Show("MATCH!!!");
            }

            
        }

...or combine it all with Linq

using System;
using System.Collections.Generic;
using System.Linq;

namespace DW_399345
{
   class Program
   {
      private static string LowerOrderedString(string a)
      {
         return new string(a.ToLower().OrderBy(s => s).ToArray());
      }

      public static bool ContentMatch(string a, string b)
      {
         return LowerOrderedString(a).Equals(LowerOrderedString(b));
      }

      static void Main(string[] args)
      {
         new List<string> { "bewinad", "DaNiWeB", "daniwebx" }
            .ForEach(s => Console.WriteLine(ContentMatch("daniweb", s)));
      }
   }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.