Example usage for java.lang StringBuilder charAt

List of usage examples for java.lang StringBuilder charAt

Introduction

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

Prototype

char charAt(int index);

Source Link

Document

Returns the char value at the specified index.

Usage

From source file:com.p000ison.dev.simpleclans2.api.chat.ChatBlock.java

/**
 * Returns the length of a string.//from  ww  w.j av a2  s.  c  om
 *
 * @param text The text to check.
 * @return The length of the string.
 */
public static int msgLength(StringBuilder text) {
    int length = 0;

    // Loop through all the characters, skipping any color characters and their following color codes

    int textLength = text.length() - 1;

    for (int x = 0; x < text.length(); x++) {
        char currentChar = text.charAt(x);

        //ignore colors, but only if there is enought space. A  at the end of the line will not be recognized
        if (currentChar == '\u00a7' && x < textLength) {
            char nextChar = text.charAt(x + 1);
            if (ChatColor.getByChar(nextChar) == null) {
                continue;
            }
        }

        int len = charLength(currentChar);
        if (len > 0) {
            length += len;
        } else {
            x++;
        }
    }
    return length;
}

From source file:fr.landel.utils.io.FileUtils.java

/**
 * Convert all newline characters into Windows newlines.
 * //from  w  ww . j  a v  a  2 s . c o m
 * @param input
 *            The text to convert
 * @return The text converted
 */
public static StringBuilder convertToWindows(final StringBuilder input) {
    final StringBuilder output = new StringBuilder(input);

    int pos = 0;
    while ((pos = output.indexOf(LFCR, pos)) > -1) {
        output.replace(pos, pos + 2, CRLF);
        pos++;
    }
    pos = 0;
    while ((pos = output.indexOf(L, pos)) > -1) {
        if (pos == 0 || output.charAt(pos - 1) != CR) {
            output.replace(pos, pos + 1, CRLF);
        }
        pos++;
    }
    pos = 0;
    final int len = output.length();
    while ((pos = output.indexOf(C, pos)) > -1) {
        if (pos == len - 1 || output.charAt(pos + 1) != LF) {
            output.replace(pos, pos + 1, CRLF);
        }
        pos++;
    }

    return output;
}

From source file:fr.landel.utils.io.FileUtils.java

/**
 * Convert all newline characters into Mac OS newlines.
 * /*from  w  w  w. ja va2s.c  om*/
 * @param input
 *            The text to convert
 * @return The text converted
 */
public static StringBuilder convertToMacOS(final StringBuilder input) {
    final StringBuilder output = new StringBuilder(input);

    int pos = 0;
    while ((pos = output.indexOf(CRLF, pos)) > -1) {
        output.replace(pos, pos + 2, LFCR);
        pos++;
    }
    pos = 0;
    while ((pos = output.indexOf(C, pos)) > -1) {
        if (pos == 0 || output.charAt(pos - 1) != LF) {
            output.replace(pos, pos + 1, LFCR);
        }
        pos++;
    }
    pos = 0;
    final int len = output.length();
    while ((pos = output.indexOf(L, pos)) > -1) {
        if (pos == len - 1 || output.charAt(pos + 1) != CR) {
            output.replace(pos, pos + 1, LFCR);
        }
        pos++;
    }

    return output;
}

From source file:org.cesecore.util.StringTools.java

/**
 * Characters from 'str' will be stripped like this:
 * any character that is in the 'stripThis' set will be replaced with '/'.
 * any character that is escaped (preceded with '\') and not in the {@value #allowedEscapeChars} set will be replaced with '/'.
 * when a character is replaced with '/' and also escaped then the preceding escape character '\' will be removed.
 * //from w  w w  .  j  a  va2s  .  c  o  m
 * @param str the original string
 * @param stripThis set of characters that should be stripped.
 * @return the stripped string
 */
