如何获取给定字符串的子字符串直到指定字符的第一次出现?

Mih*_*yan 8 string perl substring

如果是perl的问题.

例如,如果我有"hello.world"和指定的字符是'.'我想要的结果"hello".

Jim*_*vis 26

perldoc -f index:

$x = "hello.world";
$y = substr($x, 0, index($x, '.'));
Run Code Online (Sandbox Code Playgroud)

  • 我并不是将perldoc引用称为"RTFM",而是将其视为"有关更多信息,请参阅:____".:) (3认同)

yko*_*yko 7

使用substr:

my $string = "hello.world";
my $substring = substr($string, 0, index($string, "."));
Run Code Online (Sandbox Code Playgroud)

或者使用正则表达式:

my ($substring2) = $string =~ /(.*)?\./;
Run Code Online (Sandbox Code Playgroud)