Tokenize strings : String Split « Data Types « C# / C Sharp






Tokenize strings

Tokenize strings
    
/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/


// Tokenize strings. 
 
using System; 
 
public class TokenizeDemo { 
  public static void Main() { 
    string[] input = { 
                      "100 + 19", 
                      "100 / 3.3", 
                      "-3 * 9", 
                      "100 - 87" 
                     }; 
    char[] seps = {' '}; 
 
    for(int i=0; i < input.Length; i++) { 
      // split string into parts 
      string[] parts = input[i].Split(seps); 
      Console.Write("Command: "); 
      for(int j=0; j < parts.Length; j++) 
        Console.Write(parts[j] + " "); 
    
      Console.Write(", Result: "); 
      double n = Double.Parse(parts[0]); 
      double n2 = Double.Parse(parts[2]); 
 
      switch(parts[1]) { 
        case "+": 
          Console.WriteLine(n + n2); 
          break; 
        case "-": 
          Console.WriteLine(n - n2); 
          break; 
        case "*": 
          Console.WriteLine(n * n2); 
          break; 
        case "/": 
          Console.WriteLine(n / n2); 
          break; 
      } 
    } 
  } 
}

           
         
    
    
    
  








Related examples in the same category

1.Split with space
2.Split with a list of dividers
3.Split spaces with space
4.Show string in proper case
5.use the Split() method to split strings
6.Splitting StringsSplitting Strings
7.Split and join stringsSplit and join strings
8.Char String foreachChar String foreach
9.Split a string delimited by another string and return 2 non-empty elements
10.Split a string delimited by another string and return 2 elements
11.Split the original string at the delimiter and return all non-empty elements
12.Split a string delimited by another string and return all elements
13.Split a string delimited by characters and return all elements
14.Split a string delimited by characters and return 2 non-empty elements
15.Split a string delimited by characters and return 2 elements
16.Split a string delimited by characters and return all non-empty elements
17.Use StringSplitOptions enumeration to include or exclude substrings generated by the Split method.
18.Implements a StringTokenizer class for splitting a string into substrings using a set of delimiters.
19.String Tokenizer
20.Can parse a string representing a string[] into an actual string[].
21.Converts a collection of tags to a single string representation containing the tags separated by a comma
22.Takes in any string and convert it into a Byte array, suitable for e.g. insertion into a REG_BINARY Registry value.
23.Splits the string into lines.
24.Splits the string into words (all white space is removed).
25.Split Quoted String
26.Helper class to split a long word into a single one.
27.Splits the string into an array, using the separator.
28.Splits string name into a readable string based on camel casing.