Skip to content

String functions

to_uppercase

Converts the string to uppercase lettering. upper is an alias for to_uppercase.

<%= "name".to_uppercase %>
NAME

to_lowercase

Converts the string to lowercase lettering. lower is an alias for to_lowercase.

<%= "NAME".to_lowercase %>
name

trim

Removes leading and trailing spaces and new line characters from the string.

<%= " value ".trim + " ,name ".trim %>
value,name

capitalize

Capitalizes the first letter of the string.

<%= "john".capitalize %>
John

underscore

Converts the string to "snake_case" formatting. to_snake_case is an alias for underscore.

<%= "ClassName".underscore %>
class_name

camelize

Converts the string to "CamelCase" formatting.

<%= "class_name".camelize %>
ClassName

empty

Returns true if the string is empty (length 0). blank and is_empty are aliases for empty.

<%= "".empty %>
true

len

Returns the length of the string.

<%= "hello".len %>
5

urldecode

Replaces percent-encoding in the string with its ASCII character equivalents. Commonly used to send characters with special meaning inside URLs.

<%= "hello%3Dworld".urldecode %>
hello=world

urlencode

Opposite of urldecode. Replaces ASCII characters with special meaning in URLs with percent-encoded strings.

<%= "hello=world".urlencode %>
hello%3Dworld

br

Replaces new line characters in the string with <br>. Also escapes all HTML tags.

<p><%= message %></p>
context!("message" => "Hello Alice\n\n, how are you?")
<p>Hello Alice<br><br>, how are you?</p>

replace

Replaces a value inside the string with another value. sub is an alias for replace.

<p><%= "Apples are tasty".replace("Apples", "Oranges") %></p>
<p>Oranges are tasty</p>

This method accepts all data types, but it does convert them to their string representation before performing the replacement. For example, a string can be replaced with an integer:

<%= "One two three".replace("One", 1) %>
1 two three

title

Converts the string to "Title Case" formatting.

<%= "hello world".title %>
Hello World