Skip to main content

String Manipulation

The following string manipulation topics are described in this section:

capitalize

Syntax

capitalize($string)

Description

Returns a string with the first letter of the string capitalized (if it is the first character), and the rest are lower-case.

See also: downcase, upcase

Arguments

PositionPossible ValuesRequiredDefault Value
1String valueYesNone

Result

String

Examples

$string = "hello" 
capitalize($string) == "Hello"
$string = "HeLlO"
capitalize($string) == "Hello"

downcase

Syntax

downcase($string)

Description

Returns a string with all ASCII letters in string changed to lower-case.

See also: capitalize, upcase

Arguments

PositionPossible ValuesRequiredDefault Value
1String valueYesNone

Result

String

Examples

$string = "HELLO" 
downcase($string) == "hello"
$string = "HeLlO"
downcase($string) == "hello"

gsub

Syntax

gsub($string, $pattern, $replace)

See also: sub

Description

Returns a string with the all occurrences of pattern substituted for the replace argument. The pattern is typically a regular expression, but it can be a string.

Arguments

PositionPossible ValuesRequiredDefault Value
1String valueYesNone
2String value -OR- RegexYesNone
3String valueYesNone

Result

String

Examples

$string = "hello there howard" 
gsub($string, "howard", "tom") == "hello there tom"
gsub($string, /e./, "!") == "h!lo th!!howard"

include?

Syntax

include?($string, $other_str)

Description

Returns true if string contains other_str.

Arguments

PositionPossible ValuesRequiredDefault Value
1String valueYesNone
2String valueYesNone

Result

Boolean

Examples

$string = "hello" 
capitalize($string) == "Hello"
$string = "HeLlO"
capitalize($string) == "Hello"

index

Syntax

index($string, $substr[, $offset])

Description

Returns the index of the first occurrence of substr in string. Returns null if not found. If the offset parameter is present, it specifies the position in string to begin the search (can be a negative number which will be relative to the end of the string).

See also: rindex

Arguments

PositionPossible ValuesRequiredDefault Value
1String valueYesNone
2String value -OR- RegexYesNone
3NumberNo0

Result

Number or null

Examples

$string = "hello 123" 
index($string, "he") == 0
index($string, "he", 2) == null
index($string, "23") == 7
index($string, /[1-9]/) == 6

insert

Syntax

insert($string, $index, $insertion_string)

Description

Returns a string with insertion_string inserted before the character at the given index of string. Negative indices count from the end of string, and insert after the given character. If a positive index is given past the end of string, insertion_string will be inserted at the end. If a negative index is given past the beginning of string, insertion_string will be inserted at the beginning.

Arguments

PositionPossible ValuesRequiredDefault ValueComment
1String valueYesNone
2NumberYesNoneThe position at which to insert the new string
3String valueYesNoneThe string value to insert

Result

String

Examples

$string = "hello" 
capitalize($string) == "Hello"
$string = "HeLlO"
capitalize($string) == "Hello"

join

Syntax

join($array[, $separator])

Description

Join elements of array into a single string using given separator if any.

See also: split

Arguments

PositionPossible ValuesRequiredDefault ValueComment
1String valueYesNone
2String valueNo“”Empty string by default

Result

String

Examples

$values = ["some", "dash", "delimited", "string"]
join($values, "-") == "some-dash-delimited-string"
join($values) == "somedashdelimitedstring"

lines

Syntax

lines($string[, $separator])

Description

Returns an array of lines in string split using the supplied separator ($/ by default).

Arguments

PositionPossible ValuesRequiredDefault Value
1String valueYesNone
2String valueNo$/

Result

Array

Examples

$string = "hello\nthere" 
lines($string) == ["hello\n","there"]
lines($string, "e") == ["he", "llo\nthe", "re"]

ljust

Syntax

ljust($string, $padded_length[, $padding_value])

See also: rjust

Description

If padded_length is greater than the length of string, returns a string of length padded_length with string left justified and padded with padding_value; otherwise, returns string.

Arguments

PositionPossible ValuesRequiredDefault Value
1String valueYesNone
2NumberYesNone
3String valueNo“ ”

Result

String

Examples

$string = "hello" 
capitalize($string) == "Hello"
$string = "HeLlO"
capitalize($string) == "Hello"

lstrip

Syntax

lstrip($string)

Description

