Example usage for java.lang CharSequence charAt

List of usage examples for java.lang CharSequence charAt

Introduction

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

Prototype

char charAt(int index);

Source Link

Document

Returns the char value at the specified index.

Usage

From source file:org.codehaus.groovy.grails.web.pages.Parse.java

private void write(CharSequence text, boolean gsp) {
    if (!gsp) {//from ww w  . ja v a2s.  c  om
        out.print(text);
        return;
    }
    for (int ix = 0, ixz = text.length(); ix < ixz; ix++) {
        char c = text.charAt(ix);
        String rep = null;
        if (Character.isWhitespace(c)) {
            for (ix++; ix < ixz; ix++) {
                if (Character.isWhitespace(text.charAt(ix)))
                    continue;
                ix--;
                rep = " ";
                break;
            }
        } else if (c == '&') {
            if (match("&semi;", text, ix)) {
                rep = ";";
                ix += 5;
            } else if (match("&amp;", text, ix)) {
                rep = "&";
                ix += 4;
            } else if (match("&lt;", text, ix)) {
                rep = "<";
                ix += 3;
            } else if (match("&gt;", text, ix)) {
                rep = ">";
                ix += 3;
            }
        } else if (c == '<') {
            if (match("<br>", text, ix) || match("<hr>", text, ix)) {
                rep = "\n";
                incrementLineNumber();
                ix += 3;
            } else {
                int end = match(PARA_BREAK, text, ix);
                if (end <= 0)
                    end = match(ROW_BREAK, text, ix);
                if (end > 0) {
                    rep = "\n";
                    incrementLineNumber();
                    ix = end;
                }
            }
        }
        if (rep != null)
            out.print(rep);
        else
            out.print(c);
    }
}

From source file:org.archive.util.UriUtils.java

protected static boolean isLikelyFalsePositive(CharSequence candidate) {
    if (TextUtils.matches("(?:text|application)/[^/]+", candidate)) {
        if (LOGGER.isLoggable(Level.FINE)) {
            LOGGER.fine("rejected: looks like an application or text mimetype: " + candidate);
        }/*from ww  w . j a  v  a2 s .c  o m*/
        return true;
    }

    for (String s : AUDIO_VIDEO_IMAGE_MIMETYPES) {
        if (s.contentEquals(candidate)) {
            if (LOGGER.isLoggable(Level.FINE)) {
                LOGGER.fine("rejected: looks like an audio video or image mimetype: " + candidate);
            }
            return true;
        }
    }

    if (TextUtils.matches("\\d+(?:\\.\\d+)*", candidate)) {
        if (LOGGER.isLoggable(Level.FINE)) {
            LOGGER.fine("rejected: looks like a decimal number: " + candidate);
        }
        return true;
    }

    if (TextUtils.matches(".*[$()'\"\\[\\]{}|].*", candidate)) {
        if (LOGGER.isLoggable(Level.FINE)) {
            LOGGER.fine("rejected: contains unusual characters: " + candidate);
        }
        return true;
    }

    // starting or ending with + particularly common because of string concatenation in javascript
    if (TextUtils.matches("^[,;+:].*|.*[.,;+:]$", candidate)) {
        if (LOGGER.isLoggable(Level.FINE)) {
            LOGGER.fine("rejected: starts or ends with an unusual starting or ending character: " + candidate);
        }
        return true;
    }
    if (candidate.charAt(0) == '.' && !TextUtils.matches("^\\.{1,2}/.*", candidate)) {
        if (LOGGER.isLoggable(Level.FINE)) {
            LOGGER.fine("rejected: starts with '.' (but not './' or '../'): " + candidate);
        }
        return true;
    }

    if (TextUtils.matches("^.*[^:]//.*$", candidate)) {
        if (LOGGER.isLoggable(Level.FINE)) {
            LOGGER.fine("rejected: contains '//' (but not '://'): " + candidate);
        }
        return true;
    }

    // look for things that look like hostnames and not filenames?
    // look for too many dots but make sure we take into account that url may have hostname?

    if (LOGGER.isLoggable(Level.FINE)) {
        LOGGER.fine("accepted: does not look like a false positive: " + candidate);
    }

    return false;
}

