Contains text: Difference between revisions

From Kolmafia
Jump to navigation Jump to search
imported>Bumcheekcity
mNo edit summary
Convert to Template:Function2 format
 
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
 
|function1.param2=substring
function1={{Function|
|function1.param2.type=string
name={{#var:name}}|
|function1.param2.description=Substring to check for
aggregate={{#var:aggregate}}|
|description=
return_type={{#var:return_type}}|
|code1={{CodeSample
return_also={{#var:return_also}}|
  |title=Code Samples
parameter1={{Param|string|str}}|
  |description=This function checks whether the string "Hello World!" contains the word "World".
parameter2={{Param|string|substring}}|
  |code=
p1desc={{pspan|str}} is the string you want to test.|
<syntaxhighlight lang="d">
p2desc={{pspan|substring}} is the substring you want to check is present inside {{pspan|str}}.
boolean string_check()
}}|
{
 
   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 32: Line 24:
}
}
</syntaxhighlight>
</syntaxhighlight>
}}
  }}
{{CodeSample|
|code2={{CodeSample
description=This function tests to see whether a character is in a clan.|
  |description=This function checks whether a character is in a clan.
code=
  |code=
<syntaxhighlight>
<syntaxhighlight lang="d">
boolean in_clan(){
boolean in_clan()
   string character = visit_url("showplayer.php?who=" + my_id().to_string());
{
   if(contains_text(character , "showclan.php?whichclan")){
   string character = visit_url( "showplayer.php?who=" + my_id().to_string() );
   if ( contains_text( character , "showclan.php?whichclan" ) )
  {
       return true;
       return true;
   }
   }
Line 45: Line 39:
}
}
</syntaxhighlight>
</syntaxhighlight>
}}
  }}
{{CodeSample|
|code3={{CodeSample|
description=Or this, more concise version...|
  |description=Or this, more concise version...
code=
  |code=
<syntaxhighlight>
<syntaxhighlight lang="d">
boolean in_clan(){
boolean in_clan()
      return contains_text(visit_url("showplayer.php?who=" + my_id().to_string()), "showclan.php?whichclan");
{
  return contains_text( visit_url( "showplayer.php?who=" + my_id().to_string() ), "showclan.php?whichclan" );
}
}
</syntaxhighlight>
</syntaxhighlight>
}}|
see_also={{SeeAlso|visit_url|my_id|to_string}}|
}}
}}
 
|see_also={{SeeAlso/Substring Search}}
}}</onlyinclude>
[[Category:String Handling Routines]]
[[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()