0

I want to compare string in C# for username duplicate.But problem is how i can compare strings having symbol and spaces in them.For example

One username is : AK-Username second username is : AK - Username

first is without space and second is with space in middle along with symbol.

I cannot remove spaces to compare as user can be [AK username]

Nasreddine
  • 36,610
  • 17
  • 75
  • 94
Arun Kumar
  • 31
  • 1
  • 6
  • 1
    So you're saying that "AK username", "AK-Username" and "AK - Username" all have to compare as equal? – Jon Skeet May 28 '16 at 18:08
  • You cannot be certain 100% with any approach that two user names are the same, so you should set some resonable goal that you will detect 90% of duplicates. You may use Edit distance to compare username and say tread two names as same if the have same letters (when any other symbols are removed) and have edit distance <= 3 – csharpfolk May 28 '16 at 18:09
  • 1
    Also could you provide more info what exactly symbols are allowed in usernames? – csharpfolk May 28 '16 at 18:11
  • No AK Username and Ak-Username are different. But AK-Username and AK - Username are same. i am using EF to check username for duplicate. Not sure how to deal with AK-username and AK - username and may be AK - UserName – Arun Kumar May 28 '16 at 18:16
  • You could use [String.Replace](https://msdn.microsoft.com/en-us/library/system.string.replace%28v=vs.110%29.aspx) to remove the spaces before comparing. – Andrew Morton May 28 '16 at 18:18
  • Are you talking about [similarity](http://stackoverflow.com/questions/3576211/string-similarity-algorithms)? – Maciej Los May 28 '16 at 18:18
  • 1
    You should save yourself the headache of trying to figure this out. Usernames should be kept simple. I'm pretty sure you'll end up with a corner case where two users will end up with the same username. – Nasreddine May 28 '16 at 18:27
  • you are Right @Nasreddine – Jamshaid K. May 28 '16 at 19:03
  • @ArunKumar Or you could simply not care at all, why is it problem if there is 1 user named "AAA BBB" and another "AAABBB"? Sure it's similar but not much more than "AAA-BBB" and there is no technical reason to have that, if you have a functional reason (like similarity) you may want to go for something else entirely than manually checking for spaces as there are plenty plenty more edge cases (what about tabs? non visible characters etc etc?) – Ronan Thibaudau May 28 '16 at 19:09

2 Answers2

0

You can just split the string and rejoin it

string str="AK Username";
string str1 = "AKUsername";
string[] SplittedStrings = str.Split(" ");
str = "";

for(int i=0; i<SplittedStrings.Length; i++)
{
  str += SpittedStrings[i];
}

if(String.Equals(str,str1,StringComparison.OrdinalIgnoreCase))
{
  MessageBox.Show("Equal");
}

EDIT: I just saw the comments below saying that you want "AK-USername" and "AK - Username" to be equal. I am refactoring the code and writing it below!

string str="AK - Username";
string str1 = "AK-Username";
string[] SplittedStrings = str.Split("-");
str = "";

for(int i=0; i<SplittedStrings.Length; i++)
{
  str += SpittedStrings[i].Trim();
}

if(String.Equals(str,str1,StringComparison.OrdinalIgnoreCase))
{
  MessageBox.Show("Equal");
}
Jamshaid K.
  • 3,555
  • 1
  • 27
  • 42
0

here another approach:

It's not perfect though. You have the problem that it captures too many characters once a user takes - or spaces into his nickname. But maybe you can refine the regex a bit so it takes more care. Good Look with that, or filter spaces before the Where and and Replace with a Singe Point, not with *. but thats up to you.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            List<String> similarStrings = new List<String> {"abc", "ab-c", "hfcx-h", "hfcxh", "hf  --  cx --- h"};
            foreach (String tempString in similarStrings)
            {
                String foreachvar = tempString;

                Console.Write("Similiar Strings to " + tempString + " >>> ");
                foreachvar = System.Text.RegularExpressions.Regex.Replace(foreachvar, @"\s|-", @".*");
                List<String> filtered = similarStrings.Where<String>(item => !item.Equals(tempString) && System.Text.RegularExpressions.Regex.IsMatch(item, foreachvar)).ToList<String>();
                foreach (String filteredTemp in filtered)
                {
                    Console.Write(filteredTemp + " ");
                }
                Console.WriteLine("");
            }
            Console.ReadLine();
        }
    }
}
prizm1
  • 363
  • 1
  • 11