473,320 Members | 1,600 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,320 software developers and data experts.

Comparing Strings & String Case

I realise .NET has all these great objects, specifically strings, but is it
really necessary to create a string variable (object) just to compare two
string values?

For example, I'm looking at an attribute of an XML string, and I wish to
compare it's value to a literal string, but it has to be a case in-sensitive
comparison. Do I really need to put these two values into a variable in
order to use the ToUpper() or ToLower() methods of the String object?

Heres' what I have:

if(oNode.Attributes.GetNamedItem("level").Value == "package")
{
}

or do I have to do this:

string sNodeValue = oNode.Attributes.GetNamedItem("level").Value;
string sLiteral = "package";

if( sNodeValue.ToLower() == sLiteral.ToLower() )
{
}
Nov 17 '05 #1
6 2704
How about

String.Compare(oNode.Attributes.GetNamedItem("leve l").Value, "package",
true);

?

Nov 17 '05 #2
You can call ToLower on a literal string too:

if( sNodeValue.ToLower() == "Package".ToLower() )
{
}

Nov 17 '05 #3
const string sLiteral = "package"; // if it's already lower-cased, it isn't
gonna change

if( oNode.Attributes.GetNamedItem("level").Value.ToLow er() == sLiteral)
{
}
--
--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

"JSheble" <js************@logicor.com> wrote in message
news:u3**************@TK2MSFTNGP10.phx.gbl...
I realise .NET has all these great objects, specifically strings, but is it really necessary to create a string variable (object) just to compare two
string values?

For example, I'm looking at an attribute of an XML string, and I wish to
compare it's value to a literal string, but it has to be a case in-sensitive comparison. Do I really need to put these two values into a variable in
order to use the ToUpper() or ToLower() methods of the String object?

Heres' what I have:

if(oNode.Attributes.GetNamedItem("level").Value == "package")
{
}

or do I have to do this:

string sNodeValue = oNode.Attributes.GetNamedItem("level").Value;
string sLiteral = "package";

if( sNodeValue.ToLower() == sLiteral.ToLower() )
{
}

Nov 17 '05 #4
This does a very efficient case-insensitive comparison (thanks to Jon Skeet
who pointed this out to me a few months ago):

private static System.Globalization.CompareInfo MyCompareInfo =
System.Globalization.CultureInfo.CurrentCulture.Co mpareInfo;

if (MyCompareInfo.Compare(String1, String2,
System.Globalization.CompareOptions.IgnoreCase) == 0)
{...}
David Anton
www.tangiblesoftwaresolutions.com
Home of the Instant C# VB.NET to C# converter
and the Instant VB C# to VB.NET converter

"JSheble" wrote:
I realise .NET has all these great objects, specifically strings, but is it
really necessary to create a string variable (object) just to compare two
string values?

For example, I'm looking at an attribute of an XML string, and I wish to
compare it's value to a literal string, but it has to be a case in-sensitive
comparison. Do I really need to put these two values into a variable in
order to use the ToUpper() or ToLower() methods of the String object?

Heres' what I have:

if(oNode.Attributes.GetNamedItem("level").Value == "package")
{
}

or do I have to do this:

string sNodeValue = oNode.Attributes.GetNamedItem("level").Value;
string sLiteral = "package";

if( sNodeValue.ToLower() == sLiteral.ToLower() )
{
}

Nov 17 '05 #5
On 3 May 2005 10:46:54 -0700, "Chris Dunaway" <du******@gmail.com>
wrote:
You can call ToLower on a literal string too:

if( sNodeValue.ToLower() == "Package".ToLower() )
{
}

<scratching head> Now WHY would anyone ever do such a thing?

Nov 17 '05 #6
Goody,
You can call ToLower on a literal string too:

if( sNodeValue.ToLower() == "Package".ToLower() )

<scratching head> Now WHY would anyone ever do such a thing?


To make a sample to show how it works.

:-)

Cor
Nov 17 '05 #7

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

Similar topics

26
by: William Park | last post by:
How do you compare 2 strings, and determine how much they are "close" to each other? Eg. aqwerty qwertyb are similar to each other, except for first/last char. But, how do I quantify that? ...
5
by: Curtis Gilchrist | last post by:
I am required to read in records from a file and store them in descending order by an customer number, which is a c-style string of length 5. I am storing these records in a linked list. My...
4
by: agent349 | last post by:
First off, I know arrays can't be compared directly (ie: if (arrary1 == array2)). However, I've been trying to compare two arrays using pointers with no success. Basically, I want to take three...
88
by: William Krick | last post by:
I'm currently evaluating two implementations of a case insensitive string comparison function to replace the non-ANSI stricmp(). Both of the implementations below seem to work fine but I'm...
4
by: Martin Fletcher | last post by:
when I execute dim strTEMP_DATA as string strTEMP_DATA = Encoding.ASCII.GetString(rBuffer).ToString I alway get a string without the closing quote. I see this in the 'Autos' window, I also...
19
by: nitro_punk85 | last post by:
I'm working on a project for my c++ class and I am having trouble comparing one string to two others using the or operator. It looks something like this: if(answer3 == answer1 || answer2) Is...
0
by: Willing 2 Learn | last post by:
I'm working on this program below but im stuck. after it finds a match b/ween sentence & non_terminal it will output where these are found in both strings. I need it to spit out the rules with a...
19
by: pkirk25 | last post by:
I wonder if anyone has time to write a small example program based on this data or to critique my own effort? A file called Realm List.html contains the following data: Bladefist-Horde...
2
by: Pugi! | last post by:
hi, I am using this code for checking wether a value (form input) is an integer and wether it is smaller than a given maximum and greater then a given minimum value: function...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.