private static String strip(final String str, final CharSet stripThis) {
    if (str == null) {
        return null;
    }
    final StringBuilder buf = new StringBuilder(str);
    int index = 0;
    int end = buf.length();
    while (index < end) {
        if (buf.charAt(index) == '\\') {
            // Found an escape character.
            if (index + 1 == end) {
                // If this is the last character we should remove it.
                buf.setCharAt(index, '/');
            } else if (!isAllowedEscape(buf.charAt(index + 1))) {
                // We did not allow this character to be escaped. Replace both the \ and the character with a single '/'.
                buf.setCharAt(index, '/');
                buf.deleteCharAt(index + 1);
                end--;
            } else {
                index++;
            }
        } else if (stripThis.contains(buf.charAt(index))) {
            // Illegal character. Replace it with a '/'.
            buf.setCharAt(index, '/');
        }
        index++;
    }
    final String result = buf.toString();
    if (log.isDebugEnabled() && !result.equals(str)) {
        log.debug("Some chars stripped. Was '" + str + "' is now '" + result + "'.");
    }
    return result;
}

From source file:com.caocao.util.StringUtils.java

/**
 * Trim all occurences of the supplied leading character from the given String.
 * @param str the String to check// w w w  .ja v a  2 s  .  c om
 * @param leadingCharacter the leading character to be trimmed
 * @return the trimmed String
 */
public static String trimLeadingCharacter(String str, char leadingCharacter) {
    if (isEmpty(str)) {
        return str;
    }
    StringBuilder sb = new StringBuilder(str);
    while (sb.length() > 0 && sb.charAt(0) == leadingCharacter) {
        sb.deleteCharAt(0);
    }
    return sb.toString();
}

From source file:org.diorite.impl.metrics.Metrics.java

private static void appendJSONPair(final StringBuilder json, final String key, final String value) {
    boolean isValueNumeric = false;

    try {// ww  w  .  java 2s  .com
        if (value.equals("0") || !value.endsWith("0")) {
            Double.parseDouble(value);
            isValueNumeric = true;
        }
    } catch (final NumberFormatException e) {
        isValueNumeric = false;
    }

    if (json.charAt(json.length() - 1) != '{') {
        json.append(',');
    }

    json.append(escapeJSON(key));
    json.append(':');

    if (isValueNumeric) {
        json.append(value);
    } else {
        json.append(escapeJSON(value));
    }
}

From source file:org.eclipse.ecr.core.storage.sql.extensions.EmbeddedFunctions.java

public static final String parseWord(String string) {
    int len = string.length();
    if (len < 3) {
        return null;
    }// w w  w. jav a 2 s  .co m
    StringBuilder buf = new StringBuilder(len);
    for (int i = 0; i < len; i++) {
        char c = Character.toLowerCase(string.charAt(i));
        if (c == '\u00e6') {
            buf.append("ae");
        } else if (c >= '\u00e0' && c <= '\u00ff') {
            buf.append(UNACCENTED.charAt((c) - 0xe0));
        } else if (c == '\u0153') {
            buf.append("oe");
        } else {
            buf.append(c);
        }
    }
    // simple heuristic to remove plurals
    int l = buf.length();
    if (l > 3 && buf.charAt(l - 1) == 's') {
        buf.setLength(l - 1);
    }
    String word = buf.toString();
    if (stopWords.contains(word)) {
        return null;
    }
    return word;
}

From source file:practica1.Babage.java

private static String factores(int numero) {
    StringBuilder factores = new StringBuilder();

    for (int w = 1; w <= numero; w++) {
        if (numero % w == 0) {
            factores.append(String.valueOf(w)).append(", ");
        }//  www .  ja  v  a  2  s  .  c  om
    }

    if (factores.charAt(factores.length() - 1) == ' ') {
        factores.setLength(factores.length() - 2);
    }

    return factores.toString();
}

From source file:org.fao.fenix.wds.core.utils.olap.OLAPWrapper.java

