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:StringUtils.java

/**
 * Reformats a string where lines that are longer than <tt>width</tt>
 * are split apart at the earliest wordbreak or at maxLength, whichever is
 * sooner. If the width specified is less than 5 or greater than the input
 * Strings length the string will be returned as is.
 * <p/>//from w  w w.jav  a 2s.  co m
 * Please note that this method can be lossy - trailing spaces on wrapped
 * lines may be trimmed.
 *
 * @param input the String to reformat.
 * @param width the maximum length of any one line.
 * @return a new String with reformatted as needed.
 */
public static String wordWrap(String input, int width, Locale locale) {
    // protect ourselves
    if (input == null) {
        return "";
    } else if (width < 5) {
        return input;
    } else if (width >= input.length()) {
        return input;
    }

    StringBuilder buf = new StringBuilder(input);
    boolean endOfLine = false;
    int lineStart = 0;

    for (int i = 0; i < buf.length(); i++) {
        if (buf.charAt(i) == '\n') {
            lineStart = i + 1;
            endOfLine = true;
        }

        // handle splitting at width character
        if (i > lineStart + width - 1) {
            if (!endOfLine) {
                int limit = i - lineStart - 1;
                BreakIterator breaks = BreakIterator.getLineInstance(locale);
                breaks.setText(buf.substring(lineStart, i));
                int end = breaks.last();

                // if the last character in the search string isn't a space,
                // we can't split on it (looks bad). Search for a previous
                // break character
                if (end == limit + 1) {
                    if (!Character.isWhitespace(buf.charAt(lineStart + end))) {
                        end = breaks.preceding(end - 1);
                    }
                }

                // if the last character is a space, replace it with a \n
                if (end != BreakIterator.DONE && end == limit + 1) {
                    buf.replace(lineStart + end, lineStart + end + 1, "\n");
                    lineStart = lineStart + end;
                }
                // otherwise, just insert a \n
                else if (end != BreakIterator.DONE && end != 0) {
                    buf.insert(lineStart + end, '\n');
                    lineStart = lineStart + end + 1;
                } else {
                    buf.insert(i, '\n');
                    lineStart = i + 1;
                }
            } else {
                buf.insert(i, '\n');
                lineStart = i + 1;
                endOfLine = false;
            }
        }
    }

    return buf.toString();
}

From source file:com.google.jenkins.flakyTestHandler.plugin.TestFlakyStatsOverRevision.java

/**
 * Get the name of a test that's URL-safe.
 *
 * @param testName input test name/*  ww w.j  a  v  a  2 s.c o  m*/
 * @return name of the test with illegal characters replaced with '_'
 */
public static String getSafeTestName(String testName) {
    StringBuilder buf = new StringBuilder(testName);
    for (int i = 0; i < buf.length(); i++) {
        char ch = buf.charAt(i);
        if (!Character.isJavaIdentifierPart(ch)) {
            buf.setCharAt(i, '_');
        }
    }
    return buf.toString();
}

From source file:io.wcm.maven.plugins.i18n.SlingI18nMap.java

/**
 * Creates a valid node name. Replaces all chars not in a-z, A-Z and 0-9 or '_', '.' with '-'.
 * @param value String to be labelized./*from  w  w  w. j  a v  a  2s .c om*/
 * @return The labelized string.
 */
