@Test public void testAppendParameterToUrl() { assertEquals( "http: UrlUtil.appendParameterToUrl("http: ); assertEquals( "http: UrlUtil.appendParameterToUrl("http: ); assertEquals( "123?pageIndex=19", UrlUtil.appendParameterToUrl("123", "pageIndex", "19") ); assertEquals( "http: UrlUtil.appendParameterToUrl("http: ); assertEquals( "http: UrlUtil.appendParameterToUrl("http: ); assertEquals( "http: UrlUtil.appendParameterToUrl("http: ); assertEquals( "http: UrlUtil.appendParameterToUrl("http: ); assertEquals( "http: UrlUtil.appendParameterToUrl( "http: ) ); }
public static String appendParameterToUrl( @Nonnull String url, @Nullable String parameterName, @Nullable String parameterValue) { if (!isValidUri(url) || StringUtil.isBlank(parameterName)) { return url; } String parameter = StringUtil.isBlank(parameterValue) ? parameterName : parameterName + '=' + parameterValue; int questionSignPos = url.indexOf('?'); int sharpPos = url.indexOf('#'); if (questionSignPos == -1 && sharpPos == -1) { return url + '?' + parameter; } else if (questionSignPos == -1 || sharpPos != -1 && questionSignPos > sharpPos) { return url.substring(0, sharpPos) + '?' + parameter + url.substring(sharpPos); } else { StringBuilder resultUrl = new StringBuilder(url.substring(0, questionSignPos + 1)).append(parameter); if (url.length() > questionSignPos + 1) { resultUrl.append('&').append(url.substring(questionSignPos + 1)); } return resultUrl.toString(); } }
UrlUtil { public static String appendParameterToUrl( @Nonnull String url, @Nullable String parameterName, @Nullable String parameterValue) { if (!isValidUri(url) || StringUtil.isBlank(parameterName)) { return url; } String parameter = StringUtil.isBlank(parameterValue) ? parameterName : parameterName + '=' + parameterValue; int questionSignPos = url.indexOf('?'); int sharpPos = url.indexOf('#'); if (questionSignPos == -1 && sharpPos == -1) { return url + '?' + parameter; } else if (questionSignPos == -1 || sharpPos != -1 && questionSignPos > sharpPos) { return url.substring(0, sharpPos) + '?' + parameter + url.substring(sharpPos); } else { StringBuilder resultUrl = new StringBuilder(url.substring(0, questionSignPos + 1)).append(parameter); if (url.length() > questionSignPos + 1) { resultUrl.append('&').append(url.substring(questionSignPos + 1)); } return resultUrl.toString(); } } }
UrlUtil { public static String appendParameterToUrl( @Nonnull String url, @Nullable String parameterName, @Nullable String parameterValue) { if (!isValidUri(url) || StringUtil.isBlank(parameterName)) { return url; } String parameter = StringUtil.isBlank(parameterValue) ? parameterName : parameterName + '=' + parameterValue; int questionSignPos = url.indexOf('?'); int sharpPos = url.indexOf('#'); if (questionSignPos == -1 && sharpPos == -1) { return url + '?' + parameter; } else if (questionSignPos == -1 || sharpPos != -1 && questionSignPos > sharpPos) { return url.substring(0, sharpPos) + '?' + parameter + url.substring(sharpPos); } else { StringBuilder resultUrl = new StringBuilder(url.substring(0, questionSignPos + 1)).append(parameter); if (url.length() > questionSignPos + 1) { resultUrl.append('&').append(url.substring(questionSignPos + 1)); } return resultUrl.toString(); } } private  UrlUtil(); }
UrlUtil { public static String appendParameterToUrl( @Nonnull String url, @Nullable String parameterName, @Nullable String parameterValue) { if (!isValidUri(url) || StringUtil.isBlank(parameterName)) { return url; } String parameter = StringUtil.isBlank(parameterValue) ? parameterName : parameterName + '=' + parameterValue; int questionSignPos = url.indexOf('?'); int sharpPos = url.indexOf('#'); if (questionSignPos == -1 && sharpPos == -1) { return url + '?' + parameter; } else if (questionSignPos == -1 || sharpPos != -1 && questionSignPos > sharpPos) { return url.substring(0, sharpPos) + '?' + parameter + url.substring(sharpPos); } else { StringBuilder resultUrl = new StringBuilder(url.substring(0, questionSignPos + 1)).append(parameter); if (url.length() > questionSignPos + 1) { resultUrl.append('&').append(url.substring(questionSignPos + 1)); } return resultUrl.toString(); } } private  UrlUtil(); @SuppressWarnings("unused") static Set<String> getParameterNames(@Nonnull String url); @SuppressWarnings("IfStatementWithIdenticalBranches") static String removeParameterFromUrl(@Nonnull String url, @Nullable String parameterName); static String replaceParameterInUrl(
            @Nonnull String url, @Nullable String parameterName, @Nullable String parameterValue); static String appendParameterToUrl(
            @Nonnull String url, @Nullable String parameterName, @Nullable String parameterValue); @SuppressWarnings("OverlyComplexMethod") static String appendParametersToUrl(@Nonnull String url, @Nonnull String... parameterParts); @SuppressWarnings("unused") static String appendRelativePathToUrl(@Nonnull String url, @Nullable String relativePath); @SuppressWarnings("BooleanMethodIsAlwaysInverted") @Contract("null -> false") static boolean isValidUrl(@Nullable String url); @Contract("null, _ -> false") static boolean isValidUrl(@Nullable String url, String[] allowedSchemes); @SuppressWarnings({"ResultOfMethodCallIgnored", "BooleanMethodIsAlwaysInverted"}) @Contract("null -> false") static boolean isValidUri(@Nullable String uri); @SuppressWarnings("unused") @Contract("null -> null") @Nullable static String extractFileName(@Nullable String url); }
