public class TestString2 {
public static void main(String[] args) {
String s1="core Java";
String s2="Core Java";
System.out.println(s1.charAt(3));//提取下标为3的字符
System.out.println(s2.length());//字符串长度
System.out.println(s1.equals(s2));//比较两个字符串是否相等
System.out.println(s1.equalsIgnoreCase(s2));//比较两个字符串(忽略大小写)
System.out.println(s1.indexOf("Java"));//字符串1中是否包含Java
String s=s1.replace(' ','&');//将s1中的空格替换成&
System.out.println("result is :"+s);
String s3="";
String s4="How are you?";
System.out.println(s4.startsWith("How"));//是否以How开头
System.out.println(s4.endsWith("you"));//是否以you结尾
s3=s4.substring(4);//提取子字符串:从下标为4的开始到字符串结尾为止
System.out.println(s3);
s3=s4.substring(4,7);//提取子字符串:下标【4,7)不包括7
System.out.println(s3);
s3=s4.toLowerCase();//转小写
System.out.println(s3);
s3=s4.toUpperCase();//转大写
System.out.println(s3);
String s5=" How old are you!! ";
s3=s5.trim();//去除字符串首尾的空格。注意:中间的空格不能去除
System.out.println(s3);
System.out.println(s5);
}
}
【字符串(String)类常用方法】
最新推荐文章于 2021-01-13 10:07:10 发布