1

The URL "localhost:9005/index.jsp?accountno=20&password=1234&username=abcd" is passed as a string to a Java file and from here I need to ignore till "?" and store the rest as a string in java.Can anyone help me with this.

0

4 Answers 4

3

Regex is a good option or you can also manipulate URLs using java.net.URL class:

URL url = new URL("http://" + "localhost:9005/index.jsp?accountno=20&password=1234&username=abcd");

System.out.println(url.getPath()); // prints: /index.jsp
System.out.println(url.getQuery()); // prints: accountno=20&password=1234&username=abcd
Sign up to request clarification or add additional context in comments.

Comments

2

Regex replacement would be one option here:

String url = "localhost:9005/index.jsp?accountno=20&password=1234&username=abcd";
String query = url.replaceAll("^.*?(?:\\?|$)", "");
System.out.println(query);

This prints:

accountno=20&password=1234&username=abcd

Comments

1

String#substring

public class Main {
    public static void main(String[] args) {
        String url = "localhost:9005/index.jsp?accountno=20&password=1234&username=abcd";
        String paramsStr = url.substring(url.indexOf("?") + 1);
        System.out.println(paramsStr);
    }
}

Output:

accountno=20&password=1234&username=abcd

Comments

0

You can use .split()

String url = "localhost:9005/index.jsp?accountno=20&password=1234&username=abcd";
String queryString = url.split("\\?")[1];

1 Comment

? is a reserved character for regex and is used to mark something optional. You need to escape it as \\? otherwise, you will be welcomed with java.util.regex.PatternSyntaxException.

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.