Split Function in Tcl: Usage & Examples

Formulas & functions · Updated November 23, 2026

The split function in Tcl divides a string into a list of substrings based on a set of separator characters, returning a Tcl list. For example, split "a,b,c" "," returns the list a b c.

What Is the split Command?

In Tcl, split is a built-in command (not a function in the mathematical sense) that takes a string and optionally a set of characters to use as delimiters. It returns a Tcl list where each element is a substring of the original string that was separated by any of the delimiter characters. If no delimiter is given, the string is split into individual characters. This command is fundamental for text processing, parsing data files, and handling user input.

Basic Syntax and Arguments

The syntax is:

split string ?splitChars?
  • string – the input string to be split.
  • splitChars (optional) – a string containing the set of characters that act as delimiters. Each character in this string is treated as a separator.

If splitChars is omitted, the string is split after every character, producing a list of single-character elements. For example, split "hello" returns h e l l o.

How split Works with Delimiters

When you provide a delimiter string, each character in that string is a delimiter. Consecutive delimiters produce empty list elements. For instance, split "a,,b" "," returns a {} b. This is different from many other languages where consecutive delimiters are collapsed. To handle empty fields, you may need to filter them out after splitting.

If you want to split on a multi-character string (like a comma followed by a space), you cannot directly pass that as a single delimiter because each character is treated separately. Instead, you can use regexp or string map to replace the multi-character delimiter with a single character first, or use regexp -all -inline to extract fields.

Practical Examples

Here are some common use cases:

  • Parsing CSV data: split $line "," splits a comma-separated line into fields.
  • Splitting on whitespace: split $line " " splits on spaces, but note that tabs and multiple spaces are not handled unless you include them in the delimiter set.
  • Breaking a string into characters: split $string gives a list of characters, useful for iterating over each character.
  • Handling path separators: On Windows, split $path "/\\" splits on both forward and backslashes.

Common Pitfalls and Misconceptions

A frequent mistake is assuming that split uses a regular expression or a multi-character delimiter. It does not. Another pitfall is forgetting that empty fields are preserved, which can be surprising when parsing data with trailing delimiters. For example, split "a,b," "," returns a b {}. To remove empty elements, you can use lsearch -all -inline -not -exact $list {} or a foreach loop.

Also, if the delimiter string is empty (i.e., split $string ""), it behaves like no delimiter, splitting into characters. This is a common source of confusion.

Alternatives and Related Commands

Depending on your needs, other Tcl commands may be more suitable:

  • regexp – use regexp -all -inline to split on a regular expression pattern, which allows multi-character delimiters and more complex rules.
  • string map – replace a multi-character delimiter with a single character before splitting.
  • scan – parse formatted input, useful for fixed-width fields.
  • join – the inverse operation, which concatenates a list into a string with a delimiter.

For example, to split on a comma followed by an optional space, you could do: regexp -all -inline {[^,]+} $line but that loses empty fields; a better approach is to use split after normalizing the string.

Performance Considerations

split is implemented in C and is very fast for typical strings. For large strings, it is still efficient, but be aware that it creates a list of all substrings, which can consume memory if the string is huge. If you only need to process fields sequentially, consider using regexp -all -inline or a loop with string first to avoid building the entire list at once.

When splitting on whitespace, note that the delimiter set must include all whitespace characters you want to consider (space, tab, newline). A common idiom is split $string " \t\n" to split on spaces, tabs, and newlines.

Frequently Asked Questions

How do I split a string on multiple characters in Tcl?

Use split with a delimiter string containing all the characters you want to split on. For example, split "a-b_c" "-_" splits on both hyphen and underscore, returning a b c. If you need to split on a multi-character sequence like “->”, first replace it with a single character using string map, then split.

Does split preserve empty fields?

Yes, consecutive delimiters produce empty list elements. For instance, split "a,,b" "," returns a {} b. If you want to ignore empty fields, filter them out after splitting, for example with lsearch -all -inline -not -exact $result {}.

What is the difference between split and regexp in Tcl?

split uses a set of literal characters as delimiters and is faster and simpler. regexp with -all -inline allows regular expression patterns, so you can match multi-character delimiters, optional whitespace, or other complex patterns. Use split for simple character-delimited data, and regexp when you need pattern-based splitting.

How do I split a string into characters in Tcl?

Simply call split without a second argument: split "hello" returns h e l l o. This is useful for iterating over each character in a string.

Can I split a string on a newline character?

Yes, include the newline character in the delimiter string. For example, split $data "\n" splits on newlines. If your data uses Windows line endings (\r\n), you may want to split on both: split $data "\r\n".

Related

Try it: the Formula Explainer: paste any formula and see each function and reference in plain English.

More in Formulas & functions

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.