473,508 Members | 2,091 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Case Insensitive string comparison

Is this a long way to compare strings ignoring the case?
if ( string.Compare(wsUri.Host, "localHost",
System.StringComparison.OrdinalIgnoreCase) == 0 )
{
// etc.
}

Mar 2 '07 #1
6 11283
<an******@hotmail.comwrote:
Is this a long way to compare strings ignoring the case?
if ( string.Compare(wsUri.Host, "localHost",
System.StringComparison.OrdinalIgnoreCase) == 0 )
{
// etc.
}
Well, it's easier to use

if ("localHost".Equals(wsUri.Host, StringComparison.OrdinalIgnoreCase))

(If you know that wsUri.Host is non-null, you can swap what the method
is called on for clarity.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 2 '07 #2
Well it's long, but just as number of characters.
A shorter version could be string.Compare(wsUri.Host, "localHost",true)==0
It still takes three parameters.

<an******@hotmail.comha scritto nel messaggio
news:11**********************@h3g2000cwc.googlegro ups.com...
Is this a long way to compare strings ignoring the case?
if ( string.Compare(wsUri.Host, "localHost",
System.StringComparison.OrdinalIgnoreCase) == 0 )
{
// etc.
}

Mar 2 '07 #3
On Mar 2, 1:11 pm, "Laura T." <L...@NOWHERE.COMwrote:
Well it's long, but just as number of characters.
A shorter version could be string.Compare(wsUri.Host, "localHost",true)==0
It still takes three parameters.

<anoni...@hotmail.comha scritto nel messaggionews:11**********************@h3g2000cwc. googlegroups.com...
Is this a long way to compare strings ignoring the case?
if ( string.Compare(wsUri.Host, "localHost",
System.StringComparison.OrdinalIgnoreCase) == 0 )
{
// etc.
}- Hide quoted text -

- Show quoted text -
wsUri.Host.ToString().ToUpper().Equals("LOCALHOST" ) would be much
simpler i think

-
shashank kadge

Mar 6 '07 #4
On Mar 6, 3:40 pm, "shashank kadge" <mailtosha...@gmail.comwrote:
>
wsUri.Host.ToString().ToUpper().Equals("LOCALHOST" ) would be much
simpler i think
It's not guaranteed to work though, due to potential culture issues.

I ran into a problem years ago where I was trying to compare strings
in a case-insensitive way by doing the above - and we found that in
Turkey, we had a problem, because the upper case of "i" isn't "I" in
Turkey (and vice versa).

Using a case-insensitive comparison is the best way of working.

Jon

Mar 6 '07 #5
On Mar 6, 11:49 am, "Jon Skeet [C# MVP]" <s...@pobox.comwrote:
On Mar 6, 3:40 pm, "shashank kadge" <mailtosha...@gmail.comwrote:
wsUri.Host.ToString().ToUpper().Equals("LOCALHOST" ) would be much
simpler i think

It's not guaranteed to work though, due to potential culture issues.

I ran into a problem years ago where I was trying to compare strings
in a case-insensitive way by doing the above - and we found that in
Turkey, we had a problem, because the upper case of "i" isn't "I" in
Turkey (and vice versa).

Using a case-insensitive comparison is the best way of working.

Jon
Got u Jon. thanks.

-
shashank kadge

Mar 6 '07 #6
A nice shorthand overload is:

String.Compare(wsUri.Host, "localHost", true) == 0 ...

--
HTH,

Kevin Spencer
Microsoft MVP

Help test our new betas,
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:11**********************@j27g2000cwj.googlegr oups.com...
On Mar 6, 3:40 pm, "shashank kadge" <mailtosha...@gmail.comwrote:
>>
wsUri.Host.ToString().ToUpper().Equals("LOCALHOST ") would be much
simpler i think

It's not guaranteed to work though, due to potential culture issues.

I ran into a problem years ago where I was trying to compare strings
in a case-insensitive way by doing the above - and we found that in
Turkey, we had a problem, because the upper case of "i" isn't "I" in
Turkey (and vice versa).

Using a case-insensitive comparison is the best way of working.

Jon

Mar 7 '07 #7

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

Similar topics

2
11548
by: Will McGugan | last post by:
What is the best way to do a simple case insensitive comparison in Python? As far as I can tell, the only option is to do str1.lowercase() == str2.lowercase() (or uppercase). But that strikes me...
1
3138
by: ajay.sonawane | last post by:
How can I find the wheather the occurance substring is main string in wide char provided that the compasion should be case insensitive.
1
4210
by: Ron James | last post by:
I have a Listbox containing strings. When calling Contains (), I would like a case insensitive comparison. (I don't want to add newfile.txt to a Listbox containing NewFile.txt). I'm...
7
1695
by: Jan Nielsen | last post by:
Hi all I am learning Asp.Net using Vb.net (VS 2002). I found the following example in a book: Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles...
4
23928
by: Mark Rae | last post by:
Hi, Is it possible to create a case-insensitive List<stringcollection? E.g. List<stringMyList = new List<string>; MyList.Add("MyString"); So that:
1
5486
by: benhoefer | last post by:
I have been searching around and have not been able to find any info on this. I have a unique situation where I need a case sensitive map: std::map<string, intimap; I need to be able to run a...
1
11689
by: Coder Guy | last post by:
Is it possible to do a case insensitive comparison of individual characters? I tried: Char.ToLowerInvariant(charA).CompareTo(Char.ToLowerInvariant(charB)) And though it works for the most...
14
24242
by: Mosfet | last post by:
Hi, what is the most efficient way of doing a case insensitive comparison ? I am trying to write a universal String class and I am stuck with the case insensitive part : TCHAR is a char in...
0
201
by: Chris Rebert | last post by:
On Thu, Sep 4, 2008 at 3:37 PM, Robert Dailey <rcdailey@gmail.comwrote: Then store the string in its original case in the value part of the key-value pair: stage_map = (key,whatever) - Chris
0
7124
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...
1
7046
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...
0
5629
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,...
1
5053
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4707
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3195
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...
0
3182
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1558
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
418
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...

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.