Example usage for java.lang StringBuffer length

List of usage examples for java.lang StringBuffer length

Introduction

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

Prototype

@Override
    public synchronized int length() 

Source Link

Usage

From source file:StringUtil.java

/**
 * <P>Right justify a string using the specified padding character to pad
 * the left part of the string until it reaches <code>maxLength</code>.
 * If the length of the string is greater than <code>maxLength</code> characters,
 * return only the first, left <code>maxLength</code> characters.</P>
 *
 * @return   The right-justified string.
 *//*from   w  w  w . j  av  a2 s .com*/
public static String rightJustify(String s, int maxLength, char fill) {

    if (s == null || maxLength == 0) {
        return s;
    }
    // If the string has more than "maxLength" characters, 
    // return only the first "maxLength" characters of the string. 
    if (s.trim().length() > maxLength) {
        return s.substring(0, maxLength).trim();
    }

    StringBuffer sb = new StringBuffer(s.trim());

    // Insert as many padding characters as needed to reach "maxLength".
    while (sb.length() < maxLength) {
        sb.insert(0, fill);
    }

    return sb.toString();
}

From source file:Main.java

public static final String getComment(Element elem) {
    StringBuffer sb = new StringBuffer();
    Node node = elem.getPreviousSibling();
    while (node != null) {
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            break;
        }/*from w ww.  j  a va  2 s. co  m*/
        if (node.getNodeType() == Node.COMMENT_NODE) {
            if (sb.length() > 0) {
                sb.insert(0, '\n');
                sb.insert(0, ((Comment) node).getData());
            } else {
                sb.append(((Comment) node).getData());
            }
        }
        node = node.getPreviousSibling();
    }
    return sb.toString();
}

From source file:Main.java

public static String attributesStr(Node node) {
    if (node == null) {
        return null;
    }/*from   w  w w . j a v  a 2 s .c om*/
    StringBuffer attributes = new StringBuffer();
    for (int i = 0; i < node.getAttributes().getLength(); i++) {
        attributes.append(node.getAttributes().item(i).getNodeName() + "="
                + node.getAttributes().item(i).getNodeValue() + ", ");
    }
    if (attributes.length() > 1) {
        attributes.delete(attributes.length() - 2, attributes.length());
    } else {
        attributes.append(node.getNodeName() + " has NO attributes.");
    }
    return attributes.toString();
}

From source file:net.jforum.util.URLNormalizer.java

  /**
 * //from   w ww.j ava2  s .com
 * @param url the url to normalize
 * @param limit do not process more than <code>limit + 1</code> chars
 * @param friendlyTruncate If <code>true</code>, will try to not cut a word if
 * more than <code>limit</code> chars were processed. It will stop in the next
 * special char
 * @return the normalized url
 */
public static String normalize(String url, int limit, boolean friendlyTruncate)
{
  char[] chars = url.toCharArray();
    
  StringBuffer sb = new StringBuffer(url.length());
    
  for (int i = 0; i < chars.length; i++) {
    if (i <= limit || (friendlyTruncate && i > limit && sb.charAt(sb.length() - 1) != '_')) {
        
      if (Character.isSpaceChar(chars[i]) || chars[i] == '-') {
        if (friendlyTruncate && i > limit) {
          break;
        }
          
        if (i > 0 && sb.charAt(sb.length() - 1) != '_') {
          sb.append('_');
        }
      }
        
      if (Character.isLetterOrDigit(chars[i])) {
        sb.append(chars[i]);
      }
      else if (friendlyTruncate && i > limit) {
        break;
      }
    }
  }
    
  return sb.toString().toLowerCase();
}

From source file:Main.java

/**
 * Removes all occurrences of the specified stylename in the given style and
 * returns the updated style. Trailing semicolons are preserved.
 *///w  w  w .  j a v a  2 s  .  c o  m
public static String removeStylename(String style, String stylename) {
    StringBuffer buffer = new StringBuffer();

    if (style != null) {
        String[] tokens = style.split(";");

        for (int i = 0; i < tokens.length; i++) {
            if (!tokens[i].equals(stylename)) {
                buffer.append(tokens[i] + ";");
            }
        }
    }

    return (buffer.length() > 1) ? buffer.substring(0, buffer.length() - 1) : buffer.toString();
}

From source file:XMLWriteTest.java

/**
 * Converts a color to a hex value./*from   w ww . j av a2 s .  co m*/
 * @param c a color
 * @return a string of the form #rrggbb
 */
private static String colorToString(Color c) {
    StringBuffer buffer = new StringBuffer();
    buffer.append(Integer.toHexString(c.getRGB() & 0xFFFFFF));
    while (buffer.length() < 6)
        buffer.insert(0, '0');
    buffer.insert(0, '#');
    return buffer.toString();
}

From source file:info.globalbus.dkim.DKIMUtil.java

protected static String concatArray(ArrayList<String> l, String separator) {
    StringBuffer buf = new StringBuffer();
    Iterator<String> iter = l.iterator();
    while (iter.hasNext()) {
        buf.append(iter.next()).append(separator);
    }/* w  ww.  j  av  a 2 s . com*/

    return buf.substring(0, buf.length() - separator.length());
}

From source file:cn.edu.bit.whitesail.utils.MD5Signature.java

public static String calculate(byte[] byteArray) {
    StringBuffer result = null;
    if (byteArray == null)
        return null;
    try {//from  ww  w .j  a v  a2  s  . c om
        MessageDigest m = MessageDigest.getInstance("MD5");
        m.update(byteArray);
        result = new StringBuffer(new BigInteger(1, m.digest()).toString(16));
        for (int i = 0; i < 32 - result.length(); i++)
            result.insert(0, '0');
    } catch (NoSuchAlgorithmException ex) {
        LOG.fatal("MD5 Hashing Failed,System is going down");
        System.exit(1);
    }
    return result.toString();
}

From source file:Util.java

public static boolean retreiveTextFileFromJar(String resourceName, String targetDirectory) throws Exception {
    boolean found = false;
    if (resourceName != null) {
        InputStream is = Util.class.getResourceAsStream(resourceName);
        if (is == null)
            logger.log(Level.WARNING, "The resource '" + resourceName + "' was not found.");
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line;/* w ww.  j  ava 2  s . com*/
        String lineSep = System.getProperty("line.separator");
        StringBuffer sb = new StringBuffer();
        while ((line = br.readLine()) != null) {
            sb.append(line);
            sb.append(lineSep);
        }
        is.close();
        if (sb != null) {
            if (sb.length() > 0) {
                FileWriter temp = new FileWriter(targetDirectory + File.separator + resourceName);
                temp.write(sb.toString());
                temp.close();
                found = true;
            }
        }
    }
    return (found);
}

From source file:StringUtils.java

static public String padWithLeadingZeros(String string, int desiredLength) {
    StringBuffer bff = new StringBuffer(string);
    while (bff.length() < desiredLength)
        bff.insert(0, '0');
    return bff.toString();
}