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:org.batoo.common.util.StringUtils.java

/**
 * Trim trailing whitespace from the given String.
 * //from w  ww. j a v a  2  s .  c o m
 * @param str
 *            the String to check
 * @return the trimmed String
 * @see java.lang.Character#isWhitespace
 */
public static String trimTrailingWhitespace(String str) {
    if (!StringUtils.hasLength(str)) {
        return str;
    }
    final 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:android.databinding.tool.LayoutXmlProcessor.java

/**
 * Generates a string identifier that can uniquely identify the given layout bundle.
 * This identifier can be used when we need to export data about this layout bundle.
 *//*w  w w  .  j  av a2 s  . com*/
public String generateExportFileName(ResourceBundle.LayoutFileBundle layout) {
    StringBuilder name = new StringBuilder(layout.getFileName());
    name.append('-').append(layout.getDirectory());
    for (int i = name.length() - 1; i >= 0; i--) {
        char c = name.charAt(i);
        if (c == '-') {
            name.deleteCharAt(i);
            c = Character.toUpperCase(name.charAt(i));
            name.setCharAt(i, c);
        }
    }
    return name.toString();
}

From source file:org.batoo.common.util.StringUtils.java

/**
 * Trim leading and trailing whitespace from the given String.
 * //from  w w  w.ja  v a 2s .  c  o m
 * @param str
 *            the String to check
 * @return the trimmed String
 * @see java.lang.Character#isWhitespace
 */
public static String trimWhitespace(String str) {
    if (!StringUtils.hasLength(str)) {
        return str;
    }
    final 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:org.cafed00d.subtitle.FileProcessor.java

/**
 * Examines the line of text given looking for words. It recognizes that a
 * word is starting when it comes across a letter. Once it finds a letter, it
 * passes control to {@link WordProcessor} which will extract the current word
 * and determine if it can be corrected.
 * //from  ww w . j a  v a2  s  .  c o  m
 * @param line
 *          The line of text to process.
 * @return The processed, corrected, line of text.
 */
private String processLine(String line) {
    StringBuilder result = new StringBuilder(line);
    for (int i = 0; i < result.length(); i++) {
        if (Character.isLetter(result.charAt(i))) {
            WordProcessor word = new WordProcessor(result, i);
            i = word.process();
            if (word.isCorrectionMade()) {
                correctedCount++;
                if (generateLog) {
                    if (!correctedWords.containsKey(word.getOriginalWord())) {
                        correctedWords.put(word.getOriginalWord(), word.getCorrectedWord());
                    }
                }
            }
            wordCount++;
        }
    }
    return result.toString();
}

From source file:com.autentia.wuija.web.DownloadServlet.java

/**
 * Process GET request/*from  www  .  jav  a 2 s .  c  o  m*/
 * 
 * @param request HTTP request
 * @param response HTTP response
 * @throws javax.servlet.ServletException
 */
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException {
    final String requestUri = request.getRequestURI();
    final String fileName = requestUri.substring(requestUri.lastIndexOf('/') + 1, requestUri.length());

    if (log.isTraceEnabled()) {
        log.trace("fileName='" + fileName + "'.");
    }

    if (fileName == null) {
        final String msg = "Filename not found in the requested URL.";
        log.error(msg);
        throw new ServletException(msg);
    }

    final StringBuilder tempFilePath = new StringBuilder(System.getProperty("java.io.tmpdir"));

    if (log.isTraceEnabled()) {
        log.trace("tempdir = " + tempFilePath);
    }

    if (tempFilePath.charAt(tempFilePath.length() - 1) != File.separatorChar) {
        tempFilePath.append(File.separatorChar);
    }

    tempFilePath.append(fileName);

    if (log.isTraceEnabled()) {
        log.trace("filePath = " + tempFilePath);
    }

    final File file = new File(tempFilePath.toString());
    file.deleteOnExit();

    String clientFileName = request.getHeader(FILE_NAME);
    if (clientFileName == null) {
        clientFileName = file.getName();
    }
    response.setHeader("Pragma", "no-cache");
    response.setHeader("Content-disposition", "attachment; filename=" + clientFileName);

    // XXX [wuija]: reemplazar por una librera de mimetypes
    if (clientFileName.toLowerCase().endsWith(".csv")) {
        response.setContentType("application/vnd.ms-excel");
    } else if (clientFileName.toLowerCase().endsWith(".zip")) {
        response.setContentType("application/zip");
    } else if (clientFileName.toLowerCase().endsWith(".pdf")) {
        response.setContentType("application/pdf");
    }
    response.setContentLength((int) file.length());

    try {
        int read = 0;
        final byte[] bytes = new byte[1024];
        final FileInputStream in = new FileInputStream(file);

        while ((read = in.read(bytes)) != -1) {
            response.getOutputStream().write(bytes, 0, read);
        }
        in.close();

        log.trace("The contents of the file have been written.");

        response.getOutputStream().flush();
        response.getOutputStream().close();
    } catch (IOException e) {
        final String msg = "Error writting the contents of the file.";
        log.error(msg);
        throw new ServletException(msg, e);
    }

    file.delete();
    log.trace("File='" + file + "' deleted.");

}

From source file:net.sourceforge.fenixedu.util.UniqueAcronymCreator.java

private static StringBuilder appendLastChar(int index, StringBuilder acronym) {
    if (logger.isDebugEnabled()) {
        logger.info("appendLastChar, called with index " + index + " and " + acronym);
    }/* w  w  w. ja va  2 s . co  m*/

    for (int i = splitsName.length - 1; i > -1; i--) {
        if (!(isValidAcception(splitsName[i])) && splitsName[i].length() > index) {
            String toAppend = (splitsName[i].substring(index, index + 1));
            toAppend = (toLowerCase) ? toAppend.toLowerCase() : toAppend.toUpperCase();

            if (acronym.toString().contains("-")) {
                int hiffen = acronym.toString().indexOf("-");
                if (isValidNumeration(String.valueOf(acronym.charAt(hiffen + 1)))
                        || hasNumber(String.valueOf(acronym.charAt(hiffen + 1)))) {
                    acronym.insert(hiffen, toAppend);
                    if (logger.isDebugEnabled()) {
                        logger.info("appendLastChar, found a '-', appending before hiffen " + toAppend);
                    }
                } else {
                    acronym.append(toAppend);
                    if (logger.isDebugEnabled()) {
                        logger.info("appendLastChar, found a '-', appending in end " + toAppend);
                    }
                }
            } else {
                if (logger.isDebugEnabled()) {
                    logger.info("appendLastChar, appending " + toAppend);
                }
                acronym.append(toAppend);
            }

            break;
        }
    }

    return acronym;
}

From source file:edu.chalmers.dat076.moviefinder.service.TitleParser.java

/**
 * Removes everything starting from index n until the char c. If interval
 * contains a positive 4 digit number (and nothing else) it is returned,
 * otherwise -1 is returned.//  w w w .  j  av  a  2 s  .c  o m
 *
 * @param mySb
 * @param n
 * @param c
 * @return if interval contains a number it is returned
 */
public int removeUntil(StringBuilder mySb, int n, char c) {
    int re = -1;

    for (int i = n; i <= mySb.length(); i++) {
        if (mySb.charAt(i) == c) {
            if (i - n == 5) {
                re = checkForYear(mySb.subSequence(n + 1, i));
            }
            mySb.delete(n, i + 1);
            break;
        }
    }
    return re;
}

From source file:org.batoo.common.util.StringUtils.java

/**
 * Trim <i>all</i> whitespace from the given String: leading, trailing, and inbetween characters.
 * //from   w  w w  .  j  a v  a2s.  c  om
 * @param str
 *            the String to check
 * @return the trimmed String
 * @see java.lang.Character#isWhitespace
 */
public static String trimAllWhitespace(String str) {
    if (!StringUtils.hasLength(str)) {
        return str;
    }
    final StringBuilder sb = new StringBuilder(str);
    int index = 0;
    while (sb.length() > index) {
        if (Character.isWhitespace(sb.charAt(index))) {
            sb.deleteCharAt(index);
        } else {
            index++;
        }
    }
    return sb.toString();
}

From source file:org.auraframework.test.AuraTestingUtil.java

/**
 * Make a UID be incorrect.//  ww w  .  ja v  a  2 s . c  o  m
 */
protected String modifyUID(String old) {
    StringBuilder sb = new StringBuilder(old);
    char flip = sb.charAt(3);

    // change the character.
    if (flip == 'a') {
        flip = 'b';
    } else {
        flip = 'a';
    }
    sb.setCharAt(3, flip);
    return sb.toString();
}

From source file:org.apache.lens.api.jaxb.YAMLToStringStrategy.java

private StringBuilder appendNewLine(StringBuilder buffer) {
    if (buffer.length() != 0 && buffer.charAt(buffer.length() - 1) != '\n') {
        while (buffer.charAt(buffer.length() - 1) == ' ') {
            buffer.setLength(buffer.length() - 1);
        }//from w ww .  j a v a 2  s.co m
        buffer.append("\n");
    }
    return buffer;
}