Java StringBuilder.lastIndexOf Examples

Java StringBuilder.lastIndexOf - 30 examples found. These are the top rated real world Java examples of StringBuilder.lastIndexOf extracted from open source projects. You can rate examples to help us improve the quality of examples.
  @Nullable
  private static String trimToValue(@Nullable StringBuilder output) {
    if (output == null) {
      return null;
    }
    int pos = output.lastIndexOf("  ");
    int pos2 = output.lastIndexOf("\t");
    pos = Math.max(pos, pos2);
    if (pos == -1) {
      return null;
    }

    output.delete(0, pos + 1);
    String blackList = "\r\n \"";
    int startPos = 0;
    int endPos = output.length() - 1;
    while (true) {
      if (startPos >= endPos) {
        return null;
      }
      if (blackList.indexOf(output.charAt(startPos)) != -1) {
        startPos++;
      } else {
        break;
      }
    }
    while (true) {
      if (blackList.indexOf(output.charAt(endPos)) != -1) {
        endPos--;
      } else {
        break;
      }
    }
    return output.subSequence(startPos, endPos + 1).toString();
  }
 /**
  * Create result values from observation according to ResultEncoding and ResultStructure
  *
  * @param observations Observation to create result values from
  * @param sosResultEncoding The ResultEncoding
  * @param sosResultStructure The ResultStructure
  * @return Result values String from observation according to ResultEncoding and ResultStructure
  * @throws OwsExceptionReport If creation fails
  */
 public static String createResultValuesFromObservations(
     final List<AbstractObservation> observations,
     final SosResultEncoding sosResultEncoding,
     final SosResultStructure sosResultStructure)
     throws OwsExceptionReport {
   final StringBuilder builder = new StringBuilder();
   if (CollectionHelper.isNotEmpty(observations)) {
     final String tokenSeparator = getTokenSeparator(sosResultEncoding.getEncoding());
     final String blockSeparator = getBlockSeparator(sosResultEncoding.getEncoding());
     final Map<Integer, String> valueOrder =
         getValueOrderMap(sosResultStructure.getResultStructure());
     addElementCount(builder, observations.size(), blockSeparator);
     for (final AbstractObservation observation : observations) {
       for (final Integer intger : valueOrder.keySet()) {
         final String definition = valueOrder.get(intger);
         if (definition.equals(PHENOMENON_TIME)) {
           builder.append(
               getTimeStringForPhenomenonTime(
                   observation.getPhenomenonTimeStart(), observation.getPhenomenonTimeEnd()));
         } else if (definition.equals(RESULT_TIME)) {
           builder.append(getTimeStringForResultTime(observation.getResultTime()));
         } else {
           builder.append(getValueAsStringForObservedProperty(observation, definition));
         }
         builder.append(tokenSeparator);
       }
       builder.delete(builder.lastIndexOf(tokenSeparator), builder.length());
       builder.append(blockSeparator);
     }
     if (builder.length() > 0) {
       builder.delete(builder.lastIndexOf(blockSeparator), builder.length());
     }
   }
   return builder.toString();
 }
Example #3
0
  /**
   * List the entities in JSON format
   *
   * @param entities entities to return as JSON strings
   */
  public static String writeJSON(Iterable<Entity> entities) {
    logger.log(Level.INFO, "creating JSON format object");
    StringBuilder sb = new StringBuilder();

    int i = 0;
    sb.append("{\"data\": [");
    for (Entity result : entities) {
      Map<String, Object> properties = result.getProperties();
      sb.append("{");
      if (result.getKey().getName() == null)
        sb.append("\"name\" : \"" + result.getKey().getId() + "\",");
      else sb.append("\"name\" : \"" + result.getKey().getName() + "\",");

      for (String key : properties.keySet()) {
        Object object = properties.get(key);
        if (object instanceof Text) {
          Text value = (Text) properties.get(key);
          sb.append("\"" + key + "\" : \"" + value.getValue() + "\",");
          // logger.info(value.getValue());//debug;
        } else {
          sb.append("\"" + key + "\" : \"" + properties.get(key) + "\",");
        }
      }
      sb.deleteCharAt(sb.lastIndexOf(","));
      sb.append("},");
      i++;
    }
    if (i > 0) {
      sb.deleteCharAt(sb.lastIndexOf(","));
    }
    sb.append("]}");
    return sb.toString();
  }
Example #4
0
  /**
   * 描述:从输入流中获得String.
   *
   * @param is 输入流
   * @return 获得的String
   */
  public static String convertStreamToString(InputStream is) {
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();
    String line = null;
    try {
      while ((line = reader.readLine()) != null) {
        sb.append(line + "\n");
      }

      // 最后一个\n删除
      if (sb.indexOf("\n") != -1 && sb.lastIndexOf("\n") == sb.length() - 1) {
        sb.delete(sb.lastIndexOf("\n"), sb.lastIndexOf("\n") + 1);
      }

    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      try {
        is.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    return sb.toString();
  }
 @NotNull
 public static String getLibraryName(@NotNull Library library) {
   final String result = library.getName();
   if (result != null) {
     return result;
   }
   String[] endingsToStrip = {"/", "!", ".jar"};
   StringBuilder buffer = new StringBuilder();
   for (OrderRootType type : OrderRootType.getAllTypes()) {
     for (String url : library.getUrls(type)) {
       buffer.setLength(0);
       buffer.append(url);
       for (String ending : endingsToStrip) {
         if (buffer.lastIndexOf(ending) == buffer.length() - ending.length()) {
           buffer.setLength(buffer.length() - ending.length());
         }
       }
       final int i = buffer.lastIndexOf(PATH_SEPARATOR);
       if (i < 0 || i >= buffer.length() - 1) {
         continue;
       }
       String candidate = buffer.substring(i + 1);
       if (!StringUtil.isEmpty(candidate)) {
         return candidate;
       }
     }
   }
   assert false;
   return "unknown-lib";
 }
Example #6
0
 /** @param toInsert */
 public void insertAtEndOfBody(String toInsert) {
   int insertPoint = sb.lastIndexOf("</body>");
   if (-1 == insertPoint) {
     insertPoint = sb.lastIndexOf("</BODY>");
   }
   if (-1 == insertPoint) {
     insertPoint = sb.length();
   }
   sb.insert(insertPoint, toInsert);
 }