目录
2.public boolean equals(String s)
3.public boolean startsWith(String s) 和public boolean endsWith(String s)
4.public boolean regionMatches(int firstStart,String other,int otherStart,int length)
5.public int compareTo(String s)
6.public int indexOf(String s)
7.public String substring(int startpoint)
8.public String replaceAll(String oldString,String newString)
字符串常量:
"你好"、"1234.978"、"weqweo";申明字符串:
String s;;
1.创建字符串
-
使用String类的构造方法。如:
s=new String("Hello world"); -
使用已经创建好的字符串创建另一个字符串。如:
String s = new String (s);
-
使用字符数组创建字符串对象,如:
char a[] = {'s','t','r','i','n','g'}; String s = new String(a);//该语句与s=new String("string")等价 -
提取字符数组中的一部分创建一个字符串对象,如:
char a[] = {'s','t','r','i','n','g'}; String s = new String(a,2,4);//该语句与s=new String("ring")等价
引用字符串常量对象
字符串常量是对象,因此可以把字符串常量的引用赋值给另一个字符串变量。如:
String s1,s2; s1="hello"; s2="hello";//s1,s2具有了相同的引用
2.字符串的常用方法
1.public int length()
获取字符串的长度。如:
String s = "Hello world"; int n; n=s.length();//n=11
字符串常量也可以使用该方法,如:
int n; n="Hello world".length();//n=11
2.public boolean equals(String s)
字符串比较,比较当前字符串是否与s相同。如:
String s1="Hello world"; String s2="hello world"; String s3="Hello world"; boolean r1 = s3.equals(s1);//true boolean r2 = s3.equals(s2);//false
s1==s2的指为false。因为字符串是对象 ,s1、s2是引用,s1与s2所指向的并非是同一个字符串。
调用
public boolean equalsIgnoreCase(String s)也是比较两个字符串是否相同,但忽略大小写。
3.public boolean startsWith(String s) 和public boolean endsWith(String s)
-
字符串对象调用
startsWith(String s)方法,判断当前字符串对象的前缀(包括当前字符串本身)是否含有参数指定的字符串s。 -
字符串对象调用
startsWith(String s)方法,判断当前字符串对象的后缀(包括当前字符串本身)是否含有参数指定的字符串s。
String s = "Hello world";
boolean boo = s.startsWith("He");// boo = true
boolean boo1 = s.endsWith("wo");//boo1 = false
4.public boolean regionMatches(int firstStart,String other,int otherStart,int length)
从当前字符串参数 firstStant 指定的位置开始处,取长度为 length 的一个子串,并将这个子串和参数 other 指定的一个子串进行比较。其中,other 指定的子串是从参数 othertStart 指定的位置开始,从other中取长度为length 的一个子串。如果两个子串相同(未忽略大小写)该方法就返回true,否则返回 false。如:
String s = "Hello world"; String s1 = "Hello"; String s = "Hello world"; boolean boo = s.regionMatches(0,s1,0,5); System.out.println(boo); //true
第4行的意思为:从字符串
s下标为0的字符开始,取长度为5的字符串,即Hello;从字符串s1下标为0的字符开始,取长度为5的字符串,也是Hello比较取出的两个子串是否相同;
若第4行改为
boolean boo = s.regionMatches(0,s1,1,3);则返回false,因为从字符串s下标为0的字符开始,取长度为3的字符串,即Hel;从字符串s1下标为1的字符开始,取长度为3的字符串,是ell,取出的两个子串不相同;
-
该方法的重载方法是:
public boolean regionMatches(boolean b,int firstStart,String other,int otherStart,int length)
可以通过参数b决定是否忽略大小写,当b=true时忽略大小写。
实例
判断一个字符串 student;entropy;engage;english;client 中共出现几个en;
public class Main {
public static void main(String[] args) {
String s = "student;entropy;engage;english;client";
int count = 0;
for (int i = 0; i < s.length(); i++) {
if (s.regionMatches(i, "en", 0, 2))
count++;
}
System.out.println(count); //5
return;
}
}
5.public int compareTo(String s)
字符串对象可以使用 String类中的compareTo(Strings)方法,按字典序与参数s指定的字符串比较大小。如果当前字符串与s相同,该方法返回值0:如果当前字符串对象大于s,该方法返回正值;如果小于s,该方法返回负值。如:
String str = "abcde";
int a = str.compareTo("boy");//小于0
int b = str.compareTo("aba");//大于0
int c = str.compareTo("abcde");//等于0。
按字典序比较两个字符串还可以使用
publicintcompareTolgnoreCase(Strings)方法,该方法忽略大小写。
6.public int indexOf(String s)
字符串调用 indexOf(Strings)方法从当前字符串的头开始检索字符串s,并返回首次出现s的位置。如果没有检索到字符串s,该方法返回的值是-1。如:
-
indexOf(Strings,int startpoint)方法
字符串调用indexOf(Strings,int startpoint)方法从当前字符串的 startpoint位置处开始检索字符串 s,并返回首次出现s的位置。如果没有检索到字符串s,该方法返回的值是-1。字符串调用lastlndexOf(Strings)方法从当前字符串的头开始检索字符串,并返回最后出现s的位置。如果没有检索到字符串s,该方法返回的值是-1。
String s = "I am a good cat";
int n1=s.indexOf("a");//n1=2
n1=s.indexOf("good",2);//n1=7
n1=s.indexOf("a",7);//n1=13
n1=s.indexOf("w",2);//n1=-1
7.public String substring(int startpoint)
字符串对象调用该方法获得一个当前字符串的子串,该子串是从当前字符串的startpoint 处截取到字符串的末尾处所得到的字符串。
-
substring(int start ,intend)
字符串对象调用 substring(int start ,intend)方法获得一个当前字符串的子串,该子串是从当前字符串的 stant 处截取到 end 处所得到的字符串,但不包括 end 处所对应的字符。
例如:
String s = "I love tom"; String s1 = s.substring(2,5);//s1="lov"
8.public String replaceAll(String oldString,String newString)
字符串对象s调用该方法可以返回一个串对象,这个串对象是通过用参数 newString 指定的字符串替换s中由oldString指定的所有字符串而得到的字符串。如果s中不包含串oldString则返回原串s。如:
String s="Hello He world";
String s1 = s.replaceAll("He","he"); //s1 = "hello he world"
9.public String trim()
一个字符串s通过调用trim()方法返回一个字符串对象,该字符串对象是s去掉前后空格后的字符串。
String s=" Hello He world "; String s1 = s.trim(); System.out.println(s1);//s1="Hello He world"
本文详细介绍了Java中字符串的创建方式、常用方法如获取长度、比较、截取、替换以及去除空格等,并通过实例演示了如何使用这些方法。


1万+

被折叠的 条评论
为什么被折叠?



