编程语言
首页 > 编程语言> > 如何在java中查找字符串中的全字索引

如何在java中查找字符串中的全字索引

作者:互联网

我想找出给定字符串中所有单词的起始索引.
让我们说下面给出了一个字符串.

“an ancient manuscripts, another means to divide sentences into
paragraphs was a line break (newline) followed by an initial at the
beginning of the next paragraph. An initial is an oversize capital
letter, sometimes outdented beyond the margin of text. This style can
be seen, for example, in the original Old English manuscript of
Beowulf. Outdenting is still used in English typography, though not
commonly.[4] Modern English typography usually indicates a new
paragraph by indenting the first line”); “

我想找出“段落”的起始索引.其中不应包括“段落”,“段落”.

谁能想出如何在java中做到这一点.
提前致谢.

解决方法:

您可以使用word boundaries character的正则表达式:

String text = "an ancient manuscripts, another means to divide sentences into paragraphs was a line break (newline) followed by an initial at the beginning of the next paragraph. An initial is an oversize capital letter, sometimes outdented beyond the margin of text. This style can be seen, for example, in the original Old English manuscript of Beowulf. Outdenting is still used in English typography, though not commonly.[4] Modern English typography usually indicates a new paragraph by indenting the first line";

Matcher m = Pattern.compile("\\bparagraph\\b").matcher(text);
while (m.find()) {
    System.out.println("Matching at: " + m.start());
}

如果你不想要“段落”. (“段落”后跟一个点),你可以试试

Matcher m = Pattern.compile("\\bparagraph($| )").matcher(text);

这意味着段落后跟空格或行尾.

如果您要查找的字符串可以包含特殊字符(如“(”),则可以使用Pattern.quote()来转义它:

String mySearchString = "paragraph";
Matcher m = Pattern.compile("\\b" + Pattern.quote(mySearchString) + "($| )").matcher(text);

标签:indexof,java,string,lastindexof
来源: https://codeday.me/bug/20191003/1847376.html