views:

41

answers:

2

I am using the switch statement to match some options which may or may not have VALUES associated with them, then extracting the values (which I imagine could be nothing or just a bunch of empty strings).

In Tcl are these equivalent? I.e. will trailing white space be fed as an option or parsed out? Should I "string trim" the value or is this unnecessary?

**

+1  A: 

By default, whitespace is not trimmed from the start or end of any strings. After all, it can sometimes be significant. If you do want to strip the leading and trailing whitespace, use:

set str [string trim $str]

You can just strip the leading space or trailing space by using string trimleft and string trimright respectively.

Donal Fellows
+1  A: 

In Tcl is whitespace equal to Empty string?

Whitespace is not equal to an empty string. Using TCL 8.5.2, the expression expr {"" eq " "} evaluates to 0.

will trailing white space be fed as an option or parsed out?

If you are accepting the strings as-is and performing no processing on them, then you will get any whitespace that is present.

Should I "string trim" the value or is this unnecessary?

The answer to this depends on your application. If the whitespace is not significant, trim it away using the strim trim command (as Donal Fellows mentioned). Doing so will likely simplify your logic. You can also use regsub -all {\s+} $input_string { } to collapse all repeated whitespace characters inside of a string.

bta