Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a REBOL string contains another string

Tags:

rebol

I tried using the find function to check for the occurrence of the string "ll" in the string "hello", but it returns "ll" instead of true or false:

"This prints 'll'"
print find "hello" "ll"

Does REBOL's standard library have any functions to check if a string contains another string?

like image 559
Anderson Green Avatar asked Mar 18 '23 18:03

Anderson Green


2 Answers

Try this

>> print found? find "hello" "ll"
true
>> print found? find "hello" "abc"
false
like image 61
Shixin Zeng Avatar answered May 16 '23 08:05

Shixin Zeng


@ShixinZeng is correct that there is a FOUND? in Rebol. However, it is simply defined as:

found?: function [
    "Returns TRUE if value is not NONE."
    value
] [
    not none? :value
]

As it is equivalent to not none?...you could have written:

>> print not none? find "hello" "ll"
true
>> print not none? find "hello" "abc"
false

Or if you want the other bias:

>> print none? find "hello" "ll"
false
>> print none? find "hello" "abc"
true

Intended to help with readability while using FIND. However, I don't like it because it has confusing behavior by working when not used with FIND, e.g.

>> if found? 1 + 2 [print "Well this is odd..."]
Well this is odd...

Since you can just use the result of the FIND in a conditional expression with IF and UNLESS and EITHER, you don't really need it very often...unless you are assigning the result to a variable you really want to be boolean. In which case, I don't mind using NOT NONE? or NONE? as appropriate.

And in my opinion, losing FOUND? from the core would be fine.

like image 34
HostileFork says dont trust SE Avatar answered May 16 '23 06:05

HostileFork says dont trust SE