Java StringBuilder.reverse Examples

Java StringBuilder.reverse - 30 examples found. These are the top rated real world Java examples of StringBuilder.reverse extracted from open source projects. You can rate examples to help us improve the quality of examples.
Example #1
0
  // -------------------  Solution 3 ------------------------//
  // split the string by hand (same algorithm as above 2)
  public String reverseWords3(String s) {
    // validate input
    if (s == null || s.length() == 0) {
      return "";
    }

    // iterate backwards and append words one-by-one
    StringBuilder reversedStr = new StringBuilder();
    StringBuilder curWord = new StringBuilder();
    for (int i = s.length() - 1; i >= 0; i--) {
      if (s.charAt(i) == ' ') { // skip all spaces
        continue;

      } else { // for non-spaces
        // starter of a new word
        if (i == s.length() - 1 || s.charAt(i + 1) == ' ') {
          if (curWord.length() > 0) {
            reversedStr.append(curWord.reverse().toString() + " ");
          }
          curWord = new StringBuilder();
        }
        curWord.append(s.charAt(i));
      }
    }
    // don't forget to add the last word!
    reversedStr.append(curWord.reverse().toString());
    return reversedStr.toString();
  }
Example #2
0
  public static void add(String a, String b) {
    if (a.length() != b.length() && a.length() < 32 && b.length() < 32) {
      if (a.length() > b.length()) {
        StringBuilder z = new StringBuilder(b);
        z.reverse();
        while (z.length() < a.length()) {
          z.append(0);
        }
        z.reverse();
        b = z.toString();
      } else {
        StringBuilder z = new StringBuilder(a);
        z.reverse();
        while (z.length() < b.length()) {
          z.append(0);
        }
        z.reverse();
        a = z.toString();
      }
    }

    if (b.length() > 31) {
      b = "overflow";
    }
    if (a.length() > 31) {
      a = "overflow";
    }

    boolean cero = true;
    int i = 0;
    while (i < b.length() && cero == true) {
      if (b.charAt(i) != '0') {
        cero = false;
      }
      i++;
    }

    System.out.println(a + " " + b);

    if (!cero && !a.equals("overflow") && !b.equals("overflow")) {
      StringBuilder c = new StringBuilder();
      StringBuilder d = new StringBuilder();
      for (int j = 0; j < a.length(); j++) {
        if (a.charAt(j) != b.charAt(j)) {
          c.append(1);
        } else {
          c.append(0);
        }
        if (a.charAt(j) == '1' && b.charAt(j) == '1') {
          d.append(1);
        } else {
          d.append(0);
        }
      }
      a = c.toString();
      b = mult2Bin(d.toString());
      add(a, b);
    }
  }
 /** 分割电话号码 */
 private String splitPhoneNum(String phone) {
   StringBuilder builder = new StringBuilder(phone);
   builder.reverse();
   for (int i = 4, len = builder.length(); i < len; i += 5) {
     builder.insert(i, ' ');
   }
   builder.reverse();
   return builder.toString();
 }
 public static String toReverseText(String text) {
   StringBuilder newString = new StringBuilder();
   StringBuilder tempString = new StringBuilder();
   for (int i = 0; i < text.length(); i++) {
     if (text.charAt(i) == ' ') {
       newString.append(tempString.reverse()).append(" ");
       tempString.delete(0, tempString.length());
     } else {
       tempString.append(text.charAt(i));
     }
   }
   newString.append(tempString.reverse());
   return newString.toString();
 }
Example #5
0
 /**
  * Returns any trailing period, comma, semicolon, or colon characters from the given string. This
  * method is useful when parsing raw HTML links, in which case trailing punctuation must be
  * removed. Note that only punctuation that is not previously matched is trimmed - if the input is
  * "http://example.com/page_(page)" then the trailing parantheses will not be trimmed.
  *
  * @param text The text from which trailing punctuation should be returned.
  * @return Any trailing punctuation from the given text, or an empty string otherwise.
  */
 private String extractTrailingPunctuation(String text) {
   if (StringUtils.isBlank(text)) {
     return "";
   }
   StringBuilder buffer = new StringBuilder();
   for (int i = text.length() - 1; i >= 0; i--) {
     char c = text.charAt(i);
     if (c == '.' || c == ';' || c == ',' || c == ':' || c == '(' || c == '[' || c == '{') {
       buffer.append(c);
       continue;
     }
     // if the value ends with ), ] or } then strip it UNLESS there is a matching
     // opening tag
     if (c == ')' || c == ']' || c == '}') {
       String closeChar = String.valueOf(c);
       String openChar = (c == ')') ? "(" : ((c == ']') ? "[" : "{");
       int pos = Utilities.findMatchingStartTag(text, i, openChar, closeChar);
       if (pos == -1) {
         buffer.append(c);
         continue;
       }
     }
     break;
   }
   if (buffer.length() == 0) {
     return "";
   }
   buffer = buffer.reverse();
   return buffer.toString();
 }
  /**
   * the method reverses a string and returns the reversed value
   *
   * @param input the string to be reversed
   * @return a String
   */
  public static String reverse(String input) {

    StringBuilder sinput = new StringBuilder(input);
    StringBuilder sinput1 = sinput.reverse();
    String reverse = sinput1.toString();
    return reverse;
  }