newkid91
newkid91

Reputation: 469

Why doesn't ToUpper() return "*" when applied to "8"?

string s = "h";
s = s.ToUpper();

returns "H".

string s = "8";
s = s.ToUpper();

returns "8"

Should this not return "*"?

Upvotes: 30

Views: 7166

Answers (5)

Vloxxity
Vloxxity

Reputation: 980

  1. Numbers don't have uppercases.
  2. if you would use the function ToUpper() to following Text: "there are 8 buildings"
    What result would you like to have? "THERE ARE 8 BUILDINGS" or "THERE ARE * BUILDINGS"
  3. the Keyboardlayout is localized e.g. on german keyboards there is a ( at the 8 key

Upvotes: 1

BoltClock
BoltClock

Reputation: 723719

No, it shouldn't. ToUpper() doesn't mean WithShiftKeyOnAnInternationalASCIIKeyboard(). There isn't an uppercase 8, as 8 is a number, not a letter.

Of course, this is a gross over-simplification (being a number alone doesn't automatically make a certain character in a character set caseless), but it's likely what you're asking for anyway so I'll leave it at that.

Upvotes: 93

Scott McIntyre
Scott McIntyre

Reputation: 1375

The real answer is because the TextInfo associated with the CultureInfo for en-US does not define "*" as the uppercase of "8".

It may be possible to extend that TextInfo, override toUpper(), and have it work like you wish.

Upvotes: 6

BugFinder
BugFinder

Reputation: 17858

Just because you press shift 8 to get a * doesnt make it an uppercase value, it only applies for a-z characters.

Upvotes: -1

Oded
Oded

Reputation: 499042

Because there is no upper case 8.

Just because the specific keyboard you are using has a * on the same key as the 8, doesn't mean that all keyboards do. Some languages do not have upper case letter - what should ToUpper return for those?

String.ToUpper():

This method uses the casing rules of the current culture to convert each character in the current instance to its uppercase equivalent. If a character does not have an uppercase equivalent, it is included unchanged in the returned string.

Upvotes: 28

Related Questions