Last active
September 21, 2017 08:02
-
-
Save wyon/9b9d71bdad9a909b6b422c08c222a07e to your computer and use it in GitHub Desktop.
url:String,拼接query:String
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void main(String[] args) throws ParseException { | |
String query = "hello=world"; | |
List<String> list = new ArrayList<>(); | |
String url; | |
url = "http://www.baidu.com"; // no ?, no # | |
list.add(url); url = "http://www.baidu.com?"; // has ?, no # : just ? | |
list.add(url); url = "http://www.baidu.com?&"; // has ?, no # : ?& | |
list.add(url); url = "http://www.baidu.com?query"; // has ?, no # : ?query | |
list.add(url); url = "http://www.baidu.com?query&"; // has ?, no # : ?query& | |
list.add(url); url = "http://www.baidu.com#e"; // no ?, has # : #e | |
list.add(url); url = "http://www.baidu.com#?"; // has ?, has # : invalid url : just ? | |
list.add(url); url = "http://www.baidu.com#?&"; // has ?, has # : invalid url : ?& | |
list.add(url); url = "http://www.baidu.com#?query"; // has ?, has # : invalid url : ?query | |
list.add(url); url = "http://www.baidu.com#?query&"; // has ?, has # : invalid url : ?query& | |
list.add(url); url = "http://www.baidu.com?#e"; // has ?, has # : valid url : just ? | |
list.add(url); url = "http://www.baidu.com?&#e"; // has ?, has # : valid url : ?& | |
list.add(url); url = "http://www.baidu.com?query#e"; // has ?, has # : valid url : ?query | |
list.add(url); url = "http://www.baidu.com?query&#e"; // has ?, has # : valid url : ?query& | |
list.add(url); | |
for (String str : list) { | |
System.out.println(str); | |
System.out.println(addQuery(str, query)); | |
System.out.println(); | |
} | |
} | |
private static String addQuery(String url, String query) { | |
if (TextUtils.isEmpty(url)) { | |
return query; | |
} | |
if (TextUtils.isEmpty(query)) { | |
return url; | |
} | |
StringBuilder stringBuilder = new StringBuilder(url.length() + query.length() + 2); | |
stringBuilder.append(url); | |
final int schemeSeparatorIndex = url.indexOf(':'); | |
final int querySeparatorIndex = url.indexOf('?', schemeSeparatorIndex); | |
int fragmentSeparatorIndex = url.indexOf('#', schemeSeparatorIndex); | |
if (querySeparatorIndex != -1) { // has '?' | |
if (fragmentSeparatorIndex != -1) { // has fragment | |
if (fragmentSeparatorIndex < querySeparatorIndex) { // invalid url | |
// LogManager.w("addQuery", "invalid url: url=" + url + "; addQuery=" + query); | |
if (url.length() - 1 - querySeparatorIndex > 0) { // had old query | |
if (stringBuilder.charAt(stringBuilder.length() - 1) != '&') { | |
stringBuilder.append('&'); | |
} | |
stringBuilder.append(query); | |
} else { | |
stringBuilder.append(query); | |
} | |
} else { // insert query [query, fragment] | |
if (fragmentSeparatorIndex - querySeparatorIndex > 1) { // has old query | |
if (stringBuilder.charAt(fragmentSeparatorIndex - 1) != '&') { | |
stringBuilder.insert(fragmentSeparatorIndex, '&'); | |
fragmentSeparatorIndex++; | |
} | |
stringBuilder.insert(fragmentSeparatorIndex, query); | |
} else { // dont has old query, just a '?' | |
stringBuilder.insert(querySeparatorIndex + 1, query); | |
} | |
} | |
} else { // has '?', dont has fragment | |
if (stringBuilder.length() - 1 != querySeparatorIndex) { // has old query | |
if (stringBuilder.charAt(stringBuilder.length() - 1) != '&') { | |
stringBuilder.append('&'); | |
} | |
stringBuilder.append(query); | |
} else { // dont has old query, just '?' | |
stringBuilder.append(query); | |
} | |
} | |
} else { // dont has '?' | |
if (fragmentSeparatorIndex != -1) { // has fragment | |
stringBuilder.insert(fragmentSeparatorIndex, '?'); | |
fragmentSeparatorIndex++; | |
stringBuilder.insert(fragmentSeparatorIndex, query); | |
} else { | |
stringBuilder.append('?').append(query); | |
} | |
} | |
return stringBuilder.toString(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment