Implement String#contains

fixes #8659
This commit is contained in:
Michael Friedrich 2015-03-11 00:11:18 +01:00 committed by Gunnar Beutner
parent 47b3ed948d
commit 059cda9e37
2 changed files with 22 additions and 0 deletions

View File

@ -399,6 +399,20 @@ Example:
"Hello World".find("World") /* Returns 6 */ "Hello World".find("World") /* Returns 6 */
### <a id="string-contains"></a> String#contains
Signature:
function contains(str);
Returns `true` if the string `str` was found in the string. If the string
was not found `false` is returned. Use [find](20-library-reference.md#string-find)
for getting the index instead.
Example:
"Hello World".contains("World") /* Returns true */
### <a id="string-len"></a> String#len ### <a id="string-len"></a> String#len
Signature Signature

View File

@ -110,6 +110,13 @@ static Value StringFind(const std::vector<Value>& args)
return result; return result;
} }
static bool StringContains(const Value& value)
{
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
String self = vframe->Self;
return self.Contains(value);
}
static Value StringReplace(const String& search, const String& replacement) static Value StringReplace(const String& search, const String& replacement)
{ {
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame(); ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
@ -133,6 +140,7 @@ Object::Ptr String::GetPrototype(void)
prototype->Set("lower", new Function(WrapFunction(StringLower))); prototype->Set("lower", new Function(WrapFunction(StringLower)));
prototype->Set("split", new Function(WrapFunction(StringSplit))); prototype->Set("split", new Function(WrapFunction(StringSplit)));
prototype->Set("find", new Function(WrapFunction(StringFind))); prototype->Set("find", new Function(WrapFunction(StringFind)));
prototype->Set("contains", new Function(WrapFunction(StringContains)));
prototype->Set("replace", new Function(WrapFunction(StringReplace))); prototype->Set("replace", new Function(WrapFunction(StringReplace)));
} }