UrlUtil { public static String appendParameterToUrl( @Nonnull String url, @Nullable String parameterName, @Nullable String parameterValue) { if (!isValidUri(url) || StringUtil.isBlank(parameterName)) { return url; } String parameter = StringUtil.isBlank(parameterValue) ? parameterName : parameterName + '=' + parameterValue; int questionSignPos = url.indexOf('?'); int sharpPos = url.indexOf('#'); if (questionSignPos == -1 && sharpPos == -1) { return url + '?' + parameter; } else if (questionSignPos == -1 || sharpPos != -1 && questionSignPos > sharpPos) { return url.substring(0, sharpPos) + '?' + parameter + url.substring(sharpPos); } else { StringBuilder resultUrl = new StringBuilder(url.substring(0, questionSignPos + 1)).append(parameter); if (url.length() > questionSignPos + 1) { resultUrl.append('&').append(url.substring(questionSignPos + 1)); } return resultUrl.toString(); } } private  UrlUtil(); @SuppressWarnings("unused") static Set<String> getParameterNames(@Nonnull String url); @SuppressWarnings("IfStatementWithIdenticalBranches") static String removeParameterFromUrl(@Nonnull String url, @Nullable String parameterName); static String replaceParameterInUrl(
            @Nonnull String url, @Nullable String parameterName, @Nullable String parameterValue); static String appendParameterToUrl(
            @Nonnull String url, @Nullable String parameterName, @Nullable String parameterValue); @SuppressWarnings("OverlyComplexMethod") static String appendParametersToUrl(@Nonnull String url, @Nonnull String... parameterParts); @SuppressWarnings("unused") static String appendRelativePathToUrl(@Nonnull String url, @Nullable String relativePath); @SuppressWarnings("BooleanMethodIsAlwaysInverted") @Contract("null -> false") static boolean isValidUrl(@Nullable String url); @Contract("null, _ -> false") static boolean isValidUrl(@Nullable String url, String[] allowedSchemes); @SuppressWarnings({"ResultOfMethodCallIgnored", "BooleanMethodIsAlwaysInverted"}) @Contract("null -> false") static boolean isValidUri(@Nullable String uri); @SuppressWarnings("unused") @Contract("null -> null") @Nullable static String extractFileName(@Nullable String url);  }