java 从字符串中提取字符

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 从字符串中提取字符相关的知识,希望对你有一定的参考价值。

public class Main {

    public static void main(String[] args) {

        //Character Extraction - Getting characters out of a String object
        String s1 = "Larry";
        char letter = s1.charAt(2); //Gets a single character at a given index
        System.out.println(letter);

        String s2 = "I am a long string";
        char[] chars = new char[4]; //Must be long enough to account for the num of chars you will be extracting
        //Gets multiple chars from a String object.
        s2.getChars(7, 11, chars, 0); // 1: Where to begin 2: Index one higher than where to stop. 3: Where to put the result 4: What index the char array to put the result
        System.out.println(new String(chars));

        byte[] bytes= s2.getBytes();
        System.out.println(new String(bytes));

        char[] charArray = s2.toCharArray();
        System.out.println(charArray);

        // ---List---
        //charAt - Gets a single character at a given index
        //getChars - Gets multiple chars from a String object.
        //getBytes - Gets a byte array from a String object
        //toCharArray - Gets a char array from a String object

    }
}

以上是关于java 从字符串中提取字符的主要内容,如果未能解决你的问题,请参考以下文章

java 从字符串中提取字符

JAVA中如何从字符串中提取一个整数

如何从Java中的字符串中提取大写子字符串?

Java如何从字符串中提取数字

从 URL 中提取部分字符串 - Java Regex

Java如何从字符串中提取数字