Forum Discussion
RoutingLoop_179
Cirrus
Mar 28, 2017Scan or split to find last instance of character.
Hi is there an efficient way to find the last occurrence of a character in a string and split into 2 variables.
Currently I use "scan [TCP::payload] {%[^@]@%s} garbage sitename"
This splits...
Kai_Wilke
MVP
Mar 29, 2017Hi RoutingLoop,
you may try a combination of
[string last] and [string range] to find the position of the last occurrence and to cut the leading/trailing values.
set input "test@test@test.de"
set last_at [string last "@" $input]
set leading_part [string range $input 0 [expr { $last_at - 1 }]]
set trailing_part [string range $input [expr { $last_at + 1 }] end]
... or you could try a combination of
[split], [lrange], [lindex] and [join] to create and select the required [list] entries and to format the resulting string.
set input "test@test@test.de"
set input_list [split $input "@" ]
set leading_part [join [lrange $input_list 0 end-1] "@"]
set trailing_part [lindex $input_list end]
Note: Performance wise both example are almost equal. So it will depend only on your personal preferences.
Cheers, Kai
Help guide the future of your DevCentral Community!
What tools do you use to collaborate? (1min - anonymous)Recent Discussions
Related Content
DevCentral Quicklinks
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com
Discover DevCentral Connects