Bubblegum
view release on metacpan or search on metacpan
lib/Bubblegum/Object/String.pm view on Meta::CPAN
my $string = 'Hello World';
$string->replace('World', 'Universe'); # Hello Universe
$string->replace('world', 'Universe', 'i'); # Hello Universe
$string->replace(qr/world/i, 'Universe'); # Hello Universe
$string->replace(qr/.*/, 'Nada'); # Nada
The replace method performs a smart search and replace operation and returns the
modified string (if any modification occurred). This method optionally takes a
replacement modifier as it's final argument. Note, this operation expects the
2nd argument to be a replacement String. Note, this method modifies the subject.
=head2 reverse
my $string = 'dlrow ,olleH';
$string->reverse; # Hello, world
The reverse method returns a string where the characters in the subject are in
the opposite order.
=head2 rindex
my $string = 'explain the unexplainable';
$string->rindex('explain'); # 14
$string->rindex('explain', 0); # 0
$string->rindex('explain', 21); # 14
$string->rindex('explain', 22); # 14
$string->rindex('explain', 23); # 14
$string->rindex('explain', 20); # 14
$string->rindex('explain', 14); # 0
$string->rindex('explain', 13); # 0
$string->rindex('explain', 0); # 0
$string->rindex('explained'); # -1
The rindex method searches for the argument within the subject and returns the
position of the last occurrence of the argument. This method optionally takes a
second argument which would be the position within the subject to start
searching from (beginning at or before the position). By default, starts
searching from the end of the string.
=head2 snakecase
my $string = 'hello world';
$string->snakecase; # helloWorld
The snakecase method modifies the subject such that it will no longer have any
non-alphanumeric characters and each word (group of alphanumeric characters
separated by 1 or more non-alphanumeric characters) is capitalized. The only
difference between this method and the camelcase method is that this method
ensures that the first character will always be lowercased. Note, this method
modifies the subject.
=head2 split
my $string = 'name, age, dob, email';
$string->split(', '); # ['name', 'age', 'dob', 'email']
$string->split(', ', 2); # ['name', 'age, dob, email']
$string->split(qr/\,\s*/); # ['name', 'age', 'dob', 'email']
$string->split(qr/\,\s*/, 2); # ['name', 'age, dob, email']
The split method splits the subject into a list of strings, separating each
chunk by the argument (string or regexp object), and returns that list as an
array reference. This method optionally takes a second argument which would be
the limit (number of matches to capture). Note, this operation expects the 1st
argument to be a Regexp object or a String.
=head2 strip
my $string = 'one, two, three';
$string->strip; # one, two, three
The strip method returns the subject replacing occurences of 2 or more
whitespaces with a single whitespace. Note, this method modifies the subject.
=head2 titlecase
my $string = 'mr. wellington III';
$string->titlecase; # Mr. Wellington III
The titlecase method returns the subject capitalizing the first character of
each word (group of alphanumeric characters separated by 1 or more whitespaces).
Note, this method modifies the subject.
=head2 to_array
my $string = 'uniform';
$string->to_array; # ['uniform']
The to_array method is used for coercion and simply returns an array reference
where the first element contains the subject.
=head2 to_code
my $string = 'uniform';
$string->to_code; # sub { 'uniform' }
The to_code method is used for coercion and simply returns a code reference
which always returns the subject when called.
=head2 to_hash
my $string = 'uniform';
$string->to_hash; # { 'uniform' => 'uniform' }
The to_hash method is used for coercion and simply returns a hash reference
with a single key and value, having the key and value both contain the subject.
=head2 to_integer
my $string = 'uniform';
$string->to_integer; # 0
$string = '123';
$string->to_integer; # 123
The to_integer method is used for coercion and simply returns the numeric
version of the subject based on whether the subject "looks like a number", if
not, returns 0.
=head2 to_string
( run in 1.939 second using v1.01-cache-2.11-cpan-483215c6ad5 )