| View previous topic :: View next topic |
| Author |
Message |
NewzNZ Halfop

Joined: 05 Mar 2009 Posts: 60
|
Posted: Sat Aug 22, 2020 11:07 pm Post subject: question & exclaimation marks |
|
|
Hi there
Am just trying to filter question marks or exclaimation marks in a string...just wondering if something like this is the right way about it:
| Code: |
if {[string match {*\?*} $var] || [string match {*\!*} $var]} {
do some stuff here etc
}
|
Thanks in advance for any help! |
|
| Back to top |
|
 |
caesar Mint Rubber

Joined: 14 Oct 2001 Posts: 3741 Location: Mint Factory
|
Posted: Sun Aug 23, 2020 3:54 am Post subject: |
|
|
What do you mean by filtering? If you mean to see if the string has any of the two then you got two options: string index or regexp.
string first
| Code: |
% set text "something ! in here"
something ! in here
% string first "!" $text
10
|
notice that this returns the position of the needle in the haystack, so you need to do something like:
| Code: |
if {[string first "!" $text] > -1 || [string first "?" $text] > -1} {
# do something
}
|
regexp:
| Code: |
% regexp -- {\!|\?} $text
1
|
notice that this one returns 0/1 (false or true) if any of the two is in there.
If you meant to replace/remove them from the string then you need string map like this:
| Code: |
% string map {"!" ""} $text
something in here
% set text "something ? in here"
something ? in here
% string map {"?" ""} $text
something in here
|
Edit: Fixed a typo. _________________ Once the game is over, the king and the pawn go back in the same box.
Last edited by caesar on Mon Aug 24, 2020 12:38 am; edited 1 time in total |
|
| Back to top |
|
 |
NewzNZ Halfop

Joined: 05 Mar 2009 Posts: 60
|
Posted: Sun Aug 23, 2020 4:59 pm Post subject: |
|
|
Hi - that's great thank you for the suggestions - will try those options...
Thank you again! |
|
| Back to top |
|
 |
|