Java: split() - 将“字符串”分割为若干个独立的“字符”
[root@rockylinux tmp]# uname -a
Linux rockylinux 4.18.0-348.20.1.el8_5.x86_64 #1 SMP Thu Mar 10 20:59:28 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
[root@rockylinux tmp]# javac -version
javac 17.0.1
[root@rockylinux tmp]# cat test.java
/**
* 日期:2022-04-11
*
* 功能:将“字符串”分割为若干单独的“字符”;
*
*
* 类:String
*
*
* 实现:
*
* String st = new String("www.runoob.com");
*
* // “字符串”分割后产生的若干个“字符”,保存在数组“return_string_array” 和“return_char_array”之内
* String[] return_string_array = st.split("");
* char[] return_char_array = st.toCharArray();
*
*
*
*/
public class test
{
public static void main(String[] args)
{
String st = new String( "www.runoob.com");
// "" :空分隔符,可以将“字符串”分割为若干单个“字符”
String[] retstr = st.split("");
char[] retchar = st.toCharArray();
// 输出原始字符串st
System.out.println("---- 原始字符串:st ----");
System.out.println("st=" + st);
// 输出retstr[]
System.out.println("---- st.split(\"\") ----");
int is = 0;
for(String x: retstr)
{
System.out.println("retstr[" + is + "]=" + x);
is += 1;
}
// 输出 retchar[]
System.out.println("---- st.toCharArray() ----");
int ic = 0;
for(char c: retchar)
{
System.out.println("retchar[" + ic + "]=" + c);
ic += 1;
}
}
}
[root@rockylinux tmp]# javac test.java && java test
---- 原始字符串:st ----
st=www.runoob.com
---- st.split("") ----
retstr[0]=w
retstr[1]=w
retstr[2]=w
retstr[3]=.
retstr[4]=r
retstr[5]=u
retstr[6]=n
retstr[7]=o
retstr[8]=o
retstr[9]=b
retstr[10]=.
retstr[11]=c
retstr[12]=o
retstr[13]=m
---- st.toCharArray() ----
retchar[0]=w
retchar[1]=w
retchar[2]=w
retchar[3]=.
retchar[4]=r
retchar[5]=u
retchar[6]=n
retchar[7]=o
retchar[8]=o
retchar[9]=b
retchar[10]=.
retchar[11]=c
retchar[12]=o
retchar[13]=m
[root@rockylinux tmp]#
[root@rockylinux tmp]#
[root@rockylinux tmp]#