0

I have this String

String text = "Hello world my " + System.lineSeparator() + "name is Bob";

I also have this sentence

String sentence = "world my name is";

I have noticed that you can't find sentence in text with indexOf() because of the line separator. Is there and easy and fast way to still find sentence over a multi-line string?

EDIT: I also need to print the string that I found as it was, meaning, If there was a System.lineSeparator() in the text where I found sentence, I need to print it out with that separator.

Example of i/o:

Input:

Hello world my
name is Bob

Output:

world my
name is

Thank you for all the replies!

BobLee
  • 52
  • 6

1 Answers1

2

Thank you @alfasin

text.replaceAll(System.lineSeparator(), "").indexOf(sentence);
BobLee
  • 52
  • 6
  • I wold add replaceAll(System.lineSeparator(), "") also to sentence. – Tamir Adler Nov 06 '19 at 22:00
  • Use caution with this - if you use it on a text file that uses `\r\n` (Windows) for new lines but run it on an operating system that uses `\n` (Unix environments) (or the other way around) then it won't work. See also [this post](https://stackoverflow.com/questions/3219014/what-is-a-cross-platform-regex-for-removal-of-line-breaks) – stdunbar Nov 06 '19 at 22:01