user707549
user707549

Reputation:

in TCL how to operate the string?

I have a question about string in TCL:

HANDLE_NAME "/group1/team1/RON"

proc HANDLE_NAME {playerName} {
    #do something here
}

we pass the string "/group1/team1/RON" to the proc, but somewhere inside of the HANDLE_NAME, we only need the last part which is "RON", how to operate the input string and get the last part of the input(only RON) and set it to a variable?

can anyone help?

Upvotes: 0

Views: 256

Answers (4)

Chris
Chris

Reputation: 3817

And to add a fourth answer, if the string is actually a path to a file, use file:

set filename [file tail $playerName]

Upvotes: 2

schlenk
schlenk

Reputation: 7237

To add a third answer, you can use regexp anchored at the end of the string too.

regexp {/([^/]+)$} $playerName -> lastPart

But the lindex/split solution by acheong87 is surely the more natural way if the strings you use are like file paths.

Upvotes: 1

TrojanName
TrojanName

Reputation: 5355

Using string last to find the last forward slash. Then use string range to get the text after that. http://tcl.tk/man/tcl8.5/TclCmd/string.htm

set mystring "/group1/team1/RON"
set slash_pos [string last "/" $mystring]
set ron_start_pos [incr slash_pos]
set ron [string range $mystring $ron_start_pos end]

Upvotes: 1

Andrew Cheong
Andrew Cheong

Reputation: 30273

proc HANDLE_NAME {playerName} {
    set lastPart [lindex [split $playerName "/"] end]
    # ...
}

Upvotes: 3

Related Questions