Cynical news

Links
Literature
Music
Programming
Software
Programming
Perl Strings
Perl Strings
Create / delete

Create string $a="";
$a="abcdef";
abcdef

Initialise

Fixed contents $a = "";
$a = "abcdef";
abcdef
Repeated value 'x' x 10 $c='x';
$a=$c x 10;
xxxxxxxxxx

Count

Length of string $a="abc";
$i=length $a;
3
Number of 'X' chars in string $a="abcXdefXX";
$n=($a =~ tr/X//);
3
Number of 'X', 'Y' and 'Z' chars in string $a="abcXdefYZ";
$n=($a =~ tr/XYZ//);
3
Number of 'abc' substrings in string $a="abcabcdefabc";
$n=0;
while ($a =~ /abc/g) { $n++ }
3

Retrieve by index

Retrieve 1st char $a="abc";
$c=substr $a,0,1;
a
Retrieve last char $a="abc";
$c=substr $a, (length $a)-1;
c
Retrieve char 'n' $a="abcdef";
$n=2;
$c=substr $a, $n, 1;
c
Retrieve out-of-bounds char $a="abc";
$n=4;
$c=substr $a, $n; if ( $c eq "" ) { $c="out of range" }
out of range

Slice

Slice from index 'm' to 'n' (inclusive) $a="abcdefghi";
$m=3;
$n=6;
$s=substr $a,$m,($m-$n+1);
defg
Slice from index 'n' (inclusive) $a="abcdefghi";
$n=3;
$s=substr $a,$n;
defghi
Slice up to index 'n' (inclusive) $a="abcdefghi";
$n=3;
$s=substr $a,0,$n+1;
abcd

Compare

Test for equality (case sensitive) $a="abcdef";
$b="abc";
$t="0";
if ($a eq $b) { $t="1"; }
0
Test for equality (case insensitive) $a="abcdef";
$b="ABCdef";
$t="0";
if ($a =~ /^$b$/i ) { $t="1"; }
1
Test for prefix $a="abcdef";
$b="abc";
$t="0";
if ($a=~/^$b.*/) { $t="1"; }
1
Test for suffix $a="abcdef";
$b="def";
$t="0";
if ($a=~/.*$b$/) { $t="1"; }
1

Search

Test char 'is in' string $a="abcdefghi";
$c='c';
$t=(index($a,$c) != -1);
1
Test substring 'is in' string $a="abcdefghi";
$b="abc";
$t=(index($a,$c) != -1);
1
Find index of char (forward) $a="abcdefccccc";
$c='c';
$i=index $a,$c;
2
Find index of char (reverse) $a="abcdefccccc";
$c='c';
$i=rindex $a, $c;
10
Find index of substring (forward) $a="abcdefabc";
$b="abc";
$i=index $a,$b;
0
Find index of substring (reverse) $a="abcdefabc";
$b="abc";
$i=rindex $a,$b;
6

Split

Split on char $a="one,two,three";
$c=',';
@sa=split /$c/,$a;
one two three
Split on string $a="aa_x_bb_x_cc";
$b="_x_";
@sa=split /$b/,$a;
aa bb cc
Split limited to 'n' parts $a="one,two,three";
$c=',';
$n=2;
@sa=split /$c/,$a,$n;
one two,three

Evaluate

Evaluate empty string $a="";
$t=0;
if ($a) { $t=1; }
0
Evaluate non-empty string $a="abcdef";
$t=0;
if ($a) { $t=1; }
1

Insert

Prepend char $a="bcdef";
$c='a';
$a=$c.$a;
abcdef
Append char $a="abc";
$c='d';
$a=$a.$c;
abcd
Insert char at index 'n' $a="abc";
$c='X';
$n=1;
$a=substr($a ,0,$n).$c.substr($a,$n);
aXbc
Prepend string $a="def";
$b="abc";
$a=$b.$a;
abcdef
Append string $a="abc";
$b="def";
$a=$a.$b;
abcdef
Insert string at index 'n' $a="abcghi";
$b="def";
$n=3;
$a=substr($a ,0,$n).$b.substr($a,$n);
abcdefghi
Concatenate 'a' and 'b' $a="abc";
$b="def";
$s=$a.$b;
abcdef

Overwrite

Overwrite char at index 'n' $a="abc";
$n=1;
$c='B';
substr($a,$n,1)=$c;
aBc
Overwrite char at index 'n' $a="abc";
$n=1;
$c='B';
@b=split //,$a; $b[$n]=$c; $a=join '',@b;
aBc
Overwrite substring at index 'n' $a="abcdefghi";
$b="DEF";
$n=3;
substr($a,$n,length $b)=$b;
abcDEFghi

Replace

Replace 1st instance of char 'X' with 'Y' $a="XXXYYYZZZ";
$a=~s/X/Y/;
YXXYYYZZZ
Replace all instances of char 'X' with 'Y' $a="XXXYYYZZZ";
$a=~s/X/Y/g;
YYYYYYZZZ
Replace 1st instance of substring 'abc' with 'XYZ' $a="abcdefabc";
$a=~s/abc/XYZ/;
XYZdefabc
Replace all instances of substring 'abc' with 'XYZ' $a="abcdefabc";
$a=~s/abc/XYZ/g;
XYZdefXYZ

Remove

Remove 1st instance of char 'X' $a="XXXYYYZZZ";
$a=~s/X//;
XXYYYZZZ
Remove all instances of char 'X' $a="XXXYYYZZZ";
$a=~s/X//g;
YYYZZZ
Remove 1st instance of substring 'abc' $a="abcdefabc";
$a=~s/abc//;
defabc
Remove all instances of substring 'abc' $a="abcdefabc";
$a=~s/abc//g;
def
Remove prefix 'abc.' $a="abc.abc.def";
$a=~s/^abc\.//g;
abc.def
Remove suffix '.def' $a="abc.def.def";
$a=~s/\.def$//g;
abc.def

Remove by index

Remove 1st char $a="abcdef";
$a=substr($a,1);
bcdef
Remove last char $a="abcdefghi";
$a=substr($a,0,(length $a)-1);
abcdefgh
Remove char at index 'n' $a="abcdef";
$n=3;
substr($a,$n,1)='';
abcef
Remove chars in index range 'm' to 'n' (inclusive) $a="abcdef";
$m=2;
$n=4;
substr($a ,$m,$n-$m+1)="";
abf
Remove chars from index 'n' onwards (inclusive) $a="abcdefghi";
$n=3;
$a=substr($a,0,$n);
abc
Remove chars up to index 'n' (inclusive) $a="abcdefghi";
$n=3;
$a=substr($a,$n+1);
efghi

Trim

Trim leading white space $a=" abc def ";
$a=~s/^\s+//;
abc def
Trim trailing white space $a=" abc def ";
$a=~s/\s+$//;
abc def
Trim leading and trailing white space $a=" abc def ";
$a=~s/^\s*( .*\S)?\s*$/$1/;
abc def
Trim trailing carriage return $a="abcdef\n";
chomp($a);
abcdef

Sort

Sort in ascending order $a="cafdeb";
@b=split //,$a; @b=sort @b; $a=join '',@b;
abcdef
Sort in descending order $a="cafdeb";
@b=split //,$a; @b=sort @b; @b=reverse @b; $a=join '',@b; print @b;
fedcba
Reverse $a="abcdefghi";
@b=split //,$a; @b=reverse @b; $a=join '',@b;
ihgfedcba

Convert case

Convert to lower case $a="ABCDEFGHI";
$a=lc $a;
abcdefghi
Convert to upper case $a="abcdefghi";
$a=uc $a;
ABCDEFGHI
Convert first char to lower case $a="ABCDEFGHI";
$a=lcfirst $a;
aBCDEFGHI
Convert first char to upper case $a="abcdefghi";
$a=ucfirst $a;
Abcdefghi

www.cynicalsoftware.com
14-11-09@15:37:24