プログラムで文字列をそれらの間のスペースで分割したい
$string = "hello how are you";
出力は次のようになります。
hello
how
are
you
シンプルに思うのですが……。
$string = "hello how are you";
print $_, "\n" for split ' ', $string;
@Array = split(" ",$string);
次に、@Array
答えが含まれています
余分なスペースがある場合は、正規表現で分割します。
my $string = "hello how are you";
my @words = split /\s+/, $string; ## account for extra spaces if any
print join "\n", @words
文字列をスペースで分割するには分割が必要です
use strict;
my $string = "hello how are you";
my @substr = split(' ', $string); # split the string by space
{
local $, = "\n"; # setting the output field operator for printing the values in each line
print @substr;
}
Output:
hello
how
are
you