0

TCL の文字列について質問があります。

HANDLE_NAME "/group1/team1/RON"

proc HANDLE_NAME {playerName} {
    #do something here
}

文字列「/group1/team1/RON」を proc に渡しますが、HANDLE_NAME 内のどこかで、「RON」である最後の部分だけが必要です。入力文字列を操作して入力の最後の部分を取得する方法( RON のみ) を変数に設定しますか?

誰でも助けることができますか?

4

4 に答える 4

3
proc HANDLE_NAME {playerName} {
    set lastPart [lindex [split $playerName "/"] end]
    # ...
}
于 2012-06-21T18:24:37.287 に答える
2

そして、4番目の答えを追加するには、文字列が実際にファイルへのパスである場合は、次を使用しますfile

set filename [file tail $playerName]
于 2012-06-27T10:55:37.380 に答える
1

文字列lastを使用して、最後のスラッシュを検索します。次に、文字列範囲を使用して、その後のテキストを取得します。 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]
于 2012-06-21T18:26:36.863 に答える
1

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.

于 2012-06-24T14:19:41.283 に答える