0

I having one requirement to edit the string and display

Input string :

Invite URL: www.someurl.com id:12345678 dial:+98765432 code:0192837465 AccessCode: 12345 password:abc

some line here

i do not want to show the last 3 lines,

I tried to search the index of empty lines and get the substring from index 0 to empty line index

int index = mystring.indexof(("\\r\\n")); 

mystring.substring(0,index)

however index is always returning -1. my doubt is how to find the index for empty string.

i wanted the substring till AccessCode or Password(Either Both AccessCode and Password will be present or only AccessCode will be present) from InviteURL , any other additional line after that I have to skip

Output expected:

Invite URL: www.someurl.com id:12345678

dial:+98765432

code:0192837465

password:abc

4
  • Read all lines into an ArrayList<String> variable then loop until length - 3? Commented May 19, 2020 at 7:13
  • Using String.trim() or String.strip(), or just Streams.lines to filter the last 3 lines Commented May 19, 2020 at 7:16
  • Refer to this answer: stackoverflow.com/questions/10065885/… Commented May 19, 2020 at 7:17
  • int index = mystring.indexof(("\\r\\n")); be instead of int index = mystring.indexof(("\\n\\n")); Commented May 19, 2020 at 7:38

2 Answers 2

2

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.
Sign up to request clarification or add additional context in comments.

1 Comment

String inputString = "URL: www.someurl.com\n" + "id:12345678\n" + "dial:+98765432\n" + "code:0192837465\n" + "AccessCode:123456\n" + "password:abc\n" + "\n" + "~~~~~~\n" + "some line here\n" + "~~~~~~"; here either both AccessCode and Password will be present or only AccecssCode will present.i need to extract the substring from "URL: www.someurl.com" to AccessCode/Password . how to handle this case
1
    public static void main(String[] args) {
    String myString = "URL: www.someurl.com\n" +
            "id:12345678\n" +
            "dial:+98765432\n" +
            "code:0192837465\n" +
            "password:abc\n" +
            "\n" +
            "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n" +
            "some line here\n" +
            "~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
    String[] split = myString.split("\\n\\n");
    System.out.println(split[0]);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.