The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Venus::String - String Class

ABSTRACT

String Class for Perl 5

SYNOPSIS

  package main;

  use Venus::String;

  my $string = Venus::String->new('hello world');

  # $string->camelcase;

DESCRIPTION

This package provides methods for manipulating string data.

INHERITS

This package inherits behaviors from:

Venus::Kind::Value

METHODS

This package provides the following methods:

append

  append(Str @parts) (Str)

The append method appends arugments to the string using spaces.

Since 0.01

append example 1
  # given: synopsis;

  my $append = $string->append('welcome');

  # "hello world welcome"

camelcase

  camelcase() (Str)

The camelcase method converts the string to camelcase.

Since 0.01

camelcase example 1
  # given: synopsis;

  my $camelcase = $string->camelcase;

  # "HelloWorld"

chomp

  chomp() (Str)

The chomp method removes the newline (or the current value of $/) from the end of the string.

Since 0.01

chomp example 1
  package main;

  use Venus::String;

  my $string = Venus::String->new("name, age, dob, email\n");

  my $chomp = $string->chomp;

  # "name, age, dob, email"
chomp example 2
  package main;

  use Venus::String;

  my $string = Venus::String->new("name, age, dob, email\n\n");

  my $chomp = $string->chomp;

  # "name, age, dob, email\n"

chop

  chop() (Str)

The chop method removes and returns the last character of the string.

Since 0.01

chop example 1
  package main;

  use Venus::String;

  my $string = Venus::String->new("this is just a test.");

  my $chop = $string->chop;

  # "this is just a test"

concat

  concat(Str @parts) (Str)

The concat method returns the string with the argument list appended to it.

Since 0.01

concat example 1
  package main;

  use Venus::String;

  my $string = Venus::String->new('ABC');

  my $concat = $string->concat('DEF', 'GHI');

  # "ABCDEFGHI"

contains

  contains(Str $expr) (Bool)

The contains method searches the string for a substring or expression returns true or false if found.

Since 0.01

contains example 1
  package main;

  use Venus::String;

  my $string = Venus::String->new('Nullam ultrices placerat.');

  my $contains = $string->contains('trices');

  # 1
contains example 2
  package main;

  use Venus::String;

  my $string = Venus::String->new('Nullam ultrices placerat.');

  my $contains = $string->contains('itrices');

  # 0
contains example 3
  package main;

  use Venus::String;

  my $string = Venus::String->new('Nullam ultrices placerat.');

  my $contains = $string->contains(qr/trices/);

  # 1

default

  default() (Str)

The default method returns the default value, i.e. ''.

Since 0.01

default example 1
  # given: synopsis;

  my $default = $string->default;

  # ""

hex

  hex() (Str)

The hex method returns the value resulting from interpreting the string as a hex string.

Since 0.01

hex example 1
  package main;

  use Venus::String;

  my $string = Venus::String->new('0xaf');

  my $hex = $string->hex;

  # 175

index

  index(Str $substr, Int $start) (Str)

The index method searches for the argument within the string and returns the position of the first occurrence of the argument.

Since 0.01

index example 1
  package main;

  use Venus::String;

  my $string = Venus::String->new('unexplainable');

  my $index = $string->index('explain');

  # 2
index example 2
  package main;

  use Venus::String;

  my $string = Venus::String->new('unexplainable');

  my $index = $string->index('explain', 1);

  # 2
index example 3
  package main;

  use Venus::String;

  my $string = Venus::String->new('unexplainable');

  my $index = $string->index('explained');

  # -1

lc

  lc() (Str)

The lc method returns a lowercased version of the string.

Since 0.01

lc example 1
  package main;

  use Venus::String;

  my $string = Venus::String->new('Hello World');

  my $lc = $string->lc;

  # "hello world"

lcfirst

  lcfirst() (Str)

The lcfirst method returns a the string with the first character lowercased.

Since 0.01

lcfirst example 1
  package main;

  use Venus::String;

  my $string = Venus::String->new('Hello World');

  my $lcfirst = $string->lcfirst;

  # "hello World"

length

  length() (Int)

The length method returns the number of characters within the string.

Since 0.01

length example 1
  # given: synopsis;

  my $length = $string->length;

  # 11

lines

  lines() (ArrayRef[Str])

The lines method returns an arrayref of parts by splitting on 1 or more newline characters.

Since 0.01

lines example 1
  # given: synopsis;

  my $lines = $string->lines;

  # ["hello world"]
lines example 2
  package main;

  use Venus::String;

  my $string = Venus::String->new("who am i?\nwhere am i?\nhow did I get here");

  my $lines = $string->lines;

  # ["who am i?", "where am i?", "how did I get here"]

lowercase

  lowercase() (Str)

The lowercase method is an alias to the lc method.

Since 0.01

lowercase example 1
  package main;

  use Venus::String;

  my $string = Venus::String->new('Hello World');

  my $lowercase = $string->lowercase;

  # "hello world"

render

  render(HashRef $tokens) (Str)

The render method treats the string as a template and performs a simple token replacement using the argument provided.

Since 0.01

render example 1
  package main;

  use Venus::String;

  my $string = Venus::String->new('Hi, {name}!');

  my $render = $string->render({name => 'Friend'});

  # "Hi, Friend!"

replace

  replace(Regexp $regexp, Str $replace, Str $flags) (Replace)

The replace method performs a search and replace operation and returns the Venus::Replace object.

Since 0.01

