Switch on String

In this chapter you will learn:

  1. A string can control a switch statement

A string can control a switch statement

using System; /*from   java2  s.  c  o  m*/
 
class MainClass {  
  public static void Main() {  
    string[] strs = { "one", "two", "three", "two", "one" }; 
 
    foreach(string s in strs) { 
      switch(s) { 
        case "one": 
          Console.Write(1); 
          break; 
        case "two": 
          Console.Write(2); 
          break; 
        case "three": 
          Console.Write(3); 
          break; 
      } 
    } 
    Console.WriteLine(); 
 
  } 
}

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. Use the Trim(), TrimStart(), and TrimEnd() methods to trim strings
  2. Trim white space
  3. Trim #s from String
Home » C# Tutorial » String
string
String creation
Char in string
Compare strings
String equality
String concatanation
String copy
String Join
String split
String Search for Index
String contains
String start with
String insert
String case
Replacing substring
Remove from a string
Substring
Escape Characters
String verbatim
String padding
Switch on String
String trim
String intern
String normalization
Empty String