Anderson Green
Anderson Green

Reputation: 31840

Check if a REBOL string contains another string

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?

Upvotes: 4

Views: 267

Answers (3)

sqlab
sqlab

Reputation: 6436

as written in prior answers find gives you back already a result you can use in conditional expressions.

>> either find "hello" "ll" [
[    print "hit"
[    ] [
[    print "no hit"
[    ]
hit

or in a more rebolish way

>> print either find "hello" "ll" [ "hit"] [ "no hit"]
hit

But you can also use to-logic on the result giving you true or false

>> print to-logic find "hello" "ll"
true
>> print  find "hello" "lzl"                 
none
>> print  to-logic find "hello" "lzl"
false

Upvotes: 1

@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.

Upvotes: 1

Shixin Zeng
Shixin Zeng

Reputation: 1438

Try this

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

Upvotes: 5

Related Questions