From source file:com.jecelyin.editor.v2.core.text.TextUtils.java

public static int indexOf(CharSequence s, CharSequence needle, int start, int end) {
    int nlen = needle.length();
    if (nlen == 0)
        return start;

    char c = needle.charAt(0);

    for (;;) {//  w w w  .  ja v  a  2 s.  c  o m
        start = indexOf(s, c, start);
        if (start > end - nlen) {
            break;
        }

        if (start < 0) {
            return -1;
        }

        if (regionMatches(s, start, needle, 0, nlen)) {
            return start;
        }

        start++;
    }
    return -1;
}

From source file:com.github.irshulx.Components.InputExtensions.java

public CharSequence noTrailingwhiteLines(CharSequence text) {
    if (text.length() == 0)
        return text;
    while (text.charAt(text.length() - 1) == '\n') {
        text = text.subSequence(0, text.length() - 1);
    }//  w w  w  .j  a v  a 2s.  c  om
    return text;
}

From source file:com.github.irshulx.Components.InputExtensions.java

public CharSequence noLeadingwhiteLines(CharSequence text) {
    if (text.length() == 0)
        return text;
    while (text.charAt(0) == '\n') {
        text = text.subSequence(1, text.length());
    }//  w w w  .  ja v  a  2s .  co m
    return text;
}

From source file:com.uzmap.pkg.uzmodules.UISearchBar.SearchBarActivity.java

public boolean isBlank(CharSequence cs) {
    int strLen;/*w ww  .  j av a  2 s  .c o  m*/
    if ((cs == null) || ((strLen = cs.length()) == 0))
        return true;
    for (int i = 0; i < strLen; i++) {
        if (!Character.isWhitespace(cs.charAt(i))) {
            return false;
        }
    }
    return true;
}

From source file:com.jecelyin.editor.v2.core.text.TextUtils.java

public static int getOffsetBefore(CharSequence text, int offset) {
    if (offset == 0)
        return 0;
    if (offset == 1)
        return 0;

    char c = text.charAt(offset - 1);

    if (c >= '\uDC00' && c <= '\uDFFF') {
        char c1 = text.charAt(offset - 2);

        if (c1 >= '\uD800' && c1 <= '\uDBFF')
            offset -= 2;/*w  w  w.  j a va2s . c o  m*/
        else
            offset -= 1;
    } else {
        offset -= 1;
    }

    if (text instanceof Spanned) {
        ReplacementSpan[] spans = ((Spanned) text).getSpans(offset, offset, ReplacementSpan.class);

        for (int i = 0; i < spans.length; i++) {
            int start = ((Spanned) text).getSpanStart(spans[i]);
            int end = ((Spanned) text).getSpanEnd(spans[i]);

            if (start < offset && end > offset)
                offset = start;
        }
    }

    return offset;
}

From source file:net.iiit.siel.analysis.lang.LanguageIdentifier.java

/**
 * Check char sequence.//from  ww w  .  j  a v  a 2s.c  o  m
 *
 * @param seq the seq
 * @return the boolean
 */
private Boolean checkCharSequence(CharSequence seq) {

    Boolean isForeign = true;
    char underScore = '_';
    for (int j = 0; j < seq.length(); j++) {
        if (seq.charAt(j) == underScore) {
            continue;
        }
        UnicodeBlock currentUnicode = Character.UnicodeBlock.of(seq.charAt(j));

        if (currentUnicode == Character.UnicodeBlock.BENGALI)
            isForeign = false;
        else if (currentUnicode == Character.UnicodeBlock.TELUGU)
            isForeign = false;
        else if (currentUnicode == Character.UnicodeBlock.TAMIL)
            isForeign = false;
        else if (currentUnicode == Character.UnicodeBlock.DEVANAGARI)
            isForeign = false;
        else if (currentUnicode == Character.UnicodeBlock.GURMUKHI)
            isForeign = false;
        else if (currentUnicode == Character.UnicodeBlock.GUJARATI)
            isForeign = false;
        else if (currentUnicode == Character.UnicodeBlock.ORIYA)
            isForeign = false;
        else if (seq.charAt(j) >= 0 && seq.charAt(j) <= 127)
            isForeign = false;
        /*
        *                          * Return if it is a foreign character, because seq can be composed
        *                                                   * of indic and foreign characters.
        *                                                                            */
        else
            return isForeign;
    }
    return isForeign;
}

