Search and compare

Search and compare methods

equals

Checks if two strings contain the same sequence of characters.

signature

/**
 * @param string|self $text
 * @param bool $ignoreCase Optional
 * @return bool
 */
public function equals($text, bool $ignoreCase = false): bool;

usage

if($str->equals('abcd')){
    //...
}

// Case insensitive checking
if($str->equals('aBcD', true)){
    //...
}

compareTo

Compares the current string with a given string and returns 0 if the strings are equal, 1 if the current string is greater and -1 otherwise.

signature

/**
 * @param string|self $text
 * @param bool $ignoreCase Optional
 * @return int
 */
public function compareTo($text, bool $ignoreCase = false): int;

usage

echo $str->compareTo('abcd'); //> 1
echo $str->compareTo('aBcD', true); //> 1

contains

Checks if the current string contains the given substring.

signature

/**
 * @param string|self $text
 * @param bool $ignoreCase Optional
 * @return bool
 */
public function contains($text, bool $ignoreCase = false): bool;

usage

if($str->contains('abcd')){
    //...
}

// Case insensitive checking
if($str->contains('aBcD', true)){
    //...
}

startsWith

Checks if the current string starts with the given substring.

signature

/**
 * @param string|self $text
 * @param bool $ignoreCase Optional
 * @return bool
 */
public function startsWith($text, bool $ignoreCase = false): bool;

usage

if($str->startsWith('abcd')){
    //...
}

// Case insensitive checking
if($str->startsWith('aBcD', true)){
    //...
}

endsWith

Checks if the current string ends with the given substring.

signature

/**
 * @param string|self $text
 * @param bool $ignoreCase Optional
 * @return bool
 */
public function endsWith($text, bool $ignoreCase = false): bool;

usage

if($str->endsWith('abcd')){
    //...
}

// Case insensitive checking
if($str->endsWith('aBcD', true)){
    //...
}

indexOf

Finds the first occurrence of the given substring within the current string. If no occurrence was found, false is returned.

signature

/**
 * @param string|self $text
 * @param int $offset Optional
 * @param bool $ignoreCase Optional
 * @return int|bool
 */
public function indexOf($text, int $offset, bool $ignoreCase = false);

usage

use Opis\String\UnicodeString as wstring;

echo wstring::from('abcabc')->indexOf('a'); //> 0
echo wstring::from('abcabc')->indexOf('a', 2); //> 3
echo wstring::from('abcabc')->indexOf('Ca', 2, false); //> 2

lastIndexOf

Finds the last occurrence of the given substring within the current string. If no occurrence was found, false is returned.

signature

/**
 * @param string|self $text
 * @param int $offset Optional
 * @param bool $ignoreCase Optional
 * @return int|bool
 */
public function lastIndexOf($text, int $offset, bool $ignoreCase = false);

usage

use Opis\String\UnicodeString as wstring;

echo wstring::from('AbcAbcabc')->lastIndexOf('A'); //> 3

// Case insensitive
echo wstring::from('AbcAbcabc')->lastIndexOf('A', 0, true); //> 6