Returns a string with leading whitespace removed from string.

See also: rstrip, strip

Arguments

PositionPossible ValuesRequiredDefault Value
1String valueYesNone

Result

String

Examples

$string = "  hello" 
lstrip($string) == "hello"

pluralize

Syntax

pluralize($string)

Description

Returns the plural form of the word in the string. Currently only supports the English locale.

See also: singularize

Arguments

PositionPossible ValuesRequiredDefault Value
1String valueYesNone

Result

String

Examples

$singular_type = "cloud" 
pluralize($singular_type) == "clouds"
pluralize("ip_address") == "ip_addresses"

reverse

Syntax

reverse($string)

Description

Returns a string with the characters from string in reverse order.

Arguments

PositionPossible ValuesRequiredDefault Value
1String valueYesNone

Result

String

Examples

$string = "hello" 
capitalize($string) == "Hello"
$string = "HeLlO"
capitalize($string) == "Hello"

rindex

Syntax

rindex($string, $substr[, $offset])

Description

Returns the index of the last occurrence of substr in string. Returns null if not found. If the offset parameter is present, it specifies the position in string to end the search — characters beyond this point will not be considered (can be a negative number which will be relative to the end of the string).

See also: index

Arguments

PositionPossible ValuesRequiredDefault Value
1String valueYesNone
2String value -OR- RegexYesNone
3NumberNoNone

Result

Number or null

Examples

$string = "hello 123 123" 
rindex($string, "3") == 12
rindex($string, "3", 11) == 8
rindex($string, /1./) == 10
rindex($string, "4") == null

rjust

Syntax

rjust($string, $padded_length[, $padding_value])

Description

If padded_length is greater than the length of string, returns a string of length padded_length with string right justified and padded with padding_value; otherwise, returns string.

See also: ljust

Arguments

PositionPossible ValuesRequiredDefault Value
1String valueYesNone
2NumberYesNone
3String valueNo“ ”

Result

String

Examples

$string = "hello" 
capitalize($string) == "Hello"
$string = "HeLlO"
capitalize($string) == "Hello"

rstrip

Syntax

rstrip($string)

Description

Returns a string with trailing whitespace removed from string.

See also: lstrip, strip

Arguments

PositionPossible ValuesRequiredDefault Value
1String valueYesNone

Result

String

Examples

$string = "hello  " 
rstrip($string) == "hello"

singularize

Syntax

singularize($string)

Description

The reverse of pluralize, returns the singular form of a word in a string. Currently only supports the English locale.

See also: pluralize

Arguments

PositionPossible ValuesRequiredDefault Value
1String valueYesNone

Result

String

Examples

$plural_type = "clouds" 
singularize($plural_type) == "cloud"
singularize("ip_addresses") == "ip_address"

split

Syntax

split($string, $separator_or_regexp)

Description

Split given string around matches of the given separator or regular expression.

See also: join

Arguments

PositionPossible ValuesRequiredDefault Value
1String valueYesNone
2String value -OR- RegexYesNone

Result

Array of strings

Examples

$text = "some-dash-delimited--string--" 
$values = split($text, "/-+/") # ["some", "dash", "delimited", "string"]

strip

Syntax

strip($string)

Description

Returns a string with leading and trailing whitespace removed from string.

See also: lstrip, rstrip

Arguments

PositionPossible ValuesRequiredDefault Value
1String valueYesNone

Result

String

Examples

$string = "  hello  " 
lstrip($string) == "hello"

sub

Syntax

sub($string, $pattern, $replace)

Description

Returns a string with the first occurrences of pattern substituted for the replace argument. The pattern is typically a regular expression, but it can be a string.

See also: gsub

Arguments

PositionPossible ValuesRequiredDefault Value
1String valueYesNone
2String value -OR- RegexYesNone
3String valueYesNone

Result

String

Examples

$string = "hello there howard" 
gsub($string, "howard", "tom") == "hello there tom"
gsub($string, /e./, "!") == "h!lo th!!howard"

upcase

Syntax

upcase($string)

Description

Returns a string with all ASCII letters in string converted to upper-case.

See also: capitalize, downcase

Arguments

PositionPossible ValuesRequiredDefault Value
1String valueYesNone

Result

String

Examples

$string = "hello" 
capitalize($string) == "Hello"
$string = "HeLlO"
capitalize($string) == "Hello"