ToUpper() vs UCase()

  • Thread starter Thread starter Raj
  • Start date Start date
R

Raj

string str1="rakshith";
if(str1.ToUpper()=="RAKSHITH")
{
//DO SOMETHING
}
Console.WriteLine("str1 value is : " + str1);
//prints as "RAKSHITH


if(UCase(str1)=="RAKSHITH")
{
//DO SOMETHING
}
Console.WriteLine("str1 value is: " + str1);
//prints as rakshith

Seems ToUpper() makes str1 mutable whereas in C# it is not!!

Any help would be appreciated

Thank you

Regards
Raj
 
Hello,
string str1="rakshith";
if(str1.ToUpper()=="RAKSHITH")
{
//DO SOMETHING
}
Console.WriteLine("str1 value is : " + str1);
//prints as "RAKSHITH

This is not what I'm seeing (it prints "rakshith" as expected). If you have
some code instead of // DO SOMETHING double check you are not altering str1
?...

Also not sure if the issue is VB or C# related. A c# group might be better
as you seems to provide only C# code...
 
Raj said:
string str1="rakshith";
if(str1.ToUpper()=="RAKSHITH")
{
//DO SOMETHING
}
Console.WriteLine("str1 value is : " + str1);
//prints as "RAKSHITH

.... only if the string variable 'str1' already contains "RAKSHITH".

BTW, I suggest using 'String.Compare' instead of comparing upper-case
values.
 
... only if the string variable 'str1' already contains "RAKSHITH".

BTW, I suggest using 'String.Compare' instead of comparing upper-case
values.

Other than the fact that String.Compare exists, is there any other
reason to use it instead of comparing upper (or lower) case values to
determine equality?
 
Other than the fact that String.Compare exists, is there any other
reason to use it instead of comparing upper (or lower) case values to
determine equality?

Actually a co-worker reminded me that our staff had talked about this
several months ago. The advantage of using String.Compare is that the
ToUpper and ToLower methods actually create a new string in memory and
the memory is not released until the garbage collector kicks in. So
using String.Compare instead of using ToUpper or ToLower to perform
case-insensitve compares can save on memory.
 
Joe said:
Other than the fact that String.Compare exists, is there any other
reason to use it instead of comparing upper (or lower) case values to
determine equality?

I assume that 'String.Compare' is semantically more correct than
comparing the upper-case versions of the strings. The reason for this
is that there are certain conversion rules for the lower-case to
upper-case conversion (actually in some cultures characters are
decomposed and thus result in multiple characters in the upper-case
version).

However, note that the '=' operator in VB will use VB's intrinsic string
comparison mechanism, which either performs a binary or a text
comparison depending on the 'Option Compare' Setting.
 
Raj said:
string str1="rakshith";
if(str1.ToUpper()=="RAKSHITH")
{
//DO SOMETHING
}
Console.WriteLine("str1 value is : " + str1);
//prints as "RAKSHITH


if(UCase(str1)=="RAKSHITH")
{
//DO SOMETHING
}
Console.WriteLine("str1 value is: " + str1);
//prints as rakshith

Seems ToUpper() makes str1 mutable whereas in C# it is not!!

Any help would be appreciated

Thank you

Regards
Raj

I don't get those results (nor did I expect to). They both return the
original lower case version.

The reason being is that both of these create a new string that is made up
of the result of the method and you are not actually creating a reference to
that newly created string, you are just writing out the original string.

-Scott
 
That's not VB.
Also, C# does not allow using "UCase" unqualified even if you're referencing
the VisualBasic assembly.

Neither 'UCase' nor 'ToUpper' modify the string in-place. Both the VB
global function and the .NET string 'ToUpper' method return new strings.
 
David said:
That's not VB.
Also, C# does not allow using "UCase" unqualified even if you're referencing
the VisualBasic assembly.

Neither 'UCase' nor 'ToUpper' modify the string in-place. Both the VB
global function and the .NET string 'ToUpper' method return new strings.

Well, this is a VB.net question...

For what it is worth, you can use the following in C#:

static void Main(string[] args)
{
string s = Microsoft.VisualBasic.Strings.UCase("Fred");
Console.WriteLine(s);
Console.ReadKey();
}

Just add a reference to Microsoft.VisualBasic.
 
Family said:
Also, C# does not allow using "UCase" unqualified even if you're
referencing the VisualBasic assembly.

Neither 'UCase' nor 'ToUpper' modify the string in-place. Both the VB
global function and the .NET string 'ToUpper' method return new strings.

Well, this is a VB.net question...

For what it is worth, you can use the following in C#:

static void Main(string[] args)
{
string s = Microsoft.VisualBasic.Strings.UCase("Fred");
Console.WriteLine(s);
Console.ReadKey();
}

Just add a reference to Microsoft.VisualBasic.

Yep, but you are using 'UCase' qualified by the namespace and module
name. C# doesn't support importing modules, making it possible to use
the function without further qualification.
 
Herfried said:
Family said:
Also, C# does not allow using "UCase" unqualified even if you're
referencing the VisualBasic assembly.

Neither 'UCase' nor 'ToUpper' modify the string in-place. Both the
VB global function and the .NET string 'ToUpper' method return new
strings.

Well, this is a VB.net question...

For what it is worth, you can use the following in C#:

static void Main(string[] args)
{
string s = Microsoft.VisualBasic.Strings.UCase("Fred");
Console.WriteLine(s);
Console.ReadKey();
}

Just add a reference to Microsoft.VisualBasic.

Yep, but you are using 'UCase' qualified by the namespace and module
name. C# doesn't support importing modules, making it possible to use
the function without further qualification.


Oh, sorry. When I read it, I missed the word unqualified.
 
Back
Top