473,387 Members | 2,436 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Error in string comparison (Non-English windows)

Hi

I'm having a strange error while comparing two strings. Please check the
code below. This is a simple string comparison code and works just fine on
all of my machines. While debugging an issue on a client's machine, who had
turkish windows installed on his system, I found out that this simple piece
of code does'nt work. The messages boxes that are displayed are in this
sequence.

1. to upper works with szWINDOWS
2. to lower does'nt works with szWINDOWS
3. to upper does'nt works with szwindows
4. to lower works with szwindows

It seems like ToUpper and ToLower are'nt working at all and .Equals() method
is being passed the original values of the variables szWINDOWS and
szwindows. Does this problem have anything to do with the Turkish window
that is installed on the client's machine, or is it a known issue.

string szWINDOWS = "WINDOWS";
string szwindows = "windows";

if(szWINDOWS.ToUpper().Equals ("WINDOWS") )
System.Windows.Forms.MessageBox.Show("to upper works with
szWINDOWS");
else
System.Windows.Forms.MessageBox.Show("to upper does'nt works with
szWINDOWS");

if(szWINDOWS.ToLower().Equals ("windows"))
System.Windows.Forms.MessageBox.Show("to lower works with
szWINDOWS");
else
System.Windows.Forms.MessageBox.Show("to lower does'nt works with
szWINDOWS");

if(szwindows.ToUpper().Equals ("WINDOWS"))
System.Windows.Forms.MessageBox.Show("to upper works with
szwindows");
else
System.Windows.Forms.MessageBox.Show("to upper does'nt works with
szwindows");

if(szwindows.ToLower().Equals ("windows"))
System.Windows.Forms.MessageBox.Show("to lower works with
szwindows");
else
System.Windows.Forms.MessageBox.Show("to lower does'nt works with
szwindows");

Regards

Usman
Dec 20 '06 #1
9 3726
Jon Skeet posted something about this a few days ago. The implication
that it was non-obvious, but deliberate (also included is correct
approach):

http://groups.google.co.uk/group/mic...8859549b4ed346

Marc
Dec 20 '06 #2
Thanx Marc.

It has been a great help. I've been debugging my whole project since 48
hours, and cud'nt get any idea why applicaiton is creating problems. I'll
surely look into the alternatives.

Regards
Usman
"Marc Gravell" <ma**********@gmail.comwrote in message
news:#A**************@TK2MSFTNGP06.phx.gbl...
Or: http://tinyurl.com/y3o9dz

;-p


Dec 20 '06 #4
Hmmm... just looking at Jon's sample again, and I'm damned if I can
get it to successfuly equate... following all also report false /
non-zero:
Console.WriteLine("mail".ToUpper() == "MAIL");
Console.WriteLine("mail".ToUpper() == "MAIL".ToUpper());
Console.WriteLine(StringComparer.CurrentCultureIgn oreCase.Equals("mail",
"MAIL"));
Console.WriteLine(StringComparer.CurrentCultureIgn oreCase.Compare("mail",
"MAIL"));
Console.WriteLine(string.Equals("mail", "MAIL",
StringComparison.CurrentCultureIgnoreCase));
Console.WriteLine("mail".Equals("MAIL",
StringComparison.CurrentCultureIgnoreCase));

Of course, this is Jon's test case, not yours - so your specific
culture and phrase may be more forgiving... but I don't think I know
about internationalization to give the complete answer... I'll add it
to my list of things to brush up on...

So : does anybody know how you *should* realistically compare such?

Marc
Dec 20 '06 #5
Hi

The problem just made me think that do I need to check my C++ code also for
this, or is this problem related to dotnet only. In C++ I've used stricmp()
at most of the places for case-insensitive comparison but at a few places
I've used my custom ToUpperCase and ToLowerCase functions. Just pasting one
of them, if you have any idea of it. Just curious to know, if I may have
problem here too, otherwise ignore it if its not relavant.

Thanks and Regards

Usman

string ToLowerCase(string szSourceString)
{
for(int nIndex = 0; nIndex < szSourceString.length(); nIndex++)
{
char cSingleChar = szSourceString[nIndex];
if( cSingleChar >= 'A' && cSingleChar <= 'Z')
{
szSourceString[nIndex] = cSingleChar + 32;
}
}
return szSourceString;
}

"Marc Gravell" <ma**********@gmail.comwrote in message
news:#A**************@TK2MSFTNGP06.phx.gbl...
Or: http://tinyurl.com/y3o9dz

;-p


Dec 20 '06 #6
JR
In Turkish there are two I's - with and without the dot above. The lower
case of I is ? (Dotless i), and the uppercase of i is ?.

JR
"Usman Jamil" <us***@advcomm.netwrote in message
news:O7**************@TK2MSFTNGP03.phx.gbl...
Hi

The problem just made me think that do I need to check my C++ code also
for
this, or is this problem related to dotnet only. In C++ I've used
stricmp()
at most of the places for case-insensitive comparison but at a few places
I've used my custom ToUpperCase and ToLowerCase functions. Just pasting
one
of them, if you have any idea of it. Just curious to know, if I may have
problem here too, otherwise ignore it if its not relavant.

Thanks and Regards

Usman

string ToLowerCase(string szSourceString)
{
for(int nIndex = 0; nIndex < szSourceString.length(); nIndex++)
{
char cSingleChar = szSourceString[nIndex];
if( cSingleChar >= 'A' && cSingleChar <= 'Z')
{
szSourceString[nIndex] = cSingleChar + 32;
}
}
return szSourceString;
}

"Marc Gravell" <ma**********@gmail.comwrote in message
news:#A**************@TK2MSFTNGP06.phx.gbl...
>Or: http://tinyurl.com/y3o9dz

;-p



Dec 20 '06 #7
JR
I'll try again, hoping the get through with UTF-8 and HTML:

In Turkish there are two I's - with and without the dot above. The lower
case of I is ı (Dotless i, U+0131), and the uppercase of i is İ (U+0130).

JR

"JR" <No****@qsm.co.ilwrote in message news:uk**************@TK2MSFTNGP06.phx.gbl...
In Turkish there are two I's - with and without the dot above. The lower
case of I is ? (Dotless i), and the uppercase of i is ?.

JR
"Usman Jamil" <us***@advcomm.netwrote in message
news:O7**************@TK2MSFTNGP03.phx.gbl...
>Hi

The problem just made me think that do I need to check my C++ code also
for
this, or is this problem related to dotnet only. In C++ I've used
stricmp()
at most of the places for case-insensitive comparison but at a few places
I've used my custom ToUpperCase and ToLowerCase functions. Just pasting
one
of them, if you have any idea of it. Just curious to know, if I may have
problem here too, otherwise ignore it if its not relavant.

Thanks and Regards

Usman

string ToLowerCase(string szSourceString)
{
for(int nIndex = 0; nIndex < szSourceString.length(); nIndex++)
{
char cSingleChar = szSourceString[nIndex];
if( cSingleChar >= 'A' && cSingleChar <= 'Z')
{
szSourceString[nIndex] = cSingleChar + 32;
}
}
return szSourceString;
}

"Marc Gravell" <ma**********@gmail.comwrote in message
news:#A**************@TK2MSFTNGP06.phx.gbl...
>>Or: http://tinyurl.com/y3o9dz

;-p


Dec 20 '06 #8
string ToLowerCase(string szSourceString)
{
for(int nIndex = 0; nIndex < szSourceString.length(); nIndex++)
{
char cSingleChar = szSourceString[nIndex];
if( cSingleChar >= 'A' && cSingleChar <= 'Z')
{
szSourceString[nIndex] = cSingleChar + 32;
}
}
return szSourceString;
}
Wrong for almost everything beyond plain ASCII.
Meaning it will be wrong for pretty much every language,
including English (thing résumé)
--
Mihai Nita [Microsoft MVP, Windows - SDK]
http://www.mihai-nita.net
------------------------------------------
Replace _year_ with _ to get the real email
Dec 21 '06 #9
Hmmm... just looking at Jon's sample again, and I'm damned if I can
get it to successfuly equate... following all also report false /
non-zero:
Console.WriteLine("mail".ToUpper() == "MAIL");
Console.WriteLine("mail".ToUpper() == "MAIL".ToUpper());
Console.WriteLine(StringComparer.CurrentCultureIgn oreCase.Equals("mail",
"MAIL"));
Console.WriteLine(StringComparer.CurrentCultureIgn oreCase.Compare("mail",
"MAIL"));
Console.WriteLine(string.Equals("mail", "MAIL",
StringComparison.CurrentCultureIgnoreCase));
Console.WriteLine("mail".Equals("MAIL",
StringComparison.CurrentCultureIgnoreCase));

Of course, this is Jon's test case, not yours - so your specific
culture and phrase may be more forgiving... but I don't think I know
about internationalization to give the complete answer... I'll add it
to my list of things to brush up on...

So : does anybody know how you *should* realistically compare such?

For all the examples above, as well as for the initial case (the "Windows"
string) the CurrentCulture is the most important factor.

For English:
U+0069 <-U+0049
For Turkish/Azeri
U+0069 <-U+0130
U+0131 <-U+0049

So, for Turkish/Azeri "MAIL" is really NOT ToUpper("mail")
This is how it is, and this is how it *should* be.
Now, sometimes you might need to compare things in a locale-independent way
(ie for file system, comunication protocols (ex: mailto:....), etc.)

The right thing for file system is to try accessing the file
( with _access (_taccess), or PathFileExists, or CreateFile)

For other things use StringComparer.InvariantCultureIgnoreCase( ... )
or String.ToUpperInvariant + String.CompareOrdinal
--
Mihai Nita [Microsoft MVP, Windows - SDK]
http://www.mihai-nita.net
------------------------------------------
Replace _year_ with _ to get the real email
Dec 21 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Elie B. | last post by:
Hi, I'm new to Python. I'm trying to embbed Python in my Windows application having some success with redirecting the stdin/out to my windows application using: In my C++ code I use...
0
by: ramin | last post by:
Hi, I would be greately thankful if somebody can give me some information about how string comparison is implemented in mysql. (Both in Physical and Application layer) How queries on string (for...
26
by: junky_fellow | last post by:
Consider the following piece of code: char *str = "Hello"; if (str = "Hello") printf("\nstring matches\n"); str is pointer to char and "Hello" is a string literal whose type is "array of...
5
by: MaSTeR | last post by:
Can anyone provide a practical short example of why in C# I shouldn't compare two strings with == ? If I write this in JAVA String string1 = "Widget"; if (string1 == "Widget") ...
4
by: Peter Kirk | last post by:
Hi I am looking at some code which in many places performs string comparison using == instead of Equals. Am I right in assuming that this will in fact work "as expected" when it is strings...
1
by: bjjnova | last post by:
I have the following string comparison that is throwing an error I cannot find ( I will include my attempts to trace the error) In the line following the asterisks, written as I have below, a...
14
by: Steve Bergman | last post by:
I'm looking for a module to do fuzzy comparison of strings. I have 2 item master files which are supposed to be identical, but they have thousands of records where the item numbers don't match in...
3
by: itmfl | last post by:
We are writing a program that multiplies two matrices of size n x m and m x n together. The matrices are stored in a file. The user provides the filename in the command line prompt. The file is...
10
by: lilly07 | last post by:
Hi, I have one column of strings in 1st file file and another file which consists of 5 clumns in each line and my basic objective is to find each item/line of 1st file is available in 3rd column of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.