public static StringBuilder cleanJSON(StringBuilder sb) {

    String s = "{},";
    while ((sb.indexOf(s)) > -1)
        sb.replace(sb.indexOf(s), s.length() + sb.indexOf(s), "");

    s = "\"v\":[{}]";
    while ((sb.indexOf(s)) > -1)
        sb.replace(sb.indexOf(s), s.length() + sb.indexOf(s), "");

    s = ",}";//from w  ww .ja v  a 2 s  .c o m
    while ((sb.indexOf(s)) > -1)
        sb.replace(sb.indexOf(s), s.length() + sb.indexOf(s), "}");

    int idx_1 = -1;
    int idx_2 = -1;
    for (int i = 0; i < sb.length(); i++) {
        if (sb.charAt(i) == '{')
            idx_1 = i;
        if (sb.charAt(i) == '}')
            idx_2 = i;
        if (idx_1 > -1 && idx_2 > -1) {
            int count = 0;
            for (int j = idx_1; j < idx_2; j++)
                if (sb.charAt(j) == '"')
                    count++;
            if (count == 4) {
                for (int z = idx_2; z >= idx_1; z--) {
                    sb.setCharAt(z, ' ');
                }
            }
            i = 1 + idx_2;
            idx_1 = -1;
            idx_2 = -1;
        }
    }

    s = " , ";
    while ((sb.indexOf(s)) > -1)
        sb.replace(sb.indexOf(s), s.length() + sb.indexOf(s), "");

    s = " ,{";
    while ((sb.indexOf(s)) > -1)
        sb.replace(sb.indexOf(s), s.length() + sb.indexOf(s), "{");

    s = "}]}, ";
    while ((sb.indexOf(s)) > -1)
        sb.replace(sb.indexOf(s), s.length() + sb.indexOf(s), "}]}");

    idx_1 = -1;
    idx_2 = -1;
    for (int i = 0; i < sb.length(); i++) {
        if (sb.charAt(i) == '}')
            idx_1 = i;
        if (sb.charAt(i) == '{' && i > idx_1)
            idx_2 = i;
        if (idx_1 > -1 && idx_2 > -1 && idx_2 > idx_1) {
            String tmp = sb.substring(idx_1, 1 + idx_2);
            int blanks = 0;
            int commas = 0;
            for (int z = 0; z < tmp.length(); z++) {
                switch (tmp.charAt(z)) {
                case ' ':
                    blanks++;
                    break;
                case ',':
                    commas++;
                    break;
                }
            }
            if (blanks > 0 && commas == 0) {
                String s1 = sb.substring(0, idx_1);
                String s2 = sb.substring(1 + idx_2);
                sb = new StringBuilder(s1 + "},{" + s2);
            }
            i = 1 + idx_2;
            idx_1 = -1;
            idx_2 = -1;
        }
    }

    s = "{}";
    while ((sb.indexOf(s)) > -1)
        sb.replace(sb.indexOf(s), s.length() + sb.indexOf(s), "");

    s = "},]";
    while ((sb.indexOf(s)) > -1)
        sb.replace(sb.indexOf(s), s.length() + sb.indexOf(s), "}]");

    return sb;
}

From source file:ala.soils2sat.DrawingUtils.java

private static List<String> wrapString(String s, FontMetrics fm, int width) {
    List<String> lines = new ArrayList<String>();
    StringBuilder line = new StringBuilder();
    for (int i = 0; i < s.length(); ++i) {
        char ch = s.charAt(i);
        String test = line.toString() + ch;
        if (fm.stringWidth(test) > width) {
            if (test.length() > 1) {
                // Backtrack to look for a space...
                boolean breakFound = false;
                if (ch != ' ') {

                    for (int j = line.length() - 1; j > 0; --j) {
                        if (line.charAt(j) == ' ') {
                            lines.add(line.substring(0, j));
                            line = new StringBuilder(line.substring(j + 1));
                            breakFound = true;
                            break;
                        }/*from  w  w w .j ava 2s . co m*/
                    }
                }
                if (!breakFound) {
                    lines.add(line.toString());
                    line = new StringBuilder();
                }
                line.append(ch);
            } else {
                lines.add(test);
                line = new StringBuilder();
            }
        } else {
            line.append(ch);
        }
    }
    if (line.length() > 0) {
        lines.add(line.toString());
    }
    return lines;
}