Skip to content

Instantly share code, notes, and snippets.

@libitte
Created June 21, 2013 13:37
Show Gist options
  • Select an option

  • Save libitte/5831202 to your computer and use it in GitHub Desktop.

Select an option

Save libitte/5831202 to your computer and use it in GitHub Desktop.
substr 文字列から指定した文字数だけ抜き出す
#!/usr/bin/perl
use strict;
use warnings;
my $str = "0123456789abcdefghijklmnopqrstuvwxyz";
# 0番目の要素から10文字抜き出す
$left10 = substr($str, 0, 10);
printf "LEFT 10 character: %s\n", $left10;
# 右端から10文字目のところから10文字抜き出す
$right10 = substr($str, length($str) - 10, 10);
printf "RIGHT 10 character): %s\n", $right10;
# 右端から20文字目のところから右端まですべて抜き出す
$right20 = substr($str, length($str) - 20);
printf "RIGHT 20 character: %s\n", $right20;
__END__
LEFT 10 character: 0123456789
RIGHT 10 character): qrstuvwxyz
RIGHT 20 character: ghijklmnopqrstuvwxyz
@libitte
Copy link
Author

libitte commented Jun 21, 2013

基本形

substr(文字列, 開始位置, 切り出す文字数);

切り出す文字数は省略可能で、省略すると開始位置から最後までが切り出し対象となる。

substr(文字列, 開始位置);

参考url: [http://ll.just4fun.biz/perl/%E3%82%B5%E3%83%B3%E3%83%97%E3%83%AB/%E6%96%87%E5%AD%97%E5%88%97%E3%81%AE%E5%88%87%E3%82%8A%E5%87%BA%E3%81%97%E3%82%92%E3%81%99%E3%82%8B%E6%96%B9%E6%B3%95%E3%83%BBsubstr.html]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment