Java字符串以及常用的方法

字符串的声明、创建

String s; //声明
s=new String("we are students");//创建

String s=new String("we are students");//声明创建一步完成

字符串的常用方法

(1)public int length()

使用String类中的length()方法可以获取一个字符串的长度,如:

String s="good";
int n1;
n1=s.length();

此时n1的值为4。

(2)public boolean equals(String s)

使用String类中的equals方法,比较当前字符串对象的实体是否与参数指定的字符串s的实体相同,如:

String tom=new String("good");
String Jerry=new String("good");
String boy=new String("goods");

tom.equals(boy);//输出结果为false
tom.equals(Jerry);//输出结果为true

(3)public boolean startsWith(String s)和public boolean endsWith(String s)

字符串对象调用startsWith(String s)方法,判断当前字符串对象的前缀是否是参数指定的字符串s,如:

String tom="1321534564",jerry="41654354654135";

tom.startsWith("132");//返回的值为true
jerry.startsWith("132");//返回的值为false

字符串对象调用startsWith(String s)方法,判断当前字符串对象的后缀是否是参数指定的字符串s,如:

String tom="1321534564",jerry="41654354654135";

tom.endsWith("564");//返回的值为true
jerry.endsWith("564");//返回的值为false

(4)public boolean regionMatches(int firstStart,String other,int otherStart,int length)

字符串调用regionMatches(int firstStart,String other,int otherStart,int length)方法,从当前字符串参数firstStart制定的位置开始,取长度为length的一个子串,并将这个字串和参数other指定的一个子串进行比较。其中,other指定的字符串是从参数otherStart指定的位置开始,从other中取长度为length的一个子串。如果两个子串相同该方法就返回true,否则返回false。

public class Exp14 {

	public static void main(String[] args) {
		int number=0;
		String s="student;entropy;engage,english,client";//定义字符串
		for(int k=0;k<s.length();k++){    //循环遍历字符串
			if(s.regionMatches(k, "en", 0, 2)){
				number++;
			}
		}
		System.out.println("number="+number);
	}
}
//返回值为number=5

(5)public int compareTo(String s)

字符串对象可以使用String类中的compareTo(String s)方法,按字典序与参数s制定的字符串比较大小。如果当前字符串与s相同,则返回0;如果当前字符串大于s,则返回正值;如果当前字符串小于s,则返回负值。

//将字符串数组按照字典序排列
public class Exp15 {
//java程序入口
	public static void main(String[] args) {
		String a[]={"door","apple","Applet","Birl","boy"};
		for(int i=0;i<a.length-1;i++){
			for(int j=i;j<a.length;j++){
				if(a[j].compareTo(a[i])<0){    //比较
					String temp=a[i];
					a[i]=a[j];
					a[j]=temp;
				}
			}
		}
		for(int i=0;i<a.length;i++){
			System.out.println(" "+a[i]);
		}
	}

}
/*返回为 
 Applet
 Birl
 apple
 boy
 door
*/

(6)public int indexOf(String s)

字符串调用indexOf(String s)方法从当前字符串的头开始检索字符串s,并返回首次出现的s的位置。如果没有检索到字符串s,该方法返回值为-1.

字符串调用indexOf(String s,int startpoint)方法从当前字符串的startpoint处开始检索字符串s,并返回首次出现的s的位置。如果没有检索到字符串s,该方法返回值为-1.

字符串调用lastIndexOf(String s)方法从当前字符串的头开始检索字符串s,并返回最后出现的s的位置。如果没有检索到字符串s,该方法返回值为-1.

public class Exp16 {

	public static void main(String[] args) {
		String tom="I am a good cat";
		int n=tom.indexOf("a");System.out.println(n);
		n=tom.indexOf("a", 7);System.out.println(n);
		n=tom.indexOf("w", 2);System.out.println(n);
		n=tom.lastIndexOf("a");System.out.println(n);
	}

}

(7)public String substring(int startpoint)

字符串调用substring(int startpoint)方法获得一个当前字符串的子串,该子串是从当前字符串的startpoint处截取到字符串的末尾处所得到的字符串。

字符串调用substring(int start,int end)方法获得一个当前字符串的子串,该子串是从当前字符串的start处截取到end处所得到的字符串,但不包括end处所对应的的字符。


public class Exp17 {

	public static void main(String[] args) {
		String tom="I am a good cat";
		String s=tom.substring(5);System.out.println(s);
		String m=tom.substring(2, 5);System.out.println(m);
	}

}
/*
a good cat
am 
*/

(8)public String replaceAll(String oldString,String newString)

字符串调用replaceAll(String oldString,String newString)方法可以获得一个串对象,这个对象是通过参数newString指定的字符串替换s中的由oldString指定的所有字符串而得到的字符串。

字符串调用replaceFirst(String oldString,String newString)方法可以获得一个串对象,这个对象是通过参数newString指定的字符串替换s中出现的第一个oldString指定的字符串而得到的字符串。

public class Exp18 {

	public static void main(String[] args) {
		String tom="I am a good cat";
		String s=tom.replaceAll("cat", "dog");
		String m=tom.replaceFirst("a", "b");
		System.out.println(s);
		System.out.println(m);
	}

}
/*
I am a good dog
I bm a good cat
*/

(9)public String trim()

字符串调用trim()方法可以获得一个字符串对象,这个对象是s去掉前后空格之后的字符串。

public class Exp16 {

	public static void main(String[] args) {
		String tom="    I am a good cat    ";
		String s=tom.trim();
		System.out.println(s);
	}

}
/*
I am a good cat
*/

猜你喜欢

转载自blog.csdn.net/qq_20799821/article/details/103178206