Iron Man
Iron Man

Reputation: 25

Removing everything before a certain character in TCL

Input string : 4567-ABC

I want to remove everything before "-" in the string so that Output will be ABC.

Output: ABC

Upvotes: 1

Views: 943

Answers (2)

glenn jackman
glenn jackman

Reputation: 247210

string last is useful here:

set string 4567-ABC
set idx [string last "-" $string]
set wanted [string range $string $idx+1 end]

Or without the intermediate variable

set wanted [string range $string [string last "-" $string]+1 end]

That even works if the original string does not contain any hyphens.

Upvotes: 1

Chris Heithoff
Chris Heithoff

Reputation: 1873

If you want to avoid regular expressions:

set string 4567-ABC

set output [lindex [split $string "-"] 1]

The split command takes a string and split characters as the arguments and returns a list.

Upvotes: 1

Related Questions