private static String validName(String value) {

    // replace some special chars first
    String text = value;
    text = StringUtils.replace(text, "", "ae");
    text = StringUtils.replace(text, "", "oe");
    text = StringUtils.replace(text, "", "ue");
    text = StringUtils.replace(text, "", "ss");

    // replace all invalid chars
    StringBuilder sb = new StringBuilder(text);
    for (int i = 0; i < sb.length(); i++) {
        char ch = sb.charAt(i);
        if (!((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9') || (ch == '_')
                || (ch == '.'))) {
            ch = '-';
            sb.setCharAt(i, ch);
        }
    }
    return sb.toString();
}

From source file:com.adaptris.core.marshaller.xstream.XStreamUtils.java

/**
* Converts a camelcase name into a lowercase hyphen separated format for output to XML. Used by the marshalling process to
* convert a java class/field name into an xml element name.
* 
* @param fieldName - Current element name to be processed.
* @return translated name//from   w ww  .ja va2s.com
*/
public static String toXmlElementName(String fieldName) {
    if (fieldName == null) {
        return null;
    }
    if (fieldName.length() == 0) {
        return fieldName;
    }
    if (fieldName.length() == 1) {
        return fieldName.toLowerCase();
    }

    // -- Follow the Java beans Introspector::decapitalize
    // -- convention by leaving alone String that start with
    // -- 2 uppercase characters.
    if (Character.isUpperCase(fieldName.charAt(0)) && Character.isUpperCase(fieldName.charAt(1))) {
        return fieldName;
    }

    // -- process each character
    StringBuilder cbuff = new StringBuilder(fieldName);
    cbuff.setCharAt(0, Character.toLowerCase(cbuff.charAt(0)));

    boolean ucPrev = false;
    for (int i = 1; i < cbuff.length(); i++) {
        char ch = cbuff.charAt(i);
        if (Character.isUpperCase(ch)) {
            if (ucPrev) {
                continue;
            }
            ucPrev = true;
            cbuff.insert(i, '-');
            ++i;
            cbuff.setCharAt(i, Character.toLowerCase(ch));
        } else {
            ucPrev = false;
        }
    }
    return cbuff.toString();
}

From source file:no.sesat.search.site.config.AbstractDocumentFactory.java

/***
* <p>The words within the bean name are deduced assuming the
* first-letter-capital (for example camel's hump) naming convention. For
* example, the words in <code>FooBar</code> are <code>foo</code>
* and <code>bar</code>.</p>
*
* <p>Then the {@link #getSeparator} property value is inserted so that it separates
* each word.</p>/* w  ww .ja  va  2  s. c o  m*/
*
* @param beanName The name string to convert.  If a JavaBean
* class name, should included only the last part of the name
* rather than the fully qualified name (e.g. FooBar rather than
* org.example.FooBar).
* @return the bean name converted to either upper or lower case with words separated
* by the separator.
**/
public static String beanToXmlName(final String beanName) {

    final StringBuilder xmlName = new StringBuilder(beanName);
    for (int i = 0; i < xmlName.length(); ++i) {
        final char c = xmlName.charAt(i);
        if (Character.isUpperCase(c)) {
            xmlName.replace(i, i + 1, "-" + Character.toLowerCase(c));
            ++i;
        }
    }
    return xmlName.toString();
}

From source file:no.sesat.search.site.config.AbstractDocumentFactory.java

/** The reverse transformation to beanToXmlName(string). *
 * @param xmlName/* w  w  w  .ja v  a 2s .  c  o m*/
 * @return
 */
public static String xmlToBeanName(final String xmlName) {

    final StringBuilder beanName = new StringBuilder(xmlName);
    for (int i = 0; i < beanName.length(); ++i) {
        final char c = beanName.charAt(i);
        if ('-' == c) {
            beanName.replace(i, i + 2, String.valueOf(Character.toUpperCase(beanName.charAt(i + 1))));
            ++i;
        }
    }
    return beanName.toString();
}

From source file:com.amazonaws.hal.client.ConversionUtil.java

static String getPropertyName(String original) {
    StringBuilder propertyName = new StringBuilder(original.substring("get".length()));

    propertyName.setCharAt(0, Character.toLowerCase(propertyName.charAt(0)));

    return propertyName.toString();
}

From source file:Main.java

/**
 * Returns the ASCII characters up to but not including the next "\r\n", or
 * "\n"./*from w  w w . ja  va  2s.co m*/
 *
 * @throws java.io.EOFException if the stream is exhausted before the next newline
 *                              character.
 */
public static String readAsciiLine(InputStream in) throws IOException {
    StringBuilder result = new StringBuilder(80);
    while (true) {
        int c = in.read();
        if (c == -1) {
            throw new EOFException();
        } else if (c == '\n') {
            break;
        }

        result.append((char) c);
    }
    int length = result.length();
    if (length > 0 && result.charAt(length - 1) == '\r') {
        result.setLength(length - 1);
    }
    return result.toString();
}

From source file:org.hawkular.agent.monitor.util.Util.java

/**
 * Given a string builder, this ensures its last character is a forward-slash.
 *
 * @param str string builder to have a forward-slash character as its last when this method returns
 *///from   w  w  w. j  a  v a 2  s.c  om
public static void ensureEndsWithSlash(StringBuilder str) {
    if (str.length() == 0 || str.charAt(str.length() - 1) != '/') {
        str.append('/');
    }
}

From source file:com.adaptris.core.marshaller.xstream.XStreamUtils.java

/**
 * Converts a lowercase hyphen separated format into a camelcase based
 * format. Used by the unmarshalling process to convert an xml element into
 * a java class/field name.//from  www  . j av  a2  s  .  c  o m
 * 
 * @param xmlElementName
 *            - Current element name to be processed.
 * @return translated name
 */
public static String toFieldName(String xmlElementName) {
    if (xmlElementName == null) {
        return null;
    }
    if (xmlElementName.length() == 0) {
        return xmlElementName;
    }
    if (xmlElementName.length() == 1) {
        return xmlElementName.toLowerCase();
    }
    // -- Follow the Java beans Introspector::decapitalize
    // -- convention by leaving alone String that start with
    // -- 2 uppercase characters.
    if (Character.isUpperCase(xmlElementName.charAt(0)) && Character.isUpperCase(xmlElementName.charAt(1))) {
        return xmlElementName;
    }
    // -- process each character
    StringBuilder input = new StringBuilder(xmlElementName);
    StringBuilder output = new StringBuilder();
    output.append(Character.toLowerCase(input.charAt(0)));
    boolean multiHyphens = false;
    for (int i = 1; i < input.length(); i++) {
        char ch = input.charAt(i);
        if (ch == '-') {
            if (input.charAt(++i) != '-') {
                output.append(Character.toUpperCase(input.charAt(i)));
            } else {
                multiHyphens = true;
            }
        } else {
            if (multiHyphens) {
                output.append(Character.toUpperCase(ch));
            } else {
                output.append(ch);
            }
            multiHyphens = false;

        }
    }
    return output.toString();
}