checking strings
Author |
Message |
bmass
|
Posted: Tue Nov 14, 2006 10:09 pm Post subject: checking strings |
|
|
Hey there, is there a way to check if a string contains a certain set of characters. Not if a string is equal to a certain set of characters but for example, if I have a variable string that is "Vanilla cookie with sprinkles, peanuts, almonds", I want to have an if statement where the condition is whether a variable has "peanuts" anywhere in the string. How can I do this? Thanks. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
TokenHerbz
|
Posted: Tue Nov 14, 2006 11:23 pm Post subject: (No subject) |
|
|
Here i made you a very small program which dose what you ask.
code: |
var text: string := ""
var word: string := ""
fcn search (text_: string, word_: string): boolean
if index(text_,word_) not= 0 then
result true
else
result false
end if
end search
get text:*
get word
if search(text,word) = true then
put word, " is found in: ", text
else
put word, " is NOT in: ", text
end if
|
1) its case sensitive
2) its not flexible
Ill let you optomize it... |
|
|
|
|
|
zylum
|
Posted: Tue Nov 14, 2006 11:23 pm Post subject: (No subject) |
|
|
you can use the index method. for more info on strings, check out cervantes' tutorial |
|
|
|
|
|
bmass
|
Posted: Wed Nov 15, 2006 8:56 am Post subject: (No subject) |
|
|
Thanks TokenHerbz. Can this be used in arrays? For example:
code: | var name, final : array 1 .. 12 of string
for x : 1 .. 12
fcn search (name_ (x) : string, final_ (x) : string) : boolean
if index (name_ (x), final_ (x)) not= 0 then
result true
else
result false
end if
end search
if search (name (x), final (x)) = true then
put final, " is found in: ", name (x)
else
put final, " is NOT in: ", name (x)
end if
end for |
Where name(1 - 12) and final(1 - 12) are defined earlier in the code? |
|
|
|
|
|
Clayton
|
Posted: Wed Nov 15, 2006 9:31 am Post subject: (No subject) |
|
|
errr... you don't put functions in loops.... read up on the function tutorial in the Turing Walkthrough, right now though, if you are only looking for the string once or twice, you could get away just doing the index function in your main line.
TokenHerbz... your not really supposed to just give away the answer to the person, you should know this by now. |
|
|
|
|
|
TokenHerbz
|
Posted: Wed Nov 15, 2006 11:18 am Post subject: (No subject) |
|
|
its not an answer if he dosn't know how to use it...
its a guild line, he'll have to play around with it and learn it, understand it to optomize it for the whole thing he needs it to do. |
|
|
|
|
|
wtd
|
Posted: Wed Nov 15, 2006 5:41 pm Post subject: (No subject) |
|
|
code: | fcn search (text_: string, word_: string): boolean
if index(text_,word_) not= 0 then
result true
else
result false
end if
end search |
Doing way too much work here.
code: | fcn search (text_: string, word_: string): boolean
result index (text_, word_) not= 0
end search |
|
|
|
|
|
|
bmass
|
Posted: Wed Nov 15, 2006 7:42 pm Post subject: (No subject) |
|
|
Well actually, I need it to check a number of variables defined by the user. I currently have:
get number
var project : array 1 .. number of string
for x : 1 .. number
project(x) := project(x) + other(x)
end for
And then I need for certain strings added by 'other' in project(x) |
|
|
|
|
|
Sponsor Sponsor
|
|
|
|
|