There is a number of ways to accomplish this but the easiest is most likely:
String inputString ="URL: www.someurl.com\n" +
"id:12345678\n" +
"dial:+98765432\n" +
"code:0192837465\n" +
"password:abc\n" +
"\n" +
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n" +
"some line here\n" +
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
if (inputString.contains("\r\n")) {
inputString = inputString.substring(0, inputString.indexOf("\r\n\r\n")).trim();
}
else {
inputString = inputString.substring(0, inputString.indexOf("\n\n")).trim();
}
System.out.println(inputString);
Another simple way to accomplish this is:
String inputString ="URL: www.someurl.com\n" +
"id:12345678\n" +
"dial:+98765432\n" +
"code:0192837465\n" +
"password:abc\n" +
"\n" +
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n" +
"some line here\n" +
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
String[] stringParts = inputString.split("\n\n|\r\n\r\n");
inputString = stringParts[0];
System.out.println(inputString)
And yet another way this can be accomplished is by utilizing the Scanner class:
String inputString ="URL: www.someurl.com\n" +
"id:12345678\n" +
"dial:+98765432\n" +
"code:0192837465\n" +
"password:abc\n" +
"\n" +
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n" +
"some line here\n" +
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
StringBuilder sb = new StringBuilder();
Scanner scan = new Scanner(inputString);
String line;
while (scan.hasNextLine()) {
line = scan.nextLine();
if (line.equals("")) {
break;
}
sb.append(line).append(System.lineSeparator());
}
inputString = sb.toString().trim();
System.out.println(inputString);
If you want to use a Regular Expression (RegEx) along with Pattern/Matcher classes which is part of the java.util.regex package then you can try this:
String inputString = "URL: www.someurl.com\n"
+ "id:12345678\n"
+ "dial:+98765432\n"
+ "code:0192837465\n"
+ "password:abc\n"
+ "\n"
+ "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
+ "some line here\n"
+ "~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
Pattern pattern = Pattern.compile("(.*?\n{2}[\r]?)", Pattern.DOTALL);
Matcher m = pattern.matcher(inputString);
String newString = "";
if (m.find()) {
newString = (m.group().trim());
}
System.out.println(newString);
What is this Regular Expression? - "(.*?\n{2}[\r]?)"
( : Capturing Group #1 (start) - Groups multiple tokens together and
creates a capture group for extracting a substring or using a
backreference. This is the open parentheses indicating the start of a group.
. : Dot - Matches any character except linebreaks.
* : Quantifier - Matches 0 or more of the preceding token.
? : Lazy - Makes the preceding quantifier lazy, causing it to
match as few characters as possible. By default, quantifiers are
greedy, and will match as many characters as possible.
\n : Escaped Character - Matches a LINE FEED character (char
code 10).
{2} : Quantifier - Matches the specified quantity of the
previous token. For example, {1,3} will match 1 to 3. {3} will match
exactly 3. {3,} will match 3 or more.
[ : Character Set (start) - Match any character in the set. This open
Square bracket starts the Character Set.
\r: Escaped Character - Contained within the Character Set -
Matches a CARRIAGE RETURN character (char code 13).
] : **Character Set (end) - Match any character in the set. This
close Square bracket ends the Character Set.
? : Quantifier - Matches 0 or 1 of the preceding token,
effectively making it optional. The preceding token would be the \r
contained within the Character Set.
) : Capturing Group #1 (end) - Groups multiple tokens together
and creates a capture group for extracting a substring or using a
backreference. This is the close parentheses indicating the end of a
group.
String.trim()orString.strip(), or justStreams.linesto filter the last 3 linesint index = mystring.indexof(("\\r\\n"));be instead ofint index = mystring.indexof(("\\n\\n"));