Java StringBuilder.indexOf Examples

Java StringBuilder.indexOf - 30 examples found. These are the top rated real world Java examples of StringBuilder.indexOf extracted from open source projects. You can rate examples to help us improve the quality of examples.
 public static URL container(String resource, ClassLoader cl) {
   final URL resUrl = cl.getResource(resource);
   if (resUrl == null) {
     throw new IllegalArgumentException("Resource not found: " + resource);
   }
   final StringBuilder url = new StringBuilder(resUrl.toExternalForm());
   try {
     if ("jar".equalsIgnoreCase(resUrl.getProtocol())) {
       url.delete(0, 4).delete(url.indexOf(resource) - 2, url.length());
     } else if ("file".equalsIgnoreCase(resUrl.getProtocol())) {
       url.delete(url.indexOf(resource), url.length());
     } else {
       throw new UnsupportedOperationException(
           "Cannot get container for resource ["
               + resource
               + "]. Unsupported URL protocol ["
               + resUrl.getProtocol()
               + "].");
     }
     return new URL(url.toString());
   } catch (MalformedURLException e) {
     throw new UnsupportedOperationException(
         "Cannot get container for resource [" + resource + "]. Malformed URL [" + url + "].");
   }
 }
  private static String refactorString(final String string) {
    // Avoid error when content is "".
    if (string.compareTo("") == 0) {
      return " ";
    }

    /*
     * If the font of "\n" is "" or "\n", insert " " in front of it. For
     * example: "\nabc" -> " \nabc" "\nabc\n\n" -> " \nabc\n \n".
     */
    final StringBuilder strBuilder = new StringBuilder(string);
    int start = 0;
    int index = strBuilder.indexOf("\n");
    while (index != -1) {
      if (index == 0 || strBuilder.charAt(index - 1) == '\n') {
        strBuilder.insert(start, " ");
        start = index + 2;
      } else {
        start = index + 1;
      }

      if (start > strBuilder.length() || index == strBuilder.length()) {
        break;
      }

      index = strBuilder.indexOf("\n", start);
    }

    return strBuilder.toString();
  }
Example #3
0
  @Override
  public void saveConfig() {
    String str = this.getConfig().saveToString();
    StringBuilder sb = new StringBuilder(str);

    sb.insert(
        sb.indexOf("\nversion:") + 1,
        "\n# Configuration version used during upgrade. Do not change.\n");

    sb.insert(
        sb.indexOf("\nmute_minutes") + 1,
        "\n# The default number of minutes a player will remain muted.\n");

    sb.insert(
        sb.indexOf("\nprotect_boats") + 1,
        "\n# When true, protects boats from breakage unless deliberately broken.\n");

    final File cfg_file = new File(this.getDataFolder(), "config.yml");
    final String cfg_str = sb.toString();
    final Logger logger = this.getLogger();

    new BukkitRunnable() {
      @Override
      public void run() {
        cfg_file.delete();
        try (PrintWriter writer = new PrintWriter(cfg_file, "UTF-8")) {
          cfg_file.createNewFile();
          writer.write(cfg_str);
          writer.close();
        } catch (IOException ex) {
          logger.severe("Error saving configuration!");
        }
      }
    }.runTaskAsynchronously(this);
  }
Example #4
0
 /**
  * 删除所有在startStr和endStr之间的字符串,包括startStr和endStr,即删除闭区间[startStr,endStr]
  *
  * @param sb
  * @param startStr
  * @param endStr
  */
 public static void deleteAllIn(StringBuilder sb, String startStr, String endStr) {
   int startIndex = 0;
   int endIndex = 0;
   while ((startIndex = sb.indexOf(startStr)) >= 0 && (endIndex = sb.indexOf(endStr)) >= 0) {
     sb.delete(startIndex, endIndex + endStr.length());
   }
 }
Example #5
0
  public void setWikipediaUrl(String wikipediaUrl) {
    // Log.d(TAG, "link item is " + wikipediaUrl);
    if (!wikipediaUrl.contains("http://")) {
      sb.append("http://").append(wikipediaUrl);
    } else {
      sb.append(wikipediaUrl);
      index = sb.indexOf("http");
      if (index > 0) {
        sb.delete(0, index);
      }
    }

    index = sb.indexOf(".m.");
    if (index == -1) {
      index = sb.indexOf(".");
      // Log.d(TAG, "dot index is " + index);
      sb.insert(index, ".m");
    }

    /*if(sb.indexOf("featured_article") > 0) {
    	Log.d(TAG, "article url is " + sb.toString());
    }*/
    this.wikipediaUrl = sb.toString();
    sb.setLength(0);
  }
Example #6
0
  /**
   * Formats a URI by adding a forward slash and removing the last forward slash from the URI.
   *
   * <p>e.g. some/random/uri/ -> /some/random/uri e.g. some/random/uri -> /some/random/uri e.g.
   * /some/random/uri/ -> /some/random/uri e.g. / -> / e.g. ////// -> /
   *
   * @param uri
   * @return
   */
  public static String formatUri(String uri) {
    if (StringUtilities.nullSafeStartsWith(uri, "\\")) {
      // windows file system
      return uri;
    }

    if (StringUtilities.isBlank(uri) || StringUtilities.nullSafeEqualsIgnoreCase("/", uri)) {
      return "/";
    }

    final StringBuilder externalName = new StringBuilder(uri);

    if (externalName.charAt(0) != '/') {
      externalName.insert(0, "/");
    }

    int doubleSlash = externalName.indexOf("//");

    while (doubleSlash > -1) {
      // removes leading '/'
      externalName.replace(doubleSlash, doubleSlash + 2, "/");
      doubleSlash = externalName.indexOf("//");
    }

    if (externalName.charAt(externalName.length() - 1) == '/' && externalName.length() != 1) {
      externalName.deleteCharAt(externalName.length() - 1);
    }

    return externalName.toString();
  }