From source file:edu.cornell.med.icb.goby.util.SimulateBisulfiteReads.java

protected void process(CharSequence segmentBases, int from, Writer writer) throws IOException {

    int segmentLength = segmentBases.length();
    for (int repeatCount = 0; repeatCount < numRepeats; repeatCount++) {
        int startReadPosition = choose(0, Math.max(0, segmentBases.length() - 1 - readLength));
        boolean matchedReverseStrand = doReverseStrand && doForwardStrand ? random.nextBoolean()
                : doReverseStrand;/*from   w ww. j  ava  2s. co  m*/
        if (matchedReverseStrand && !doReverseStrand)
            continue;
        if (!matchedReverseStrand && !doForwardStrand)
            continue;

        final CharSequence selectedReadRegion = segmentBases.subSequence(startReadPosition,
                startReadPosition + readLength);
        CharSequence readBases = matchedReverseStrand ? reverseComplement(selectedReadRegion)
                : selectedReadRegion;

        MutableString sequenceInitial = new MutableString();
        MutableString sequenceTreated = new MutableString();
        MutableString log = new MutableString();
        IntArrayList mutatedPositions = new IntArrayList();

        for (int i = 0; i < readLength; i++) {

            char base = readBases.charAt(i);
            // genomic position is zero-based
            int genomicPosition = matchedReverseStrand ? readLength - (i + 1) + from + startReadPosition
                    : i + startReadPosition + from;
            sequenceInitial.append(base);

            if (base == 'C') {

                boolean isBaseMethylated = random
                        .nextDouble() <= getMethylationRateAtPosition(matchedReverseStrand, genomicPosition);

                if (isBaseMethylated) {
                    // base is methylated, stays a C on forward or reverse strand
                    if (!bisulfiteTreatment) {
                        // mutate base to G
                        // introduce mutation C -> G
                        base = 'G';

                    }
                    // bases that are methylated are protected and stay C on the forward strand. They would also
                    // be seen as G on the opposite strand if the sequencing protocol did not respect strandness
                    log.append(bisulfiteTreatment ? "met: " : "mut: ");
                    log.append(genomicPosition + 1); // write 1-based position
                    log.append(' ');

                    log.append("read-index: ");
                    log.append(i + 1);
                    log.append(' ');
                    mutatedPositions.add(genomicPosition);

                } else {
                    // bases that are not methylated are changed to T through the bisulfite and PCR conversion steps
                    if (bisulfiteTreatment) {
                        base = 'T';

                    }

                }
            }
            sequenceTreated.append(base);
        }
        MutableString coveredPositions = new MutableString();
        MutableString qualityScores = new MutableString();
        for (int i = 0; i < readLength; i++) {
            final char c = QualityEncoding.ILLUMINA.phredQualityScoreToAsciiEncoding((byte) 40);
            qualityScores.append(c);

        }
        // zero-based positions covered by the read:
        IntArrayList readCoveredPositions = new IntArrayList();

        for (int i = startReadPosition + from; i < startReadPosition + from + readLength; i++) {
            // positions are written 1-based
            coveredPositions.append(i + 1);
            coveredPositions.append(" ");
            readCoveredPositions.add(i);
        }

        readCoveredPositions.retainAll(mutatedPositions);
        assert readCoveredPositions.size() == mutatedPositions
                .size() : "positions mutated or changed must be covered by read.";
        //   System.out.printf("initial: %s%nbis:     %s%n", sequenceInitial, sequenceTreated);
        writer.write(String.format("@%d reference: %s startPosition: %d strand: %s %s %s%n%s%n+%n%s%n",
                repeatCount, refChoice, startReadPosition, matchedReverseStrand ? "-1" : "+1", log,
                coveredPositions, complement(sequenceTreated), qualityScores));
    }
    writer.flush();

}

From source file:StringUtil.java

public static boolean isCharAtEqual(CharSequence source, int index, char match) {
    if ((index < 0) || (index >= source.length())) {
        return false;
    }/*from ww w  . j a v a 2 s.  c  o m*/
    return source.charAt(index) == match;
}