Home

What If I Want to Switch Case in C#?

|
|  Updated:  
2018-01-25 13:50:08
C# 2010 All-in-One For Dummies
Explore Book
Buy On Amazon
You may be interested in whether all the characters (or just one) in a C# string are uppercase or lowercase characters. And you may need to convert from one to the other.

Distinguishing between all-uppercase and all-lowercase strings

You can use the switch statement to look for a particular string. Normally, you use the switch statement to compare a counting number to some set of possible values; however, switch does work on string objects as well. This version of the termination section in BuildASentence uses the switch construct:

switch(line)

{

case "EXIT":

case "exit":

case "QUIT":

case "quit":

return true;

}

return false;

This approach works because you’re comparing only a limited number of strings. The for loop offers a much more flexible approach for searching for string values. Using the case-less Compare() gives the program greater flexibility in understanding the user.

Converting a string to upper- or lowercase

Suppose you have a string in lowercase and need to convert it to uppercase. You can use the ToUpper() method:

string lowcase = "armadillo";

string upcase = lowcase.ToUpper(); // ARMADILLO.

Similarly, you can convert uppercase to lowercase with ToLower().

What if you want to convert just the first character in a string to uppercase? The following rather convoluted code will do it:

string name = "chuck";

string properName =

char.ToUpper(name[0]).ToString() + name.Substring(1, name.Length - 1);

The idea in this example is to extract the first char in name (that’s name[0]), convert it to a one-character string with ToString(), and then tack on the remainder of name after removing the old lowercase first character with Substring().

You can tell whether a string is uppercased or lowercased by using this scary-looking if statement:

if (string.Compare(line.ToUpper(CultureInfo.InvariantCulture),

line, false) == 0) ... // True if line is all upper.

Here the Compare() method is comparing an uppercase version of line to line itself. There should be no difference if line is already uppercase. The CultureInfo.InvariantCulture property tells Compare() to perform the comparison without considering culture. You can read more about it at Microsoft.com. If you want to ensure that the string contains all lowercase characters, stick a not (!) operator in front of the Compare() call. Alternatively, you can use a loop.

About This Article

This article is from the book: 

About the book author:

John Paul Mueller is a freelance author and technical editor. He has writing in his blood, having produced 100 books and more than 600 articles to date. The topics range from networking to home security and from database management to heads-down programming. John has provided technical services to both Data Based Advisor and Coast Compute magazines.

Bill Sempf is a seasoned programmer and .NET evangelist specializing in .NET applications.

Chuck Sphar is a programmer and former senior technical writer for the Visual C++ product group at Microsoft.