我有这个字符串
String text = "Hello world my " + System.lineSeparator() + "name is Bob";
我也有这句话
String sentence = "world my name is";
我注意到由于行分隔符,您无法在sentence
和text
中找到indexOf()
。是否有简便快捷的方法仍可以通过多行字符串找到sentence
?
编辑:我还需要按原样打印找到的字符串,这意味着,如果在文本中找到System.lineSeparator()
的地方有一个sentence
,则需要使用该分隔符将其打印出来。
I / O的示例:
输入:
Hello world my
name is Bob
输出:
world my
name is
感谢您的所有答复!
谢谢@alfasin
text.replaceAll(System.lineSeparator(), "").indexOf(sentence);
String.indexOf()
似乎对我来说很好。这是一个示例。
https://repl.it/repls/ProperAcademicObject
示例中的代码如下:
String sentence = "Sentence";
String text = "This is a test." + System.lineSeparator() + "one two three." + System.lineSeparator() + sentence;
System.out.println(text);
System.out.println(text.indexOf("one two three"));
System.out.println(text.indexOf(sentence));
输出如下:
This is a test.
one two three.
Sentence
16
31