Contains text: Difference between revisions

From Kolmafia
Jump to navigation Jump to search
imported>Grotfang
Create page
 
Convert to Template:Function2 format
 
(5 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{
<onlyinclude>{{{{{format|Function2}}}
#vardefine:name|contains_text}}{{
|name=contains_text
#vardefine:return_type|boolean}}{{
|function1.return_type=boolean
 
|function1.description=Returns true if the first string contains the second string.
FunctionPage|
|function1.param1=str
 
|function1.param1.type=string
name={{#var:name}}|
|function1.param1.description=String to search in
function_category=String Handling Routines|
|function1.param2=substring
 
|function1.param2.type=string
function1={{Function|
|function1.param2.description=Substring to check for
name={{#var:name}}|
|description=
aggregate={{#var:aggregate}}|
|code1={{CodeSample
return_type={{#var:return_type}}|
  |title=Code Samples
return_also={{#var:return_also}}|
  |description=This function checks whether the string "Hello World!" contains the word "World".
parameter1={{Param|string|str}}|
  |code=
parameter2={{Param|string|substring}}|
<syntaxhighlight lang="d">
p1desc={{pspan|str}} is the string you want to test.|
boolean string_check()
p2desc={{pspan|substring}} is the substring you want to check is present inside {{pspan|str}}.
{
}}|
   if ( contains_text("Hello World!" , "World" ) )
 
  {
function_description=Returns true if {{pspan|substring}} can be found within {{pspan|str}}.|
 
code1={{CodeSample|
title=Code Samples|
description=This function tests to see whether the string "Hello World!" contains the word "World".|
code=
<syntaxhighlight>
boolean string_check(){
   if(contains_text("Hello World!" , "World")){
       return true;
       return true;
   }
   }
Line 33: Line 24:
}
}
</syntaxhighlight>
</syntaxhighlight>
}}|
  }}
 
|code2={{CodeSample
  |description=This function checks whether a character is in a clan.
  |code=
<syntaxhighlight lang="d">
boolean in_clan()
{
  string character = visit_url( "showplayer.php?who=" + my_id().to_string() );
  if ( contains_text( character , "showclan.php?whichclan" ) )
  {
      return true;
  }
  return false;
}
</syntaxhighlight>
  }}
|code3={{CodeSample|
  |description=Or this, more concise version...
  |code=
<syntaxhighlight lang="d">
boolean in_clan()
{
  return contains_text( visit_url( "showplayer.php?who=" + my_id().to_string() ), "showclan.php?whichclan" );
}
</syntaxhighlight>
}}
}}
|see_also={{SeeAlso/Substring Search}}
}}</onlyinclude>
[[Category:String Handling Routines]]

Latest revision as of 12:42, 23 December 2020

Function Syntax

boolean contains_textstring str, string substring )

Returns true if the first string contains the second string.
  • str: String to search in
  • substring: Substring to check for

Code Samples

This function checks whether the string "Hello World!" contains the word "World".

boolean string_check()
{
   if ( contains_text("Hello World!" , "World" ) )
   {
      return true;
   }
   return false;
}

This function checks whether a character is in a clan.

boolean in_clan()
{
   string character = visit_url( "showplayer.php?who=" + my_id().to_string() );
   if ( contains_text( character , "showclan.php?whichclan" ) )
   {
      return true;
   }
   return false;
}

Or this, more concise version...

boolean in_clan()
{
   return contains_text( visit_url( "showplayer.php?who=" + my_id().to_string() ), "showclan.php?whichclan" );
}

See Also

contains_text() | ends_with() | index_of() | last_index_of() | starts_with()