JAVA中String类常用方法

1. 判断长度:

int length()

返回字符串长度

String s1 = "helloworld";

System.out.println(s1.length());//返回长度 10


2. 判断索引:

char charAt(int index):

返回某索引出的字符

String s1 = "helloworld";

System.out.println(s1.charAt(1));//返回某索引处的字符 e


3. 判断是否为空字符串:

boolean isEmpty()

String s1 = "helloworld";

System.out.println(s1.isEmpty());//判断是否是空字符串 结果为false


4. 转换为大写

String toUpperCase();

5. 转换为小写

String toLowerCase();

转换为小写

String s1 = "helloworld";

String s2 = s1.toUpperCase();//转大写HELLOWORLD

String s3 = s2.toLowerCase();//转小写helloworld

System.out.println(s1);//s1仍然为原来字符串

System.out.println(s2);

System.out.println(s3);


6. 删除首尾处的空格,字符中间的空格不受影响

String trim():

删除首尾处的空格,字符中间的空格不受影响

String s33 = " he llo wo rld ";

String s4 = s33.trim();//删除首尾空格

System.out.println("------"+s33+"------");

//输出:------ he llo wo rld ------

System.out.println("------"+s4+"------");

//输出:------he llo wo rld------


7. 比较字符串内容是否相同:

boolean equals(Object obj):

比较字符串内容是否相同

String s1 = "HELLOWORLD";

String s2 = "helloworld";

System.out.println(s1.equals(s2));//比较两个字符串是否相等 false


8.忽略大小写情况下字符串内容是否相同:

boolean equalsIgnoreCase(String anotherString):

忽略大小写情况下字符串内容是否相同

String s1 = "HELLOWORLD";

String s2 = "helloworld";

System.out.println(s1.equalsIgnoreCase(s2));//忽略大小写判断是否相等 true


9. 将制定字符串连接到此字符串的结尾

String concat(String str):

将制定字符串连接到此字符串的结尾,等价于用“+”

String s3 = "abc";

String s4 = s3.concat("def");//连接两个字符串

System.out.println(s4);//abcdef


10. 比较大小

int compareTo(String anotherString) :

比较大小,返回值是两个字符串的ASCII码值相减的值

String s5 = "abc";

String s6 = "abe";

System.out.println(s6.compareTo(s5));//c:99 e:101 两个字符串的ASCII码值相减 2


11. 取其中一部分字符

String substring(int beginindex):

返回一个新的字符串,他是从beginindex开始取到字符串最后

String s7 = "北京市清华大学";

String s8 = s7.substring(3);//可以取一个子串,从num1,到最后

String s9 = s7.substring(3,5);//可以取一个子串,从[num1,num2)左闭右开

System.out.println(s7);//北京市清华大学清华大学

System.out.println(s8);//清华大学

System.out.println(s9);//清华


12、与判断相关的函数

@Test

public void test3(){

String str1 = "helloworld";

boolean b1 = str1.endsWith("ld");//判断是否已指定的字符串为结尾

System.out.println(b1);//true

boolean b2 = str1.startsWith("he");//判断是否已指定的字符串为开始

System.out.println(b2);//true

boolean b3 = str1.startsWith("wo",5);//判断是否已指定的字符串为指定的索引处开始的字符

System.out.println(b3);//true

String str2 = "wo";

System.out.println(str1.contains(str2));//判断是否包含某一字符串 true

System.out.println(str1.indexOf("lo"));

//判断某一字符串首次出现的位置 没有出现就返回-1 此处结果为3

System.out.println(str1.indexOf("lo",5));

//判断某一字符串在索引后出现的位置(从5找到最后) 没有出现就返回-1 此处结果为-1

System.out.println(str1.indexOf("lo",2));

//判断某一字符串在索引后出现的位置(从2找到最后) 没有出现就返回-1 此处结果为3

String str3 = "hellorworld";

System.out.println(str3.lastIndexOf("or"));//从右往左找 7

System.out.println(str3.lastIndexOf("or",5));//从索引值开始,从右往左找(从5找到0) 4

//什么情况下 indexOf(str) 和 lastIndexOf(str)返回值相同?

//1:字符串中仅有一个str时 2:字符串中没有str时 

}


13、与替换、匹配、切片(将字符串切成若干子字符串,返回字符数组)相关的函数

@Test

public void test4(){

//替换

String str1 = "市北京市清华大学";

String str2 = str1.replace('市','省');//替换某一字符

System.out.println(str1);//市北京市清华大学

System.out.println(str2);//省北京省清华大学

String str3 = str1.replace("北京市","中国");//替换某一字符串

System.out.println(str3);//市中国清华大学

//匹配

//正则匹配

String str4 = "12hello34world56java7891mysql1456";

//把字符串中的数字替换成,如果开头结尾有,则去掉

String string = str4.replaceAll("\\d+",",").replaceAll("^,|,$","");

// \\d+ 表示匹配多个数字 ^,表示开头的逗号, ,$ 表示结尾有逗号

System.out.println(string);

str4 = "12345";

boolean matches = str4.matches("\\d+");//判断是否都为数字

System.out.println(matches);

String tel = "0571-11223344";//判断是否是一个杭州的固定电话

boolean result = tel.matches("0571-\\d{7,8}");

System.out.println(matches);

//切片

String str = "hello|world|java";

String[] strs = str.split("\\|");//以某一字符来判断分割某一字符串,返回一个字符数组

for (int i = 0; i < strs.length; i++) {

System.out.println(strs[i]);//输出 hello world java

}

}