43 java常用类_5 _String类常用方法

length()方法

charAt(int index) 方法

contains(String str) 方法

  1. public int length():返回字符串的长度。(空格也算一个长度)
  2. public char charAt(int index) :根据下标获取字符。(要注意下标的范围0 ~ str.length()-1)
  3. public boolean contains(String str) :判断当前字符串中是否包含str.

代码如下:

package com.wlw.common_class.string;

public class Demo02 {

public static void main(String[] args) {
//字符串方法的使用
// length():返回字符串的长度。(空格也算一个长度)
// charAt(int index) :根据下标获取字符。返回某个位置的字符(要注意下标的范围0 ~ str.length()-1)
// contains(String str) :判断当前字符串中是否包含str.

String content = "java是世界上最好的编程语言";
System.out.println(content.length()); //15

System.out.println(content.charAt(0)); //j
System.out.println(content.charAt(content.length()-1)); //言

System.out.println(content.contains("java")); //true
System.out.println(content.contains("php")); // false

}
}

toCharArray() 方法

indexOf(String str) 方法

lastIndex0f(String str)方法

  1. public char[] toCharArray() :将字符串转换成数组。
  2. public int indexOf(String str) :查找str首次出现的下标,存在,则返回该下标;不存在,则返回-1
  3. public int lastIndex0f(String str) :查找字符串在当前字符串中最后一次出现的下标索引。

代码如下:

package com.wlw.common_class.string;

import java.util.Arrays;

public class Demo02 {

public static void main(String[] args) {

String content = "java是世界上最好的java编程语言,java真好";

System.out.println("===============字符串方法的使用2===============");
//4. public char[] toCharArray() :将字符串转换成数组。
//5. public int indexOf(String str) :查找str首次出现的下标,存在,则返回该下标;不存在,则返回-1
//6. public int lastIndex0f(String str) :查找字符串在当前字符串中最后一次出现的下标索引。

System.out.println(Arrays.toString(content.toCharArray()));

System.out.println(content.indexOf("java")); //0
System.out.println(content.indexOf("java",4)); //11
System.out.println(content.lastIndexOf("java")); //20

}
}

/*
执行结果:
===============字符串方法的使用2===============
[j, a, v, a, 是, 世, 界, 上, 最, 好, 的, j, a, v, a, 编, 程, 语, 言, ,, j, a, v, a, 真, 好]
0
11
20
*/

trim()方法

toUpperCase() 方法

endWith(String str)方法

  1. public String trim() :去掉字符串前后的空格。
  2. public String toUpperCase() :将小写转成大写。(全部)
  3. public boolean endWith(String str) :判断字符串是否以str结尾。

代码如下:

package com.wlw.common_class.string;

import java.util.Arrays;

public class Demo02 {

public static void main(String[] args) {

System.out.println("===============字符串方法的使用3===============");
//7. public String trim() :去掉字符串前后的空格。
//8. public String toUpperCase() :将小写转成大写。
// toLowerCase(); 将大写转成小写
//9. public boolean endWith(String str) :判断字符串是否以str结尾。
// starWith(String str):判断字符串是否以str开始。

String str = " hello world! ";
System.out.println(str.trim()); //hello world!

System.out.println(str.toUpperCase());// HELLO WORLD!
System.out.println(str.toLowerCase());// hello world!

String filename = "hello.java";
System.out.println(filename.endsWith(".java")); //true
System.out.println(filename.startsWith("hello")); //true
System.out.println(filename.startsWith("ello",1));//true,从那个下标开始检测
}
}

/*
执行结果:
===============字符串方法的使用3===============
hello world!
HELLO WORLD!
hello world!
true
true
true
*/

replace (char oldChar, char newChar)方法

split(String str)方法

  1. public String replace (char oldChar, char newChar):将旧字符串替换成新字符串
  2. public String[] split(String str) :根据str做拆分。

代码如下:

package com.wlw.common_class.string;

import java.util.Arrays;

public class Demo02 {

public static void main(String[] args) {



System.out.println("===============字符串方法的使用4===============");
//10. public String replace (char oldChar, char newChar):将旧字符串替换成新字符串
//11. public String[] split(String str) :根据str做拆分。

String content3 = "java是世界上最好的java编程语言,java真好";
System.out.println(content3.replace("java","php"));

String say = "java is the best programing languange,java xiang";
String[] arr1 = say.split(" "); //仅用空格" "
for (String s1 : arr1) {
System.out.println(s1);
}
System.out.println("=====");
String[] arr2 = say.split("[ ,]");//用 空格 和 逗号
for (String s2 : arr2) {
System.out.println(s2);

}
System.out.println("=====");
String[] arr3 = say.split("[ ,]+");//+表示前面的空格和逗号可以出现连续多个
for (String s3 : arr3) {
System.out.println(s3);
}

}
}
/*
执行结果:
php是世界上最好的php编程语言,php真好
java
is
the
best

programing
languange,java
xiang
=====
java
is
the
best

programing
languange
java
xiang
=====
java
is
the
best
programing
languange
java
xiang

*/

equals() 、compareTo()的对比

  1. equals() 是比较是否相等
  2. compareTo()是比较大小,(在字典表中的顺序,谁在前,谁在后)

代码如下:

package com.wlw.common_class.string;

import java.util.Arrays;

public class Demo02 {

public static void main(String[] args) {

System.out.println("===============字符串方法的使用补充===============");
//equals() 、compareTo()的对比
//1. equals() 是比较是否相等
//2. compareTo()是比较大小,(在字典表中的顺序,谁在前,谁在后)
String s1 = "hello";
String s2 = "HELLO";
System.out.println(s1.equals(s2)); //false
System.out.println(s1.equalsIgnoreCase(s2));// true ,忽略大小写的equals比较

String s3 = "abc"; //a 的位置是97
String s4 = "xyz"; //x 的位置是120
System.out.println(s3.compareTo(s4)); //-23

String s5 = "abc";
String s6 = "ayzvr"; //第一个都是a,就比较第二个
System.out.println(s5.compareTo(s6));//-23

String s7 = "abc";
String s8 = "abcxyz";//因为前三个都一样,就比较字符串长度
System.out.println(s7.compareTo(s8));//-3

String s9 = "abc";
String s10 = "abc";
System.out.println(s9.compareTo(s10));//0
}
}

/*
执行结果:
===============字符串方法的使用补充===============
false
true
-23
-23
-3
0
*/