字符串类 String
字符串是个常量(不可更改) 常量字符串
对字符串进行操作 都返回是一个新的字符串(原字符串不能更改)
public static void fun3() {
String string = "www.baidu.com";
boolean b1 = string.contains("baidu");
System.out.println(b1);
boolean b2 = string.startsWith("www");
System.out.println(b2);
boolean b3 = string.endsWith("com");
System.out.println(b3);
}
public static void fun2() {
String string1 = "wanglong";
String string2 = "a";
boolean b1 = string1.contains(string2);
System.out.println(b1);
String string3 = string1 + string2;
System.out.println(string3);
String newstring = string1.concat(string2);
System.out.println(newstring);
}
public static void fun1() {
String string = "wanglong";
char c = string.charAt(7);
System.out.println(c);
int index = string.indexOf('g');
System.out.println(index);
int index2 = string.indexOf('g', 4);
System.out.println(index2);
int index3 = string.indexOf("ng");
System.out.println(index3);
}
public static void main(String[] args) {
fun1();
fun2();
fun3();
}
public static void fun1() {
String string = "wanglong";
String replaceString = string.replace('w', 's');
System.out.println(replaceString);
String string2 = string.replace("long", "song");
System.out.println(string2);
}
public static void fun2() {
String string = " wang, long, z, n, b";
String[] strings = string.split("z");
for (int i = 0; i < strings.length; i++) {
System.out.println(strings[i]);
}
for (String string2 : strings) {
System.out.println(string2);
}
}
public static void fun3() {
String string = "wanglong";
String string2 = string.substring(5);
System.out.println(string2);
String string3 = string.substring(0, 4);
System.out.println(string3);
}
public static void fun4() {
String string = "wanglong";
String upperCase = string.toUpperCase();
System.out.println(upperCase);
String string2 = "DINGPENG";
String lowerCase = string2.toLowerCase();
System.out.println(lowerCase);
}
public static void fun5() {
String string = " abc def ";
String trim = string.trim();
System.out.println(trim);
}
public static void fun6() {
String s1 = "abc";
String s2 = "abc";
int compareTo = s1.compareTo(s2);
System.out.println(compareTo);
}
public static void fun7() {
String s1 = "wanglong";
String s2 = "wangLong";
boolean equalsIgnoreCase = s1.equalsIgnoreCase(s2);
System.out.println(equalsIgnoreCase);
}
public static void fun8() {
char[] array = {'w','a','n','g'};
String string = new String(array);
System.out.println(string);
int num = 5;
String newStr = String.valueOf(num);
System.out.println(newStr);
String string1 = "wanglong";
char[] charArray = string1.toCharArray();
for (char c : charArray) {
System.out.print(c);
}
String string3 = "wanglong";
boolean b = string3.isEmpty();
System.out.println(b);
System.out.println(string3.length());
}
举例:
计算字符串中 "wwwdasdwwwaswwwghwwwkl"
www出现的次数
public static void fun3() {
String string = "wwwdasdwwwaswwwghwwwkl";
String[] split = string.split("www");
System.out.println(split.length - 1);
不用系统类的写法:
int count = 0;
int index = 0;
String key = "www";
while (string.contains(key)) {
index = string.indexOf(key);
string = string.substring(index + key.length());
count++;
System.out.println(string);
}
System.out.println(count);
}
举例:
将字符串 " abc def "中的前后空格去掉
转化成 "abc def" 用代码实现(不使用系统方法)
1. 声明字符串
2. 可以将字符串转成字符数组
3. 声明开始坐标与结束坐标
4. 从开始坐标寻找到第一个不是空格的字符的索引
5. 从结尾坐标寻找到第一个不是空格的字符的索引
6. 根据开始和结束的索引 截取字符串 注意 : 留头尾问题
public static void fun2() {
String string = " abc def ";
char[] charArray = string.toCharArray();
int start = 0;
int end = string.length() - 1;
while (string.charAt(start) == ' ' && start < end) {
start++;
}
while (charArray[end] == ' ' && start < end) {
end--;
}
String substring = string.substring(start, end + 1);
System.out.println(substring);
}
举例:
将字符串 " abc def " 反转
public static void fun1() {
String string = " abc def ";
char[] charArray = string.toCharArray();
for (int i = 0; i < charArray.length / 2; i++) {
char temp = charArray[i];
charArray[i] = charArray[charArray.length - i -1];
charArray[charArray.length - i -1] = temp;
}
String newStr = new String(charArray);
System.out.println(newStr);
}