length()方法
String s="we are students";
int size=s.length();//包含空格
System.out.println(size);//15
//格式 str.indexOf(substr)
//str 任意字符串对象
//substr 要搜索的字符串
String str="We are students";
int size=str.indexOf("e");
//size范围:0~length-1
System.out.println(size);//1
int size1=str.lastIndexOf("e");
System.out.println(size1);//11
int size2=str.lastIndexOf("");
System.out.println(size2);//15 若为"",则返回字符串长度
/*格式 str.charAt(int index)
str 任意字符串
index 整型值,用于指定要返回的字符的下标
*/
String str="hello word";
char mychar=str.charAt(6);
System.out.println(mychar);//w
从某处开始到结束
从某处到某处
String s="hello world";
String my=s.substring(5);
System.out.println(my);//world 输出5之后的
String my1=s.substring(5,8);
System.out.println(my1);//wo 输出67位
String s=" hello world ";
System.out.println(s.length());//13
System.out.println(s.trim());//hello world
System.out.println(s.trim().length());//11
/*格式:str.replace(CharSequence target,CharSequence replacement)
target 要替换的
replacement 要换成的内容
*/
String s="hello world";
String my=s.replace("h","H");
System.out.println(my);//Hello world
String s1="22586981";
String s2="28515668";
boolean a=s1.startsWith("22");
boolean b=s2.endsWith("55");
System.out.println("字符串s1是以22开头吗?"+a);//true
System.out.println("字符串s2是以55结尾吗?"+b);//false
区分大小写
忽略大小写
String s1="abc";
String s2="ABC";
boolean a=s1.equals(s2);
boolean b=s1.equalsIgnoreCase(s2);
System.out.println(a);//false
System.out.println(b);//true
若1对象在2对象之前,则结果为负数;若1对象在2对象之后,则结果为正数;若相等,则为0
String s1="a";
String s2="b";
String s3="c";
System.out.println(s1.compareTo(s2));//-1
System.out.println(s2.compareTo(s3));//-1
System.out.println(s3.compareTo(s1));//2
String s="Hello World";
System.out.println(s.toUpperCase());//HELLO WORLD
System.out.println(s.toLowerCase());//hello world
/*格式
spilt(String sign) sign为分割字符的字符串
spilt(String sign,int limit) limit: 限制的分割次数
没有统一的对字符进行分割的符号,若要定义多个分割符,可用‘|‘。
例如:",|="表示分割符分别为","和"="
*/
String s="195.645.5.8";
String[] firstArray=s.split("\\.");//按照"."进行分割,用转义字符\String[] secondArray=s.split("\\.",2);// 2表示分割两次
System.out.println("s的原值是:["+s+"]");//s的原值是:[195.645.5.8]
System.out.println("全部分割的结果");
for (String a:firstArray){
System.out.print("["+a+"]"); //全部分割的结果
//[195][645][5][8]
}
System.out.println();
System.out.println("分割两次的结果");
for (String a:secondArray){
System.out.print("["+a+"]"); //分割两次的结果
//[195][645.5.8]
}
System.out.println();
原文:https://www.cnblogs.com/valder-/p/15129685.html