473,659 Members | 2,680 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.To Upper().Equals ("WINDOWS") )
System.Windows. Forms.MessageBo x.Show("to upper works with
szWINDOWS");
else
System.Windows. Forms.MessageBo x.Show("to upper does'nt works with
szWINDOWS");

if(szWINDOWS.To Lower().Equals ("windows"))
System.Windows. Forms.MessageBo x.Show("to lower works with
szWINDOWS");
else
System.Windows. Forms.MessageBo x.Show("to lower does'nt works with
szWINDOWS");

if(szwindows.To Upper().Equals ("WINDOWS"))
System.Windows. Forms.MessageBo x.Show("to upper works with
szwindows");
else
System.Windows. Forms.MessageBo x.Show("to upper does'nt works with
szwindows");

if(szwindows.To Lower().Equals ("windows"))
System.Windows. Forms.MessageBo x.Show("to lower works with
szwindows");
else
System.Windows. Forms.MessageBo x.Show("to lower does'nt works with
szwindows");

Regards

Usman
Dec 20 '06 #1
9 3758
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**********@g mail.comwrote in message
news:#A******** ******@TK2MSFTN GP06.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.WriteLi ne("mail".ToUpp er() == "MAIL");
Console.WriteLi ne("mail".ToUpp er() == "MAIL".ToUpper( ));
Console.WriteLi ne(StringCompar er.CurrentCultu reIgnoreCase.Eq uals("mail",
"MAIL"));
Console.WriteLi ne(StringCompar er.CurrentCultu reIgnoreCase.Co mpare("mail",
"MAIL"));
Console.WriteLi ne(string.Equal s("mail", "MAIL",
StringCompariso n.CurrentCultur eIgnoreCase));
Console.WriteLi ne("mail".Equal s("MAIL",
StringCompariso n.CurrentCultur eIgnoreCase));

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 internationaliz ation 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(str ing 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**********@g mail.comwrote in message
news:#A******** ******@TK2MSFTN GP06.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******** ******@TK2MSFTN GP03.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(str ing 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**********@g mail.comwrote in message
news:#A******** ******@TK2MSFTN GP06.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******** ******@TK2MSFTN GP06.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******** ******@TK2MSFTN GP03.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(str ing 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**********@g mail.comwrote in message
news:#A******* *******@TK2MSFT NGP06.phx.gbl.. .
>>Or: http://tinyurl.com/y3o9dz

;-p


Dec 20 '06 #8
string ToLowerCase(str ing 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.WriteLi ne("mail".ToUpp er() == "MAIL");
Console.WriteLi ne("mail".ToUpp er() == "MAIL".ToUpper( ));
Console.WriteLi ne(StringCompar er.CurrentCultu reIgnoreCase.Eq uals("mail",
"MAIL"));
Console.WriteLi ne(StringCompar er.CurrentCultu reIgnoreCase.Co mpare("mail",
"MAIL"));
Console.WriteLi ne(string.Equal s("mail", "MAIL",
StringCompariso n.CurrentCultur eIgnoreCase));
Console.WriteLi ne("mail".Equal s("MAIL",
StringCompariso n.CurrentCultur eIgnoreCase));

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 internationaliz ation 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. InvariantCultur eIgnoreCase( ... )
or String.ToUpperI nvariant + String.CompareO rdinal
--
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
1757
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 PyRun_SimpleString to execute this code: import sys import mymodule class LOGwriter :
0
1487
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 example '>' or like or strcmp) are implemented. Any idea where can I find the open source for string comparison/ ordering/ implementation? Does it use the sme structre as strcmp in c? Thanks a lot, Ramin
26
563
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 char". How can we compare two different objects for equality ? Is some conversion is being done before that comparison ?
5
657
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") System.Console.WriteLine("String1 Equal.");
4
7177
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 involved? By "as expected" I mean that as long as the strings are instantiated using string literals, then == and Equals are the same. string s1 = "xxx";
1
1561
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 true is always returned and the printed msg appears that the 2 variables are not equal. This happens even when the print statement writes out, "Change Doc Types TASK and TASK are not equal". (So, even when I test each variable separately and it...
14
13476
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 various ways. One might include a '-' or have leading zeros, or have a single character missing, or a zero that is typed as a letter 'O'. That kind of thing. These tables currently reside in a mysql database. I was wondering if there is a...
3
4084
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 formatted like so: /beginning/ matrix1 3 2 1 6 2 4 3 5 matrix2
10
8531
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 2 nd file. And I tried the following logic. It might be bit round about way but as a beginner am trying as follows. The column in the 1st file is having data as example NS008_456_R0030_3008 The 2nd file data is as follows: + test ...
0
8427
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8330
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8850
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8746
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8523
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7355
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1975
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1737
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.