字符串String

字符串String

获取字符串长度

length()方法

String s="we are students";
int size=s.length();//包含空格
System.out.println(size);//15

字符串查找

indexOf() 返回的是搜索的字符或字符串首次出现的位置

lastIndexOf() 返回的是搜索的字符或字符串最后一次出现的位置

//格式 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  若为"",则返回字符串长度

获取指定索引位置的字符

charAt() 将指定索引处的字符返回

/*格式  str.charAt(int index)
str  任意字符串
index  整型值,用于指定要返回的字符的下标
*/

String str="hello word";
char mychar=str.charAt(6);
System.out.println(mychar);//w

获取子字符串

substring(int beginIndex)

从某处开始到结束

substring(int beginIndex,int endIndex)

从某处到某处

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位

去除空格

trim() 返回字符串的副本,忽略前导空格和尾部空格

String s=" hello world ";
System.out.println(s.length());//13
System.out.println(s.trim());//hello world
System.out.println(s.trim().length());//11

字符串替换

replace() 将指定的字符(串)替换成新的

/*格式:str.replace(CharSequence target,CharSequence replacement)
target  要替换的
replacement  要换成的内容
*/
String s="hello world";
String my=s.replace("h","H");
System.out.println(my);//Hello world

判断字符串的开始和结尾

startsWith() 判断字符串是否以指定的字符开头

endsWith() 判断字符串是否以指定的字符结尾

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

判断字符串是否相等

equals()

区分大小写

equalsIgnoreCase()

忽略大小写

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

按字典顺序比较两个字符串

compareTo()

若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

字母大小写转换

toLowerCase() 将所有大写字母转为小写

toUpperCase() 将所有小写字母转为大写

String s="Hello World";
System.out.println(s.toUpperCase());//HELLO WORLD

System.out.println(s.toLowerCase());//hello world

字符串分割

split()

/*格式
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();

字符串String

上一篇:你必须知道的Docker数据卷(Volume)


下一篇:pytest 用例初始化和用例数据清除