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

/**
 * Creates a NMEA checksum for a sentence.
 * /* w w w  .  j ava2s .c  om*/
 * The checksum is calculated by XOR every char value, between '$' and
 * '*'(end), with the current sum.
 * 
 * @param sbString
 *            String to calculate the checksum.
 * @return The checksum.
 */
public static int getNMEAChecksum(final StringBuilder sbString) {
    int checksum = 0;

    for (int i = 0; i < sbString.length(); i++) {
        if (sbString.charAt(i) != '*' && sbString.charAt(i) != '$')
            checksum ^= sbString.charAt(i);
    }
    return checksum;
}

From source file:Main.java

public static String stripControlChars(String iString) {
    StringBuilder result = new StringBuilder(iString);
    int idx = result.length();
    while (idx-- > 0) {
        if (result.charAt(idx) < 0x20 && result.charAt(idx) != 0x9 && result.charAt(idx) != 0xA
                && result.charAt(idx) != 0xD)
            result.deleteCharAt(idx);//from  ww  w  . ja  v a  2  s. c o  m
    }
    return result.toString();
}

From source file:Main.java

/**
 * Trim trailing whitespace from the given String.
 * @param str the String to check//from www . j  av  a 2s . c  o m
 * @return the trimmed String
 * @see java.lang.Character#isWhitespace
 */
public static String trimTrailingWhitespace(String str) {
    if (!hasLength(str)) {
        return str;
    }
    StringBuilder sb = new StringBuilder(str);
    while (sb.length() > 0 && Character.isWhitespace(sb.charAt(sb.length() - 1))) {
        sb.deleteCharAt(sb.length() - 1);
    }
    return sb.toString();
}

From source file:Main.java

/**
 * Trim leading and trailing whitespace from the given String.
 * @param str the String to check/*from   w  ww  .j  av  a  2 s  .  c o  m*/
 * @return the trimmed String
 * @see java.lang.Character#isWhitespace
 */
public static String trimWhitespace(String str) {
    if (!hasLength(str)) {
        return str;
    }
    StringBuilder sb = new StringBuilder(str);
    while (sb.length() > 0 && Character.isWhitespace(sb.charAt(0))) {
        sb.deleteCharAt(0);
    }
    while (sb.length() > 0 && Character.isWhitespace(sb.charAt(sb.length() - 1))) {
        sb.deleteCharAt(sb.length() - 1);
    }
    return sb.toString();
}

From source file:Main.java

public static String removeBlanks(String content) {
    if (content == null) {
        return null;
    }/*from   w ww .  j ava 2s .c  o  m*/
    StringBuilder buff = new StringBuilder();
    buff.append(content);
    for (int i = buff.length() - 1; i >= 0; i--) {
        if (' ' == buff.charAt(i) || ('\n' == buff.charAt(i)) || ('\t' == buff.charAt(i))
                || ('\r' == buff.charAt(i))) {
            buff.deleteCharAt(i);
        }
    }
    return buff.toString();
}

From source file:Main.java

/**
 * Escapes backslashes ('\') with additional backslashes in a given String, returning a new, escaped String.
 *
 * @param value String to escape.   Cannot be null.
 * @return escaped String.  Never is null.
 *//*  ww w .  j  a v a  2 s.  co m*/
public static String escapeBackslashes(String value) {
    StringBuilder buf = new StringBuilder(value);
    for (int looper = 0; looper < buf.length(); looper++) {
        char curr = buf.charAt(looper);
        char next = 0;
        if (looper + 1 < buf.length())
            next = buf.charAt(looper + 1);

        if (curr == '\\') {
            if (next != '\\') { // only if not already escaped
                buf.insert(looper, '\\'); // escape backslash
            }
            looper++; // skip past extra backslash (either the one we added or existing)
        }
    }
    return buf.toString();
}

From source file:com.conwet.xjsp.json.JSONUtil.java

public static void discardSpaces(StringBuilder buffer) {
    while (buffer.length() > 0 && (isSpace(buffer.charAt(0)) || buffer.charAt(0) == ',')) {
        buffer.deleteCharAt(0);/*  w  ww. j av a 2 s  .c  om*/
    }
}

From source file:Main.java

private static String removeDotSegments(StringBuilder uri, int offset, int limit) {
    if (offset >= limit) {
        return uri.toString();
    }/*from  w w w  .  j  a  v  a  2s .c  o  m*/
    if (uri.charAt(offset) == '/') {
        offset++;
    }

    int segmentStart = offset;
    int i = offset;
    while (i <= limit) {
        int nextSegmentStart;
        if (i == limit) {
            nextSegmentStart = i;
        } else if (uri.charAt(i) == '/') {
            nextSegmentStart = i + 1;
        } else {
            i++;
            continue;
        }

        if (i == segmentStart + 1 && uri.charAt(segmentStart) == '.') {
            uri.delete(segmentStart, nextSegmentStart);
            limit -= nextSegmentStart - segmentStart;
            i = segmentStart;
        } else if (i == segmentStart + 2 && uri.charAt(segmentStart) == '.'
                && uri.charAt(segmentStart + 1) == '.') {
            int prevSegmentStart = uri.lastIndexOf("/", segmentStart - 2) + 2;
            int removeFrom = prevSegmentStart > offset ? prevSegmentStart : offset;
            uri.delete(removeFrom, nextSegmentStart);
            limit -= nextSegmentStart - removeFrom;
            i = prevSegmentStart;
        } else {
            i++;
            segmentStart = i;
        }
    }
    return uri.toString();
}

From source file:com.github.jknack.amd4j.ResourceURI.java

/**
 * Creates a {@link ResourceURI}./*  ww  w .  j a va  2 s. c  o  m*/
 *
 * @param baseUrl The base url.
 * @param path The dependency's path. It might be prefixed with: <code>prefix!</code> where
 *        <code>prefix</code> is usually a plugin.
 * @return A new {@link ResourceURI}.
 */
public static ResourceURI create(final String baseUrl, final String path) {
    notEmpty(baseUrl, "The baseUrl is required.");
    String normBaseUrl = baseUrl;
    if (".".equals(normBaseUrl)) {
        normBaseUrl = SEPARATOR;
    }
    if (!normBaseUrl.startsWith(SEPARATOR)) {
        normBaseUrl = SEPARATOR + normBaseUrl;
    }
    if (!normBaseUrl.endsWith(SEPARATOR)) {
        normBaseUrl += SEPARATOR;
    }
    int idx = Math.max(0, path.indexOf('!') + 1);
    StringBuilder uri = new StringBuilder(path);
    if (uri.charAt(idx) == SEPARATOR.charAt(0)) {
        uri.deleteCharAt(idx);
    }
    uri.insert(idx, normBaseUrl);
    return create(uri.toString());
}

From source file:com.jythonui.server.BUtil.java

private static String add(String path, String fName) {
    StringBuilder bu = new StringBuilder(path);
    int la = bu.length() - 1;
    char ch = bu.charAt(la);
    if (!isFSep(ch)) {
        return path + File.separator + fName;
    }/*from w  w  w.ja v a 2s .c om*/
    return path + fName;
}