java learning - convert a string of related methods

/ *
String among common methods associated with conversion are:

public char [] toCharArray (): the current string of characters become split array as the return value.
public byte [] getBytes (): get the current byte string underlying array.
public String replace (CharSequence oldString, CharSequence newString):
all the old string replacement appears to be a new string and returns a new string of results after the replacement.
Note: CharSequence meaning that can accept a string type.
* /

public class Demo04StringConvert {
    public static void main(String[] args) {
        // 转换成为字符数组
        char[] chars = "Hello".toCharArray();
        System.out.println(chars[0]);//H
        System.out.println(chars.length);//5
        System.out.println("================");


        // // 转换成为字节数组
        byte[] bytes = "abc".getBytes();
        for (int i = 0; i < bytes.length; i++) {
            System.out.println(bytes[i]);
        }
        System.out.println("================");

        // 字符串的内容替换
        String str1 = "How do you do?";
        String str2 = str1.replace("o", "*");
        System.out.println(str1);//How do you do?
        System.out.println(str2);//H*w d* y*u d*?
        System.out.println("==============");

        String lang1 = "会不会玩儿呀!你大爷的!你大爷的!你大爷的!!!";
        String lang2 = lang1.replace("你大爷的", "****");
        System.out.println(lang1);//会不会玩儿呀!你大爷的!你大爷的!你大爷的!!!

        System.out.println(lang2);//会不会玩儿呀!****!****!****!!!
    }
}
Published 23 original articles · won praise 0 · Views 156

Guess you like

Origin blog.csdn.net/qq_44813352/article/details/104314908