String - SEAProject/stdlib GitHub Wiki

String

String is a object-oriented primitive of Scalar that accept only these following types :

  • scalar
  • stdlib::string
  • stdlib::integer
  • stdlib::boolean (casted to TRUE or FALSE Scalar string value).
use stdlib::util; 
use stdlib::string qw(isString);

my $str = stdlib::string->new('hello world!');
print $str->valueOf; # hello world!
print isString($str)->valueOf; # 1
print typeOf $str; // stdlib::string

Direct update

Each primitive Object have a way to directly update his own internal _value. To modify this behavior, update the static variable directUpdate to 0.

use stdlib::string qw(isString);

my $str = stdlib::string->new('hello world');
print $str->concat('!!!')->valueOf; # hello world!!!
print $str->valueOf; # hello world!!!

$stdlib::string::directUpdate->updateValue(0); 
my $str2 = stdlib::string->new('hello world');
print $str2->concat('!!!')->valueOf; # hello world!!!
print $str2->valueOf; # hello world  

methods

updateValue(newValue: Scalar) : String

update the internal Scalar value


valueOf() : Scalar

Return the scalar value of the String object.


length() : Integer

Return the length of the String object. The length Scalar is wrapped into a Integer primitive.

Example

my $a = stdlib::string->new('abc');
print $a->length->valueOf; # 3

clone() : String

Clone the String object into a complete new one with the same value.

my $a = stdlib::string->new('hello world');
my $b = $a->clone;
print $b->valueOf; # hello world

isEqual(value: Scalar | String) : Boolean

Return true or false wrapped in a Boolean primitive if the Scalar/String is equal.

my $a = stdlib::string->new('hello world');
my $b = stdlib::string->new('hello world');
if($a->isEqual($b)->true) {
    # do work here!
}
if($a->isEqual('hello world')->true) {
    # do work here!
}

substr(start: Scalar | Integer = 0,length: Scalar | Integer = 1) : String

The substr() method returns the characters in a string beginning at the specified location through the specified number of characters. 

my $a = stdlib::string->new('hello');
print $a->substr(0,3)->valueOf; # hel

Same as substr method.


last() : String

Return the last char of the string.

my $a = stdlib::string->new('abc');
print $a->last->valueOf; # c 

charAt(index: Scalar | Integer) : String

The charAt() method returns the specified character from a string. The returned value is wrapped into a new std String object.

my $str = stdlib::string->new('abc'); 
print $str->charAt(0)->valueOf; # a

charCodeAt(index: Scalar | Integer) : Integer

The charCodeAt() method returns an integer between 0 and 65535 representing the UTF-16 code unit at the given index.

my $a = stdlib::string->new(' abc');
print $a->charCodeAt(0)->valueOf; # 32 

concat(...args: Scalar[] | String[]) : String | Self

Concat a another String or Scalar value with the current one.

my $str = stdlib::string->new('hello');
$str->concat(' world', stdlib::string->new('!'));
print $str->valueOf; // stdout: hello world!

repeat(repeatCount: Integer = 1): String

Repeat the string x time.


toLowerCase(): String

Lower case the string.


toUpperCase(): String

Lower case the string.