package String;
import java.util.Arrays;
import java.util.Locale;
public class Demo {
public static void main(String[] args) {
//String
//1.定义
System.out.println("定义:");
String s="Hello World";
System.out.println(s);
System.out.println("===");
//方法
//1.indexOf(字符/字符串):第一次出现的索引
System.out.println("1.indexOf(字符/字符串):第一次出现的索引:");
String str="abcacd";
System.out.println(str.indexOf('a'));
System.out.println(str.indexOf("acd"));
System.out.println("===");
//2.lastIndexOf(字符/字符串):最后一次出现的索引
System.out.println("2.lastIndexOf(字符/字符串):最后一次出现的索引:");
String str1="abcabcd";
System.out.println(str1.lastIndexOf('a'));
System.out.println(str1.lastIndexOf("ab"));
System.out.println("===");
//3.charAt(int index):返回对应字符
System.out.println("3.charAt(int index):返回对应字符:");
String str2="abcabc";
System.out.println(str2.charAt(0));
System.out.println("===");
//4.endsWith(String str):判断是否以指定字符串结尾
System.out.println("4.endsWith(String str):判断是否以指定字符串结尾:");
String str3="abc123";
System.out.println(str3.endsWith("123"));
System.out.println("===");
//5.length():返回字符串长度
System.out.println("5.length():返回字符串长度:");
String str4="abc123";
System.out.println(str4.length());
System.out.println("===");
//6.equals(String s):判断字符串内容是否相等
System.out.println("6.equals(String s):判断字符串内容是否相等:");
String s1="123hello";
String s2="123hello";
System.out.println(s1.equals(s2));
System.out.println("===");
//7.isEmpty():判断字符串是否为空
System.out.println("7.isEmpty():判断字符串是否为空:");
String s3="";
System.out.println(s3.isEmpty());
System.out.println("===");
//8.startsWith(String s):判断是否以指定字符串开头
System.out.println("8.startsWith(String s):判断是否以指定字符串开头:");
String s4="abx123";
System.out.println(s4.startsWith("ab"));
System.out.println("===");
//9.contains(String s):判断是否包含指定字符
System.out.println("9.contains(String s):判断是否包含指定字符:");
String s5="abcf";
System.out.println(s5.contains("f"));
System.out.println("===");
//10.toLowerCase():变为小写
System.out.println("10.toLowerCase():变为小写");
String s6="123abFFF";
System.out.println(s6.toLowerCase(Locale.ROOT));
System.out.println("===");
//11.toUpperCase():变为大写
System.out.println("11.toUpperCase():变为大写:");
String s7="abc666";
System.out.println(s7.toUpperCase(Locale.ROOT));
System.out.println("===");
//12.valueOf(int i):将int 转换为 字符串
System.out.println("12.valueOf(int i):将int 转换为 字符串:");
int a=457;
String s8=String.valueOf(a);
System.out.println(s8.getClass().getName());
System.out.println("===");
//13.char[] toCharArray():将字符串转为一个字符数组
System.out.println("13.char[] toCharArray():将字符串转为一个字符数组:");
String s9="abcdef";
char[] chars=s9.toCharArray();
for(char c:chars){
System.out.print(c+" ");
}
System.out.println();
System.out.println("===");
//14.replace(String old_str,String new_str):替换 这里返回的是一个新的字符串
System.out.println("14.replace(String old_str,String new_str):替换:");
String s10="ababab123";
String s10_rs=s10.replace("12","aa");
System.out.println(s10_rs);
System.out.println("===");
//15.String[] split(String str_分隔符):字符串分割
System.out.println("15.String[] split(String str_分隔符):字符串分割:");
String s11="成都 武汉 南京 深圳";
String[] s11_rs=s11.split(" ");
System.out.println(Arrays.toString(s11_rs));
System.out.println("===");
//16.subString(int beginIndex):划分字符串
//subString(int beginIndex,int endIndex)
System.out.println("16.subString(int beginIndex):划分字符串:");
String s12="abcde";
System.out.println(s12.substring(1));
System.out.println(s12.substring(1,2));//包前不包尾
System.out.println("===");
//17.String trim():去除首尾空格 这里返回的是一个新的字符串
System.out.println("17.String trim():去除首尾空格 这里返回的是一个新的字符串:");
String s13=" 你好中国 ";
String s13_rs=s13.trim();
System.out.println(s13_rs);
System.out.println("===");
}
}
定义:
Hello World
===
1.indexOf(字符/字符串):第一次出现的索引:
0
3
===
2.lastIndexOf(字符/字符串):最后一次出现的索引:
3
3
===
3.charAt(int index):返回对应字符:
a
===
4.endsWith(String str):判断是否以指定字符串结尾:
true
===
5.length():返回字符串长度:
6
===
6.equals(String s):判断字符串内容是否相等:
true
===
7.isEmpty():判断字符串是否为空:
true
===
8.startsWith(String s):判断是否以指定字符串开头:
true
===
9.contains(String s):判断是否包含指定字符:
true
===
10.toLowerCase():变为小写
123abfff
===
11.toUpperCase():变为大写:
ABC666
===
12.valueOf(int i):将int 转换为 字符串:
java.lang.String
===
13.char[] toCharArray():将字符串转为一个字符数组:
a b c d e f
===
14.replace(String old_str,String new_str):替换:
abababaa3
===
15.String[] split(String str_分隔符):字符串分割:
[成都, 武汉, 南京, 深圳]
===
16.subString(int beginIndex):划分字符串:
bcde
b
===
17.String trim():去除首尾空格 这里返回的是一个新的字符串:
你好中国
===Process finished with exit code 0