Name

splitTokens()

Description

The splitTokens() function splits a String at one or many character delimiters or "tokens". The delim parameter specifies the character or characters to be used as a boundary.

If no delim characters are specified, any whitespace character is used to split. Whitespace characters include tab (\t), line feed (\n), carriage return (\r), form feed (\f), and space.

After using this function to parse incoming data, it is common to convert the data from Strings to integers or floats by using the datatype conversion functions int() and float().

Examples

  • String t = "a b";
    String[] q = splitTokens(t);
    println(q[0]);  // Prints "a"
    println(q[1]);  // Prints "b"
    
  • // Despite the bad formatting, the data is parsed correctly.
    // The ", " as delimiter means to break whenever a comma *or*
    // a space is found in the String. Unlike the split() function, 
    // multiple adjacent delimiters are treated as a single break.
    String s = "a, b c ,,d "; 
    String[] q = splitTokens(s, ", ");
    println(q.length + " values found");  // Prints "4 values found"
    println(q[0]);  // Prints "a"
    println(q[1]);  // Prints "b"
    println(q[2]);  // Prints "c"
    println(q[3]);  // Prints "d"
    

Syntax

  • splitTokens(value)
  • splitTokens(value, delim)

Parameters

  • value(String)the String to be split
  • delim(String)list of individual characters that will be used as separators

Return

  • String[]