replace example 1
  # given: synopsis;

  my $replace = $string->replace('world', 'universe');

  # bless({
  #   ...,
  #   "flags"   => "",
  #   "regexp"  => "world",
  #   "string"  => "hello world",
  #   "substr"  => "universe",
  # }, "Venus::Replace")

reverse

  reverse() (Str)

The reverse method returns a string where the characters in the string are in the opposite order.

Since 0.01

reverse example 1
  # given: synopsis;

  my $reverse = $string->reverse;

  # "dlrow olleh"

rindex

  rindex(Str $substr, Int $start) (Str)

The rindex method searches for the argument within the string and returns the position of the last occurrence of the argument.

Since 0.01

rindex example 1
  package main;

  use Venus::String;

  my $string = Venus::String->new('explain the unexplainable');

  my $rindex = $string->rindex('explain');

  # 14
rindex example 2
  package main;

  use Venus::String;

  my $string = Venus::String->new('explain the unexplainable');

  my $rindex = $string->rindex('explained');

  # -1
rindex example 3
  package main;

  use Venus::String;

  my $string = Venus::String->new('explain the unexplainable');

  my $rindex = $string->rindex('explain', 21);

  # 14
  search(Regexp $regexp) (Search)

The search method performs a search operation and returns the Venus::Search object.

Since 0.01

search example 1
  # given: synopsis;

  my $search = $string->search('world');

  # bless({
  #   ...,
  #   "flags"   => "",
  #   "regexp"  => "world",
  #   "string"  => "hello world",
  # }, "Venus::Search")

snakecase

  snakecase() (Str)

The snakecase method converts the string to snakecase.

Since 0.01

snakecase example 1
  # given: synopsis;

  my $snakecase = $string->snakecase;

  # "hello_world"

split

  split(Str | Regexp $expr, Maybe[Int] $limit) (ArrayRef)

The split method returns an arrayref by splitting the string on the argument.

Since 0.01

split example 1
  package main;

  use Venus::String;

  my $string = Venus::String->new('name, age, dob, email');

  my $split = $string->split(', ');

  # ["name", "age", "dob", "email"]
split example 2
  package main;

  use Venus::String;

  my $string = Venus::String->new('name, age, dob, email');

  my $split = $string->split(', ', 2);

  # ["name", "age, dob, email"]
split example 3
  package main;

  use Venus::String;

  my $string = Venus::String->new('name, age, dob, email');

  my $split = $string->split(qr/\,\s*/);

  # ["name", "age", "dob", "email"]

strip

  strip() (Str)

The strip method returns the string replacing occurences of 2 or more whitespaces with a single whitespace.

Since 0.01

strip example 1
  package main;

  use Venus::String;

  my $string = Venus::String->new('one,  two,  three');

  my $strip = $string->strip;

  # "one, two, three"

titlecase

  titlecase() (Str)

The titlecase method returns the string capitalizing the first character of each word.

Since 0.01

titlecase example 1
  # given: synopsis;

  my $titlecase = $string->titlecase;

  # "Hello World"

trim

  trim() (Str)

The trim method removes one or more consecutive leading and/or trailing spaces from the string.

Since 0.01

trim example 1
  package main;

  use Venus::String;

  my $string = Venus::String->new('   system is   ready   ');

  my $trim = $string->trim;

  # "system is   ready"

uc

  uc() (Str)

The uc method returns an uppercased version of the string.

Since 0.01

uc example 1
  # given: synopsis;

  my $uc = $string->uc;

  # "HELLO WORLD"

ucfirst

  ucfirst() (Str)

The ucfirst method returns a the string with the first character uppercased.

Since 0.01

ucfirst example 1
  # given: synopsis;

  my $ucfirst = $string->ucfirst;

  # "Hello world"

uppercase

  uppercase() (Str)

The uppercase method is an alias to the uc method.

Since 0.01

uppercase example 1
  # given: synopsis;

  my $uppercase = $string->uppercase;

  # "HELLO WORLD"

words

  words() (ArrayRef[Str])

The words method returns an arrayref by splitting on 1 or more consecutive spaces.

Since 0.01

words example 1
  package main;

  use Venus::String;

  my $string = Venus::String->new(
    'is this a bug we\'re experiencing'
  );

  my $words = $string->words;

  # ["is", "this", "a", "bug", "we're", "experiencing"]

OPERATORS

This package overloads the following operators:

operation: (.)

This package overloads the . operator.

example 1

  # given: synopsis;

  my $text = $string . ', welcome';

  # "hello world, welcome"
operation: (eq)

This package overloads the eq operator.

example 1

  # given: synopsis;

  my $result = $string eq 'hello world';

  # 1

example 2

  package main;

  use Venus::String;

  my $string1 = Venus::String->new('hello world');
  my $string2 = Venus::String->new('hello world');

  my $result = $string1 eq $string2;

  # 1
operation: (ne)

This package overloads the ne operator.

example 1

  # given: synopsis;

  my $result = $string ne 'Hello world';

  1;

example 2

  package main;

  use Venus::String;

  my $string1 = Venus::String->new('hello world');
  my $string2 = Venus::String->new('Hello world');

  my $result = $string1 ne $string2;

  # 1
operation: (qr)

This package overloads the qr operator.

example 1

  # given: synopsis;

  my $test = 'hello world' =~ qr/$string/;

  # 1

AUTHORS

Cpanery, cpanery@cpan.org

LICENSE

Copyright (C) 2021, Cpanery

Read the "license" file.