Compare string in c# for spaces in middle -
i want compare string in c# username duplicate.but problem how can compare strings having symbol , spaces in them.for example
one username : ak-username
second username : ak - username
first without space , second space in middle along symbol.
i cannot remove spaces compare user can [ak username]
you can split string , 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: saw comments below saying want "ak-username" , "ak - username" equal. refactoring code , writing 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"); }
Comments
Post a Comment