/*
字符串的截取方法(根据一个大字符串,截取得到小字符串,其中一部分)
public String substring(int beginIndex):截取字符串,从指定的索引位置一直到字符串末尾。
public String substring(int beginIndex, int endIndex):截取字符串,从指定的开始索引,一直到指定的结束索引。
两个参数的方法,左闭右开区间,包含左边,不包含右边。
*/
public class Demo05Substring {
public static void main(String[] args) {
String str1 = "HelloWorld";
String str2 = str1.substring(5);
System.out.println(str1); // HelloWorld
System.out.println(str2); // World
String str3 = str1.substring(5, 8);
System.out.println(str3); // Wor
}}
/*
字符串的分割方法:
public String[] split(String regex):根据参数为标准,切割成为若干段儿字符串,也就是字符串数组。
参数regex其实代表“正则表达式”,这是一种用来描述规则的规范。
如果希望用英文句点进行切分,那么参数必须写成"\\.",这是一个特例。
因为英文句点在正则表达式当中有特殊含义。
*/
public class Demo07StringSplit {
public static void main(String[] args) {
String strA = "aaa,bbb,ccc";
String[] arrayA = strA.split(",");
for (int i = 0; i < arrayA.length; i++) {
System.out.println(arrayA[i]); //
}
System.out.println("============");
String strB = "XXX OOO XXX";
String[] arrayB = strB.split(" ");
for (int i = 0; i < arrayB.length; i++) {
System.out.println(arrayB[i]);
}
System.out.println("============");
String strC = "XXX.YYY.ZZZ";
String[] arrayC = strC.split("\\.");
System.out.println(arrayC.length); // 3
for (int i = 0; i < arrayC.length; i++) {
System.out.println(arrayC[i]);
}
}
}
package cn.itcast.day08.demo01;
/*
字符串当中与转换相关的方法:
public char[] toCharArray():将字符串拆分成为字符数组
public byte[] getBytes():将字符串转换成为底层的字节数组
public String replace(CharSequence target, CharSequence replacement):将字符串当中指定的内容,全都替换成为新内容。
*/
public class Demo06StringConvert {
public static void main(String[] args) {
String str1 = "Hello";
// 拆分成为字符数组
char[] chars = str1.toCharArray();
for (int i = 0; i < chars.length; i++) {
System.out.println(chars[i]);
}
System.out.println("=============");
// 拆分成为字节数组
byte[] bytes = str1.getBytes();
for (int i = 0; i < bytes.length; i++) {
System.out.println(bytes[i]);
}
System.out.println("=============");
// 替换
String str2 = "How do you do?";
String str3 = str2.replace("o", "*");
System.out.println(str2); // How do you do?
System.out.println(str3); // H*w d* y*u d*?
System.out.println("=============");
String msg = "会不会玩儿啊!你大爷的!你大爷的!你大爷的!!!";
String after = msg.replace("你大爷的", "****");
System.out.println(after); // 会不会玩儿啊!****!****!****!!!
}
}