packageDemo02;/**
* @author 270
* String当中与转换相关的常用方法有:
* <p>
* public char[] tocharArray():将当前字符串拆分成字符数组作为返回值
* public byte[] getBytes():获得当前字符串底层的字节数组。
* public String replace(CharSequence oldString,CharSequence newString)
* 将所有字符串替换成为新的字符串,返回替换之后的结果新字符串
* 【备注】:CharSequence意思就是说可以接受字符串类型
*/publicclassDemo03StringConvert{publicstaticvoidmain(String[] args){char[] ch ="Hello".toCharArray();System.out.println(ch.length);for(int i =0; i < ch.length; i++){System.out.println(ch[i]);}System.out.println("==============");//转换为字节数组byte[] bytes ="abc".getBytes();System.out.println(bytes);//显示bytes指向的地址for(int i =0; i < bytes.length; i++){System.out.println(bytes[i]);//bytes数组显示的是a,b,c对应的ASCI码值}System.out.println("==============");//替换字符串内容生成新的字符串String str1 ="What's your name ?";String str2 = str1.replace("a","t");System.out.println(str1);System.out.println(str2);System.out.println("==========");}}
打印结果
5H
e
l
l
o
==============[B@48140564979899==============What's your name ?Whtt's your ntme ?==========
字符串分隔方法
packageDemo02;/**
* @author 270
* 分割字符串的方法:
* public String[] split(String regex):按照参数的规则,将字符串切分成若干部分
*split方法其实是一个"正则表达式";,今后学习
* 【注意】:如果要按照英文句点,"."进行切分必须写\\.(两个反斜杠)
* 因为"."在正则表达式中有特殊含义
*/publicclassDemo04StringSplit{publicstaticvoidmain(String[] args){String str1 ="aaa,bbb,ccc";String[] array1 = str1.split(",");for(int i =0; i < array1.length; i++){System.out.println(array1[i]);}System.out.println("==============");String str2 ="aaa bbb ccc";String[] array2 = str2.split(" ");for(int i =0; i < array2.length; i++){System.out.println(array2[i]);}String str3 ="XXX.YYY.ZZZ";String[] array3 = str3.split(".");System.out.println(array3.length);for(int i =0; i < array3.length; i++){System.out.println(array3[i]);}System.out.println("=========");String[] array4 = str3.split("\\.");for(int i =0; i < array4.length; i++){System.out.println(array4[i]);}}}————输出结果:
aaa
bbb
ccc
==============
aaa
bbb
ccc
0XXXYYYZZZ