String()
//初始化新创建的 String对象,它代表了一个空字符序列。
String st = new String();
System.out.println(st); //
String(byte[] bytes)
通过使用平台的默认字符集解码指定的字节数组构造了一个新的 String。
byte[] arr = new byte[]{96,97,98,99}; //ASMI值
String st = new String(arr); //`abc
System.out.println(st);
//String(byte[] byte, int start, int end);
String st2 = new String(arr,1,2);
System.out.println(st2);//ab
//String(char[] arr)
//String(char[] arr, int start, int end)
char[] arr = {'a','b','c','d'};
String st1 = new String(arr);
System.out.println(st1);//abcd
String st2 = new String(arr,0,2);
System.out.println(st2);//ab
//判断功能
//boolean equals(Object obj)
//boolean equalsIgnoreCase(String atr) //忽略大小写
String st1 = "aBcD";
String st2 = "abcd";
System.out.println(st1.equals(st2));//false
System.out.println(st1.equalsIgnoreCase(st2));//ture
//boolean startWith(String atr) //是否以str开始
System.out.println(st1.startsWith("ab"));//false
//boolean endWith(String atr) //是否以str结束
System.out.println(st1.endsWith("D"));//true
//String获取功能
String st = new String("abcdefg");
//int length(); 获取字符串长度
System.out.println(st.length()); // 7
//cahr charAt(int index); 获取指定位置的字符
System.out.println(st.charAt(1)); // b
//int indexOf(String str); 查询给定字符串出现的位置
System.out.println(st.indexOf("bc")); //1
//String substring(int start); 截取给定位置到最后的字符串
System.out.println(st.substring(2));//cdefg
//String substring(int start, int end) 截取指定位置的字符串(包左不包右)
System.out.println(st.substring(1,2));//b
//String类的转换功能
String st = "abc";
String st1 = "ABC";
//char[] toCharArray(); //将字符串转化为字符串数组
char[] arr = st.toCharArray();
System.out.println(Arrays.toString(arr)); //[a, b, c]
//String toLowerCase(); //将字符串全部转化为小写
st1 = st1.toLowerCase();
System.out.println(st1); //abc
//String toUpperCase(); //将字符串全部转化为大写
st = st.toUpperCase();
System.out.println(st); //ABC
//String getBytes(); //转化为字节数组
byte[] arr1= st.getBytes();
System.out.println(Arrays.toString(arr1)); //[65, 66, 67]
//其他功能
//String tirm() 去掉字符串两端的空格
String st = " abc def g ";
st = st.trim();
System.out.println(st); //abc def g
//String[] split(String str)
String st1 = "a:b:c";
String[] arr = st1.split(":");
System.out.println(Arrays.toString(arr)); //[a, b, c]
Java-String的api
于 2022-03-08 18:25:45 首次发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)