How do I use an if statement to check if a certain string of text is in another set of characters?How to set default values for Tcl variables?TCL: regexp exclude strings having charactersTCL string toupper (each word)Delete line regex is found onHow to force expr to address a value as a string and not a number?how to set text indices via a variableReading specific column of data from text file and write to another text file tclTcl/tk - Get window height and width and set relative text height in gridHow to append to array at certain keyTo match and extract multiple words from directory path using tcl
What is more environmentally friendly? An A320 or a car?
Is there a wealth gap in Boston where the median net worth of white households is $247,500 while the median net worth for black families was $8?
Do the books ever say oliphaunts aren’t elephants?
What happens when a flying sword is killed?
Does Dispel Magic destroy Artificer Turrets?
reconstruction filter - How does it actually work?
How does one get an animal off of the Altar surreptitiously?
Is there an antonym(a complementary antonym) for "spicy" or "hot" regarding food (I DO NOT mean "seasoned" but "hot")?
Why is の所 used after ドア in this sentence?
Compound Word Neologism
Spacing after a tikz figure
Why would anyone ever invest in a cash-only etf?
Examples of simultaneous independent breakthroughs
Japanese reading of an integer
Why does Canada require mandatory bilingualism in a lot of federal government posts?
Polyhedra, Polyhedron, Polytopes and Polygon
Incrementing add under condition in pandas
Does dual boot harm a laptop battery or reduce its life?
Why didn’t Christianity spread southwards from Ethiopia in the Middle Ages?
How likely is fragmentation on a table with 40000 products likely to affect performance
How did the Sinclair compare on price with the C64 in the UK?
Is it safe if the neutral lead is exposed and disconnected?
Why does the Eurostar not show youth pricing?
8086 stack segment and avoiding overflow in interrupts
How do I use an if statement to check if a certain string of text is in another set of characters?
How to set default values for Tcl variables?TCL: regexp exclude strings having charactersTCL string toupper (each word)Delete line regex is found onHow to force expr to address a value as a string and not a number?how to set text indices via a variableReading specific column of data from text file and write to another text file tclTcl/tk - Get window height and width and set relative text height in gridHow to append to array at certain keyTo match and extract multiple words from directory path using tcl
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I want to make an if statement that will return a value when a certain characters are in another set of texts. For example if I have the words water, and fire. I want to make an if statement that will check if the word I give it has characters "wa" then it will return a value of 1. Something along the lines of this.
set word "water"
set tempvar 0
if $word has characters "wa"
set tempvar 1
tcl
add a comment |
I want to make an if statement that will return a value when a certain characters are in another set of texts. For example if I have the words water, and fire. I want to make an if statement that will check if the word I give it has characters "wa" then it will return a value of 1. Something along the lines of this.
set word "water"
set tempvar 0
if $word has characters "wa"
set tempvar 1
tcl
add a comment |
I want to make an if statement that will return a value when a certain characters are in another set of texts. For example if I have the words water, and fire. I want to make an if statement that will check if the word I give it has characters "wa" then it will return a value of 1. Something along the lines of this.
set word "water"
set tempvar 0
if $word has characters "wa"
set tempvar 1
tcl
I want to make an if statement that will return a value when a certain characters are in another set of texts. For example if I have the words water, and fire. I want to make an if statement that will check if the word I give it has characters "wa" then it will return a value of 1. Something along the lines of this.
set word "water"
set tempvar 0
if $word has characters "wa"
set tempvar 1
tcl
tcl
edited Mar 26 at 19:42
Donal Fellows
106k15 gold badges118 silver badges182 bronze badges
106k15 gold badges118 silver badges182 bronze badges
asked Mar 26 at 19:31
MakuzaMakuza
1
1
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
The simplest (and fastest!) way to search for whether a substring is present is the string first
command; that returns -1 if the substring isn't there, and the (first) index of where it is found when the substring is present.
if [string first "wa" $word] >= 0
set tempvar 1
The other technique that works well is one of the lesser known operating modes of Tcl's regular expression engine: if the RE starts with ***=
then the rest of the string is a literal.
# This is marginally slower than string first; the overhead of the RE engine matters a little
if [regexp ***=wa $word]
set tempvar 1
Note that if you were asking if it was a prefix, other commands are more suitable (string equal
with the -length
option, or string match
). And use lsearch
if you want to know if a particular value is an element in a list, instead of string searching.
The reason you have to be a little careful is that a lot of the more sophisticated matchers have important metacharacters. It doesn't matter when looking forwa
, but if you're looking forwa
or*wa*
it can matter a lot. A little care at the beginning makes life much easier later on.
– Donal Fellows
Mar 29 at 9:38
add a comment |
I would encapsulate this in a proc:
proc string_contains haystack needle
expr [string first $needle $haystack] != -1
then
% string_contains water wa
1
% string_contains fire wa
0
and
% if [string_contains water wa] then puts yes else puts no
yes
% if [string_contains fire wa] then puts yes else puts no
no
One has to be careful about the empty string as needle then,string first
is just broken in this respect:string_contains water ""
will give0
.
– mrcalvin
Mar 28 at 23:36
add a comment |
Generally, in any language, for matching purpose, Regular Expressions are used, here used as regexp $pattern $string and return 1 if there is a match.
set word "water"
set tempvar 0
if [regexp "wa" $word]
set tempvar 1
puts $tempvar
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55364952%2fhow-do-i-use-an-if-statement-to-check-if-a-certain-string-of-text-is-in-another%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
The simplest (and fastest!) way to search for whether a substring is present is the string first
command; that returns -1 if the substring isn't there, and the (first) index of where it is found when the substring is present.
if [string first "wa" $word] >= 0
set tempvar 1
The other technique that works well is one of the lesser known operating modes of Tcl's regular expression engine: if the RE starts with ***=
then the rest of the string is a literal.
# This is marginally slower than string first; the overhead of the RE engine matters a little
if [regexp ***=wa $word]
set tempvar 1
Note that if you were asking if it was a prefix, other commands are more suitable (string equal
with the -length
option, or string match
). And use lsearch
if you want to know if a particular value is an element in a list, instead of string searching.
The reason you have to be a little careful is that a lot of the more sophisticated matchers have important metacharacters. It doesn't matter when looking forwa
, but if you're looking forwa
or*wa*
it can matter a lot. A little care at the beginning makes life much easier later on.
– Donal Fellows
Mar 29 at 9:38
add a comment |
The simplest (and fastest!) way to search for whether a substring is present is the string first
command; that returns -1 if the substring isn't there, and the (first) index of where it is found when the substring is present.
if [string first "wa" $word] >= 0
set tempvar 1
The other technique that works well is one of the lesser known operating modes of Tcl's regular expression engine: if the RE starts with ***=
then the rest of the string is a literal.
# This is marginally slower than string first; the overhead of the RE engine matters a little
if [regexp ***=wa $word]
set tempvar 1
Note that if you were asking if it was a prefix, other commands are more suitable (string equal
with the -length
option, or string match
). And use lsearch
if you want to know if a particular value is an element in a list, instead of string searching.
The reason you have to be a little careful is that a lot of the more sophisticated matchers have important metacharacters. It doesn't matter when looking forwa
, but if you're looking forwa
or*wa*
it can matter a lot. A little care at the beginning makes life much easier later on.
– Donal Fellows
Mar 29 at 9:38
add a comment |
The simplest (and fastest!) way to search for whether a substring is present is the string first
command; that returns -1 if the substring isn't there, and the (first) index of where it is found when the substring is present.
if [string first "wa" $word] >= 0
set tempvar 1
The other technique that works well is one of the lesser known operating modes of Tcl's regular expression engine: if the RE starts with ***=
then the rest of the string is a literal.
# This is marginally slower than string first; the overhead of the RE engine matters a little
if [regexp ***=wa $word]
set tempvar 1
Note that if you were asking if it was a prefix, other commands are more suitable (string equal
with the -length
option, or string match
). And use lsearch
if you want to know if a particular value is an element in a list, instead of string searching.
The simplest (and fastest!) way to search for whether a substring is present is the string first
command; that returns -1 if the substring isn't there, and the (first) index of where it is found when the substring is present.
if [string first "wa" $word] >= 0
set tempvar 1
The other technique that works well is one of the lesser known operating modes of Tcl's regular expression engine: if the RE starts with ***=
then the rest of the string is a literal.
# This is marginally slower than string first; the overhead of the RE engine matters a little
if [regexp ***=wa $word]
set tempvar 1
Note that if you were asking if it was a prefix, other commands are more suitable (string equal
with the -length
option, or string match
). And use lsearch
if you want to know if a particular value is an element in a list, instead of string searching.
answered Mar 26 at 19:49
Donal FellowsDonal Fellows
106k15 gold badges118 silver badges182 bronze badges
106k15 gold badges118 silver badges182 bronze badges
The reason you have to be a little careful is that a lot of the more sophisticated matchers have important metacharacters. It doesn't matter when looking forwa
, but if you're looking forwa
or*wa*
it can matter a lot. A little care at the beginning makes life much easier later on.
– Donal Fellows
Mar 29 at 9:38
add a comment |
The reason you have to be a little careful is that a lot of the more sophisticated matchers have important metacharacters. It doesn't matter when looking forwa
, but if you're looking forwa
or*wa*
it can matter a lot. A little care at the beginning makes life much easier later on.
– Donal Fellows
Mar 29 at 9:38
The reason you have to be a little careful is that a lot of the more sophisticated matchers have important metacharacters. It doesn't matter when looking for
wa
, but if you're looking for wa
or *wa*
it can matter a lot. A little care at the beginning makes life much easier later on.– Donal Fellows
Mar 29 at 9:38
The reason you have to be a little careful is that a lot of the more sophisticated matchers have important metacharacters. It doesn't matter when looking for
wa
, but if you're looking for wa
or *wa*
it can matter a lot. A little care at the beginning makes life much easier later on.– Donal Fellows
Mar 29 at 9:38
add a comment |
I would encapsulate this in a proc:
proc string_contains haystack needle
expr [string first $needle $haystack] != -1
then
% string_contains water wa
1
% string_contains fire wa
0
and
% if [string_contains water wa] then puts yes else puts no
yes
% if [string_contains fire wa] then puts yes else puts no
no
One has to be careful about the empty string as needle then,string first
is just broken in this respect:string_contains water ""
will give0
.
– mrcalvin
Mar 28 at 23:36
add a comment |
I would encapsulate this in a proc:
proc string_contains haystack needle
expr [string first $needle $haystack] != -1
then
% string_contains water wa
1
% string_contains fire wa
0
and
% if [string_contains water wa] then puts yes else puts no
yes
% if [string_contains fire wa] then puts yes else puts no
no
One has to be careful about the empty string as needle then,string first
is just broken in this respect:string_contains water ""
will give0
.
– mrcalvin
Mar 28 at 23:36
add a comment |
I would encapsulate this in a proc:
proc string_contains haystack needle
expr [string first $needle $haystack] != -1
then
% string_contains water wa
1
% string_contains fire wa
0
and
% if [string_contains water wa] then puts yes else puts no
yes
% if [string_contains fire wa] then puts yes else puts no
no
I would encapsulate this in a proc:
proc string_contains haystack needle
expr [string first $needle $haystack] != -1
then
% string_contains water wa
1
% string_contains fire wa
0
and
% if [string_contains water wa] then puts yes else puts no
yes
% if [string_contains fire wa] then puts yes else puts no
no
answered Mar 26 at 22:02
glenn jackmanglenn jackman
177k26 gold badges155 silver badges251 bronze badges
177k26 gold badges155 silver badges251 bronze badges
One has to be careful about the empty string as needle then,string first
is just broken in this respect:string_contains water ""
will give0
.
– mrcalvin
Mar 28 at 23:36
add a comment |
One has to be careful about the empty string as needle then,string first
is just broken in this respect:string_contains water ""
will give0
.
– mrcalvin
Mar 28 at 23:36
One has to be careful about the empty string as needle then,
string first
is just broken in this respect: string_contains water ""
will give 0
.– mrcalvin
Mar 28 at 23:36
One has to be careful about the empty string as needle then,
string first
is just broken in this respect: string_contains water ""
will give 0
.– mrcalvin
Mar 28 at 23:36
add a comment |
Generally, in any language, for matching purpose, Regular Expressions are used, here used as regexp $pattern $string and return 1 if there is a match.
set word "water"
set tempvar 0
if [regexp "wa" $word]
set tempvar 1
puts $tempvar
add a comment |
Generally, in any language, for matching purpose, Regular Expressions are used, here used as regexp $pattern $string and return 1 if there is a match.
set word "water"
set tempvar 0
if [regexp "wa" $word]
set tempvar 1
puts $tempvar
add a comment |
Generally, in any language, for matching purpose, Regular Expressions are used, here used as regexp $pattern $string and return 1 if there is a match.
set word "water"
set tempvar 0
if [regexp "wa" $word]
set tempvar 1
puts $tempvar
Generally, in any language, for matching purpose, Regular Expressions are used, here used as regexp $pattern $string and return 1 if there is a match.
set word "water"
set tempvar 0
if [regexp "wa" $word]
set tempvar 1
puts $tempvar
answered Mar 31 at 4:56
DrektzDrektz
348 bronze badges
348 bronze badges
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55364952%2fhow-do-i-use-an-if-statement-to-check-if-a-certain-string-of-text-is-in-another%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown