From jse1.4 String - Two split methods are new. A subSequence method has been added, as required by the CharSequence interface, which now implements String. Three additional methods have been added: matches , replaceAll and replaceFirst .
Using Java String.split(String regex, int limit) with Pattern.quote(String s)
The string "boo: and: foo", for example, gives the following results with these parameters:
Regex Limit Result : 2 { "boo", "and:foo" } : 5 { "boo", "and", "foo" } : -2 { "boo", "and", "foo" } o 5 { "b", "", ":and:f", "", "" } o -2 { "b", "", ":and:f", "", "" } o 0 { "b", "", ":and:f" }
String str = "abc?def,ghi?jkl,mno,pqr?stu,vwx?yz"; String quotedText = Pattern.quote( "?" );
I have the same problem in URL parameters. To solve this problem I need to split based on the first ? . Thus, the remaing String contains parameter values, and they need to be split based on & .
String paramUrl = "https://www.google.co.in/search?q=encode+url&oq=encode+url"; String subURL = URLEncoder.encode( paramUrl, "UTF-8"); String myMainUrl = "http://example.com/index.html?url=" + subURL +"&name=chrome&version=56"; System.out.println("Main URL : "+ myMainUrl ); String decodeMainURL = URLDecoder.decode(myMainUrl, "UTF-8"); System.out.println("Main URL : "+ decodeMainURL ); String[] split = decodeMainURL.split(Pattern.quote( "?" ), 2); String[] Parameters = split[1].split("&"); for (String param : Parameters) { System.out.println( param ); }
Run Javascript on the JVM with Rhino / Nashorn "With JavaScripts String.prototype.split :
var str = "abc?def,ghi?jkl,mno,pqr?stu,vwx?yz"; var parts = str.split(','); console.log( parts );