Example usage for java.lang StringBuilder StringBuilder

List of usage examples for java.lang StringBuilder StringBuilder

Introduction

In this page you can find the example usage for java.lang StringBuilder StringBuilder.

Prototype

public StringBuilder(CharSequence seq) 

Source Link

Document

Constructs a string builder that contains the same characters as the specified CharSequence .

Usage

From source file:Main.java

public static String dumpJPEG(byte[] jpeg) {
    StringBuilder sb = new StringBuilder("jpeg ");
    if (jpeg == null) {
        sb.append("<null>");
    } else {/*from  w  ww  .  ja v  a  2  s . c om*/
        sb.append("length=" + jpeg.length);
    }
    return sb.toString();

}

From source file:Main.java

public static File convertFormat(File file, String newFormat) {
    StringBuilder tmpValue = new StringBuilder(file.getAbsolutePath());
    int index = tmpValue.lastIndexOf(".") + 1;
    tmpValue.delete(index, tmpValue.length());
    tmpValue.append(newFormat);/*from   w  w  w  .j  a  va  2  s.com*/
    File result = new File(tmpValue.toString());

    return result;
}

From source file:Main.java

public static String titleCase(String s) {
    StringBuilder rv = new StringBuilder(s.length());
    StringTokenizer strtok = new StringTokenizer(s);

    while (strtok.hasMoreTokens()) {
        String word = strtok.nextToken();
        String firstLetter = word.substring(0, 1);
        String restOfWord = word.substring(1);

        if (rv.length() > 0)
            rv.append(" ");
        rv.append(firstLetter.toUpperCase());
        rv.append(restOfWord.toLowerCase());
    }/*from ww  w  . j a v  a 2s. c  om*/

    return rv.toString();
}

From source file:Main.java

public static String UUID() {
    String uuid = java.util.UUID.randomUUID().toString();
    StringBuilder bf = new StringBuilder(32);
    for (int i = 0; i < uuid.length(); ++i) {
        char c = uuid.charAt(i);
        if (c != '-' && c != '_') {
            bf.append(c);/*  w  ww. ja  va2  s .co  m*/
        }
    }
    return bf.toString();
}

From source file:Main.java

private static String swapChars(String str, int lIdx, int rIdx) {
    StringBuilder sb = new StringBuilder(str);
    char l = sb.charAt(lIdx), r = sb.charAt(rIdx);
    sb.setCharAt(lIdx, r);//from  w ww  .  j  a va2s  . com
    sb.setCharAt(rIdx, l);
    return sb.toString();
}

From source file:Main.java

public static final String byteArrayToHexString(byte[] data) {
    StringBuilder sb = new StringBuilder(data.length * 2);
    for (byte b : data) {
        int v = b & 0xff;
        if (v < 16) {
            sb.append('0');
        }//from  w w  w.j a  v  a2  s  . c o  m
        sb.append(Integer.toHexString(v));
    }
    return sb.toString().toUpperCase(Locale.getDefault());
}

From source file:Main.java

public static String bytesToHexString(byte[] bytes) {
    StringBuilder sb = new StringBuilder(bytes.length);
    String temp;/*from   w ww  .j  av  a  2  s. c o m*/
    for (int i = 0; i < bytes.length; i++) {
        temp = Integer.toHexString(0xFF & bytes[i]);
        if (temp.length() < 2)
            sb.append(0);
        sb.append(temp.toUpperCase()).append(" ");
    }
    return sb.toString();
}

From source file:Main.java

private static String deriveLongerString(String str) {
    StringBuilder sb = new StringBuilder(str);
    StringBuilder builder = new StringBuilder();
    builder.append(sb.toString().toLowerCase());
    builder.append(sb.toString().toUpperCase());
    StringBuilder result = new StringBuilder();
    result.append(sb);//w w w .ja  v a 2 s  .  c  o m
    result.append(mix(builder.toString()));
    result.append(mix(builder.reverse().toString()));
    return result.toString();
}

From source file:Main.java

public static String replace(String str, String target, String replacement) {
    StringBuilder sb = new StringBuilder(str);

    int index = sb.length();
    int lenTarget = target.length();
    while ((index = sb.lastIndexOf(target, index)) != -1) {
        sb.replace(index, index + lenTarget, replacement);
        index -= lenTarget;//from  w  w  w.  j  a  v  a  2  s . c  om
    }

    return sb.toString();
}

From source file:Main.java

public static String getMoneyString(int value) {
    String returnValue = "" + value;
    StringBuilder builder = new StringBuilder(returnValue);
    if (returnValue.length() > 2) {
        builder.insert(builder.length() - 2, ".");
        builder.insert(0, "$");
    } else {/*from  ww w .  j av  a  2 s  .  c  o  m*/
        builder.insert(0, "$0.");
    }

    return builder.toString();
}