Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi Friends

how to compare 2 string 1 byte and 2 byte?
Ex: I have 2 string : str1(2 byte)= "MBS"
str2(1 byte)= "MBS"

how to command str1.CompareTo(str2) return value 0 (2 string are the same)?


Thanh you very much!
Posted
Comments
Varun Sareen 20-Feb-12 6:57am    
Please tell us the complete scenario

(if I got you) Use String.Replace method to remove unwanted blanks (or whatever) from str1.
 
Share this answer
 
Dear Friend,

This will do:-

string.Compare(s1, s2, CultureInfo.CurrentCulture, CompareOptions.IgnoreWidth)


Please refer this link for more details:-

http://stackoverflow.com/questions/4142394/double-byte-string-comparison-in-c-sharp[^]

Thanks
 
Share this answer
 
Comments
ngthtra 20-Feb-12 20:56pm    
So if I want to find a string into other,What command I have to use to ignore character width(1 byte or 2 byte)?
I would start having a read of this MSDN article and looking at the examples.

MSDN String.CompareTo[^]

this shows that the ordinal position of the two string start in the same place

this is taken from the article

"
Remarks

The CompareTo method was designed primarily for use in sorting or alphabetizing operations. It should not be used when the primary purpose of the method call is to determine whether two strings are equivalent. To determine whether two strings are equivalent, call the Equals method.
"

So if you are after comparing the actual values look at using the Equals[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900