code: public static String addQueryStringFragment(String url, String fragment) {
          StringBuilder newUrl = new StringBuilder(url);
  
          if (fragment.startsWith("?") || fragment.startsWith("&")) {
              fragment = fragment.substring(1);
          }
  
          if (!url.endsWith("?") && !url.endsWith("&")) {
              if (url.contains("?")) {
                  newUrl.append('&');
              } else {
                  newUrl.append('?');
              }
          }
  
          return newUrl.append(fragment).toString();
      }
docs: Adds a query string fragment to the URL, adding a '?' if there's no query string yet.
  
  @param url       the URL
  @param fragment  the query string fragment
  
  @return the URL with the query string fragment appended