Example usage for java.lang String getChars

List of usage examples for java.lang String getChars

Introduction

In this page you can find the example usage for java.lang String getChars.

Prototype

public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) 

Source Link

Document

Copies characters from this string into the destination character array.

Usage

From source file:MainClass.java

public static void main(String args[]) {
    String s1 = "hello there";
    char charArray[] = new char[5];

    System.out.printf("s1: %s", s1);

    for (int count = s1.length() - 1; count >= 0; count--)
        System.out.printf("%s ", s1.charAt(count));

    // copy characters from string into charArray
    s1.getChars(0, 5, charArray, 0);
    System.out.print("\nThe character array is: ");

    for (char character : charArray)
        System.out.print(character);
}

From source file:Util.java

/**
 * Converts a method name into a camel-case field name, starting from {@code start}.
 */// w w  w .  j a v  a 2s . c  o  m
public static String toProperty(int start, String methodName) {
    char[] prop = new char[methodName.length() - start];
    methodName.getChars(start, methodName.length(), prop, 0);
    int firstLetter = prop[0];
    prop[0] = (char) (firstLetter < 91 ? firstLetter + 32 : firstLetter);
    return new String(prop);
}

From source file:Main.java

public static int getDepth(String value) {
    int len = value.length();
    int levels = 0;
    char[] chars = new char[len];
    value.getChars(0, len, chars, 0);
    for (int i = 0; i < len; i++) {
        if (chars[i] == '/' && (i + 1 < len) && chars[i + 1] != '/') {
            levels++;//w  w  w .j a  va2s  .c o  m
        }
    }
    return levels;
}

From source file:Main.java

public static String toFormXml(Object obj) {
    if (obj == null) {
        return "";
    }//from   w ww  .j a v  a  2 s  .  c  om
    String message = obj.toString();
    char content[] = new char[message.length()];
    message.getChars(0, message.length(), content, 0);
    StringBuffer result = new StringBuffer(content.length + 50);
    for (int i = 0; i < content.length; i++) {
        switch (content[i]) {
        case '<':
            result.append("&lt;");
            break;
        case '>':
            result.append("&gt;");
            break;
        case '&':
            result.append("&amp;");
            break;
        case '"':
            result.append("&quot;");
            break;
        default:
            result.append(content[i]);
        }
    }
    return (result.toString());
}

From source file:Main.java

/**
 * @param hexString max 4 characters length
 * @return same string with leading zeros
 *//*from  w  ww  . j a v a  2s. c o  m*/
public static char[] leading4Zeros(String hexString) {
    char[] chars = "0000".toCharArray();
    int length = hexString.length();
    hexString.getChars(0, length, chars, 4 - length);
    return chars;
}

From source file:Main.java

/**
 * Replaces every \n, \r and \t with a single space. Afterwards multiples spaces
 * are merged into a single space. Finally leading spaces are deleted.
 *
 * This method must be fast, but may not lead to the shortest replacement String.
 *
 * You are only allowed to change this code if you can prove it became faster on a device.
 * see cgeo.geocaching.test.WhiteSpaceTest#replaceWhitespaceManually in the test project
 *
 * @param data//ww  w .ja  v  a2  s.  c o m
 *            complete HTML page
 * @return the HTML page as a very long single "line"
 */
public static String replaceWhitespace(final String data) {
    final int length = data.length();
    final char[] chars = new char[length];
    data.getChars(0, length, chars, 0);
    int resultSize = 0;
    boolean lastWasWhitespace = true;
    for (char c : chars) {
        if (c == ' ' || c == '\n' || c == '\r' || c == '\t') {
            if (!lastWasWhitespace) {
                chars[resultSize++] = ' ';
            }
            lastWasWhitespace = true;
        } else {
            chars[resultSize++] = c;
            lastWasWhitespace = false;
        }
    }
    return String.valueOf(chars, 0, resultSize);
}

From source file:Main.java

private static String cut(String s, int begin, int end) {
    int l = s.length();
    char[] ch = new char[l - (end - begin)];
    s.getChars(0, begin, ch, 0);
    s.getChars(end, l, ch, begin);/*from www  .  j  a v a  2s .c  om*/
    return new String(ch);
}

From source file:Main.java

private static String tzid(String s) {
    int length = s.length();
    if (length > 4) {
        char[] tzid = { 'G', 'M', 'T', 0, 0, 0, ':', 0, 0 };
        s.getChars(length - 5, length - 2, tzid, 3);
        s.getChars(length - 2, length, tzid, 7);
        if ((tzid[3] == '+' || tzid[3] == '-') && Character.isDigit(tzid[4]) && Character.isDigit(tzid[5])
                && Character.isDigit(tzid[7]) && Character.isDigit(tzid[8])) {
            return new String(tzid);
        }/*from   w ww  . ja  v a  2 s.c o  m*/
    }
    return null;
}

From source file:HTMLFilter.java

/**
 * Filter the specified message string for characters that are sensitive
 * in HTML.  This avoids potential attacks caused by including JavaScript
 * codes in the request URL that is often reported in error messages.
 *
 * @param message The message string to be filtered
 *///from w w w .jav  a  2  s  . c  o  m
public static String filter(String message) {

    if (message == null)
        return (null);

    char content[] = new char[message.length()];
    message.getChars(0, message.length(), content, 0);
    StringBuffer result = new StringBuffer(content.length + 50);
    for (int i = 0; i < content.length; i++) {
        switch (content[i]) {
        case '<':
            result.append("&lt;");
            break;
        case '>':
            result.append("&gt;");
            break;
        case '&':
            result.append("&amp;");
            break;
        case '"':
            result.append("&quot;");
            break;
        default:
            result.append(content[i]);
        }
    }
    return (result.toString());

}

From source file:com.adguard.commons.lang.StringHelperUtils.java

public static String lowerCaseAscii(String s) {
    if (s == null)
        return null;

    int len = s.length();
    char[] buf = new char[len];
    s.getChars(0, len, buf, 0);
    for (int i = 0; i < len; i++) {
        if (buf[i] >= 'A' && buf[i] <= 'Z')
            buf[i] += 0x20;//from  w w  w  . j a  v  a  2  s  .  c  o  m
    }

    return new String(buf);
}