import java.util.Arrays;
public class List1<T> {
public static void main(String[] args) {
//字符串常营方法
String str="hello world";
String str1="hello world";
String str2="HELLO world";
String str3=" hello world887 ";
System.out.println(str.length());//输出字符串长度,空格也算 11
System.out.println(str.equals(str1));//判断两个字符串内容是否相等 true
System.out.println(str.equalsIgnoreCase(str2));//忽略两个字符串的大小写并判断是否相等 true
System.out.println(str3.trim());//清除字符串两边的空格 hello world887
System.out.println(str.substring(2));//从第二位开始截取字符串输出包括第二位到最后的字符串 llo world
System.out.println(str.substring(2,7));//截取第二位到第七位的字符串。包括第二位不包括第七位 llo w
System.out.println(str1.concat(str2));//将字符串str2续到str1的后边并输出续后的结果 hello worldHELLO world
System.out.println("www".concat(str2));//同上 wwwHELLO world
System.out.println(str2.contains("HE"));//判断字符串str2是否包含HE true
System.out.println(str1.indexOf("l"));//从头开始找返回字符l的位置 2
System.out.println(str1.lastIndexOf("l"));//从末尾开始找返回l的位置 9
System.out.println(str3.replaceAll("\\d",""));//替换正则表达式的值为空 ( hello world)
System.out.println(str3.replace("l","q"));//替换字符串str3中的l变为q ( heqqo worqd887)
String []st;
st=str.split("l");//将字符串str按照l分开为字符串数组
System.out.println(Arrays.toString(st));//返回分开的字符串数组 [he, , o wor, d]
System.out.println(st.length);//返回字符串数组的长度 4
System.out.println(str2.toLowerCase());//将字符串str2全部转换为小写字母 hello world
System.out.println(str.toUpperCase());//将字符串str全部转换为大写字母 HELLO WORLD
System.out.println("s".repeat(3));//将字符串s重复3次 sss
}
}
字符串的常用方法
于 2022-07-30 11:13:47 首次发布