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.
4 Answers
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
Comments
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
You can use .split()
String url = "localhost:9005/index.jsp?accountno=20&password=1234&username=abcd";
String queryString = url.split("\\?")[1];
1 Comment
Arvind Kumar Avinash
? 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.