さまざまな文字列操作

明解C言語 入門編 > 11. 文字列とポインタ >

さまざまな文字列操作

Perl
# strcat 
$st1 = "1234567890";
$st2 = "ABCDE";

$st3 = $st1 . $st2;
printf("%s + ", $st1);
printf("%s = ", $st2);
printf("%s\n", $st3);

$st3 = join('', ($st1, $st2));
printf("%s + ", $st1);
printf("%s = ", $st2);
printf("%s\n", $st3);
printf("\n");

# strrev 
printf("%s\n", $st3);
@ary = split(//, $st3);
@ary = reverse(@ary);
$st3 = join('', @ary);
printf("%s\n", $st3);
printf("\n");



# strcmp 
$pt1 = "123";
$pt2 = "123";

$i = ($pt1 cmp $pt2);
if ($i == 0)
{
    printf("%s = %s (%d)\n", $pt1, $pt2, $i);
}
elsif ($i > 0)
{
    printf("%s > %s (%d)\n", $pt1, $pt2, $i);
}
else
{
    printf("%s < %s (%d)\n", $pt1, $pt2, $i);
}

$pt2 = "122";
$i = ($pt1 cmp $pt2);
if ($i == 0)
{
    printf("%s = %s (%d)\n", $pt1, $pt2, $i);
}
elsif ($i > 0)
{
    printf("%s > %s (%d)\n", $pt1, $pt2, $i);
}
else
{
    printf("%s < %s (%d)\n", $pt1, $pt2, $i);
}

$pt2 = "124";
$i = ($pt1 cmp $pt2);
if ($i == 0)
{
    printf("%s = %s (%d)\n", $pt1, $pt2, $i);
}
elsif ($i > 0)
{
    printf("%s > %s (%d)\n", $pt1, $pt2, $i);
}
else
{
    printf("%s < %s (%d)\n", $pt1, $pt2, $i);
}

$pt2 = "999";
$i = ($pt1 cmp $pt2);
if ($i == 0)
{
    printf("%s = %s (%d)\n", $pt1, $pt2, $i);
}
elsif ($i > 0)
{
    printf("%s > %s (%d)\n", $pt1, $pt2, $i);
}
else
{
    printf("%s < %s (%d)\n", $pt1, $pt2, $i);
}

$pt2 = "12";
$i = ($pt1 cmp $pt2);
if ($i == 0)
{
    printf("%s = %s (%d)\n", $pt1, $pt2, $i);
}
elsif ($i > 0)
{
    printf("%s > %s (%d)\n", $pt1, $pt2, $i);
}
else
{
    printf("%s < %s (%d)\n", $pt1, $pt2, $i);
}

$pt2 = "1234";
$i = ($pt1 cmp $pt2);
if ($i == 0)
{
    printf("%s = %s (%d)\n", $pt1, $pt2, $i);
}
elsif ($i > 0)
{
    printf("%s > %s (%d)\n", $pt1, $pt2, $i);
}
else
{
    printf("%s < %s (%d)\n", $pt1, $pt2, $i);
}

print "\n";


# strstr 
$pt1 = "1234567890";
$pt2 = "45";
$i = index($pt1, $pt2);
printf("index(\"%s\", \"%s\") = %d\n", $pt1, $pt2, $i);

$pt2 = "12";
$i = index($pt1, $pt2);
printf("index(\"%s\", \"%s\") = %d\n", $pt1, $pt2, $i);

$pt2 = "123456780";
$i = index($pt1, $pt2);
printf("index(\"%s\", \"%s\") = %d\n", $pt1, $pt2, $i);

print "\n";


# strrchr 
$c = "9";
$i = rindex($pt1, $c);
printf("rindex(\"%s\", \"%s\") = %s\n", $pt1, $c, $i);

$c = "A";
$i = rindex($pt1, $c);
printf("rindex(\"%s\", \"%s\") = %s\n", $pt1, $c, $i);

print "\n";



# s
$pt1 = "aabbccabcAABBCCABC";
print $pt1, "\n";

$pt1 =~ s/a/1/; # "a" を "1" に
print "s/a/1/ = ", $pt1, "\n";

$pt1 = "aabbccabcAABBCCABC";
$pt1 =~ s/a/1/g; # "a" を "1" に
print "s/a/1/g = ", $pt1, "\n";

$pt1 = "aabbccabcAABBCCABC";
$pt1 =~ s/a/1/gi; # "a" を "1" に
print "s/a/1/gi = ", $pt1, "\n";

$pt1 = "aabbccabcAABBCCABC";
$pt1 =~ s/abc/123/gi; # "abc" を "123" に
print "s/abc/123/gi = ", $pt1, "\n";

$pt1 = "aabbccabcAABBCCABC";
$pt1 =~ s/abc/12/gi; # "abc" を "12" に
print "s/abc/12/gi = ", $pt1, "\n";

$pt1 = "aabbccabcAABBCCABC";
$pt1 =~ s/ac/13/gi; # "ac" を "13" に
print "s/ac/13/gi = ", $pt1, "\n";
print "\n";


$pt1 = "aabbccabcAABBCCABC";
$pt1 =~ tr/a/1/; # "a" を "1" に
print "tr/a/1/ = ", $pt1, "\n";

$pt1 = "aabbccabcAABBCCABC";
$pt1 =~ tr/a/1/c; # "a"以外 を "1" に
print "tr/a/1/c = ", $pt1, "\n";

$pt1 = "aabbccabcAABBCCABC";
$pt1 =~ tr/a/1/d; # "a" を "1" に
print "tr/a/1/d = ", $pt1, "\n";

$pt1 = "aabbccabcAABBCCABC";
$pt1 =~ tr/a/1/s; # "a"の連続を "1" に
print "tr/a/1/s = ", $pt1, "\n";

$pt1 = "aabbccabcAABBCCABC";
$pt1 =~ tr/abc/123/; # "a", "b", "c" を "1", "2", "3" に
print "tr/abc/123/ = ", $pt1, "\n";

$pt1 = "aabbccabcAABBCCABC";
$pt1 =~ tr/abc/1/c; # "a", "b", "c" 以外 を "1" に
print "tr/abc/1/c = ", $pt1, "\n";

$pt1 = "aabbccabcAABBCCABC";
$pt1 =~ tr/abc/123/d; # "a", "b", "c" を "1", "2", "3" に
print "tr/abc/123/d = ", $pt1, "\n";

$pt1 = "aabbccabcAABBCCABC";
$pt1 =~ tr/abc/12/d; # "a", "b" を "1", "2" に。"c" は 削除
print "tr/abc/12/d = ", $pt1, "\n";

$pt1 = "aabbccabcAABBCCABC";
$pt1 =~ tr/abc/123/s; # "a"の連続を "1" に。 "b"の連続を "2" に。 "c"の連続を "3" に。
print "tr/abc/123/s = ", $pt1, "\n";

実行結果

L:\>perl lesson_11_087.pl
1234567890 + ABCDE = 1234567890ABCDE
1234567890 + ABCDE = 1234567890ABCDE

1234567890ABCDE
EDCBA0987654321

123 = 123 (0)
123 > 122 (1)
123 < 124 (-1)
123 < 999 (-1)
123 > 12 (1)
123 < 1234 (-1)

index("1234567890", "45") = 3
index("1234567890", "12") = 0
index("1234567890", "123456780") = -1

rindex("1234567890", "9") = 8
rindex("1234567890", "A") = -1

aabbccabcAABBCCABC
s/a/1/ = 1abbccabcAABBCCABC
s/a/1/g = 11bbcc1bcAABBCCABC
s/a/1/gi = 11bbcc1bc11BBCC1BC
s/abc/123/gi = aabbcc123AABBCC123
s/abc/12/gi = aabbcc12AABBCC12
s/ac/13/gi = aabbccabcAABBCCABC

tr/a/1/ = 11bbcc1bcAABBCCABC
tr/a/1/c = aa1111a11111111111
tr/a/1/d = 11bbcc1bcAABBCCABC
tr/a/1/s = 1bbcc1bcAABBCCABC
tr/abc/123/ = 112233123AABBCCABC
tr/abc/1/c = aabbccabc111111111
tr/abc/123/d = 112233123AABBCCABC
tr/abc/12/d = 112212AABBCCABC
tr/abc/123/s = 123123AABBCCABC