Counting number of occurrences of a string inside another (Perl)

后端 未结 4 908
孤街浪徒
孤街浪徒 2020-12-01 09:25

What is the fastest way to count the number of times a certain string appears in a bigger one? My best guess would be to replace all instances of that string with nothing, c

4条回答
  •  不知归路
    2020-12-01 09:38

    my $string = "aaaabbabbba";
    my @count = ($string =~ /a/g);
    print @count . "\n";
    

    or

    my $count = ($string =~ s/a/a/g);
    

提交回复
热议问题