Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
假设我有一个句子/段落:
This cat is very cute.
在这里,“可爱”是句子中的第 5 个词。如果我知道这个词的第一个字母的索引——在这种情况下c是 17——我怎样才能找出这个词在句子中的位置?
c
计算子字符串中的空格数,然后将其加 1 可能会起作用。
#!/usr/bin/perl use strict; use warnings; my $in = "This cat is very cute."; my $sub = substr $in, 0, 17; my $word_count = scalar(split " ", $sub) + 1; print "$word_count\n";