《2019年java基础知识字符串课件》由会员分享,可在线阅读,更多相关《2019年java基础知识字符串课件(27页珍藏版)》请在金锄头文库上搜索。
1、第章字符串,5.1 字符串 5.2 字符串的常用方法 5.3 字符串与基本数据的相互转化,5.1 字符串,Java使用java.lang包中的String类来创建一个字符串变量,因此字符串变量是对象。 1.字符串常量 如,“你好”,“1234.987”,“weqweo”。 2.声明字符串对象 使用String类声明一个字符串对象: String s1,s2;,3.创建字符串对象,使用String类的构造方法,例如: String s; s=new String(“we are students”); 用一个已有的字符串创建另一个字符串 String jack=String(s);,String
2、类的常用构造方法:,String (char a) char a=b,o,y; String s=new String(a); String (char a,int startIndex,int count) char a=s,t,u,d,e,n,t; String s=new String(a,2,4); s的值为:“uden”,第一个位置是0,4.引用字符串常量对象,一个字符串常量:“how are you” String s1,s2; s1= “how are you”; s2= “how are you”; 将字符串常量的引用赋值给s1和s2,因此s1和s2具有相同实体。,5.2 字符
3、串类的常用方法,(1)public int length() 使用string类中的length()方法可以获取一个字符串的长度 ,如: String s=“we are studengt”,tom= 我们是学生; Int n1,n2; N1=s. length(); N2=tom. length(); 那么n1的值是15,n2的值是5。 字符串常量也可以使用length()获得长度,如“你的爱好”. length()的值是4。,字符串对象调用String类中的equals方法,比较当前字符串对象的实体是否与参数指定的字符串s的实体相同. 如: String tom=new String( w
4、e are students); String boy=new String( We are students); String jerry= new String(we are students); tom.equals(boy)的值是false,tom.equals(jerry)的值是 true。 利用5.1说明equals的用法。 例5.1,(2) public boolean equals(String s),Class Example5_1 public static void main(string arg) String s1,s2; s1=new String( we are
5、students); s2=new String( we are students); System.out,printIn(s1.enquals(s2); System.out,printIn(s1=(s2); String s3,s4; S3=“how are you”; S3=“how are you”; System.out,printIn(s3.enquals(s4); System.out,printIn(s3=(s4); ,(3)public boolean startsWith(String s)和public boolean endsWith(String s),字符串对象调
6、用startsWith(String s)方法,判断当前字符串对象的前缀是否是参数指定的字符串s .如: String tom=“220302620629021”, jerry=“21079670924022”; tom.startsWith(“220”)的值是true, jerry.startsWith (“220”)的值是flase。 使用endstartsWith(String s)方法,判断一个字符串后缀是否是参数指定的字符串s .如: String tom=“220302620629021”, jerry=“21079670924022”;,tom. endsWith(“021”)的
7、值是true, jerry. endsWith (“021”)的值是flase。 字符串对象调用public boolean startsWith(String prefix,int toffset)方法,判断当前字符串从toffset索引处开始的前缀是否是参数指定的字符串prefix,如: String str=“123456”; boolean boo=str.startsWith(“456”,3); 那么boo的值为true。,(4)public boolean regionMatches(int first,String other,int ortherStart,int length
8、),字符串调用此方法,从当前字符串参数first指定的位置开始处,取长度为length的一个子串,并将这个子串和参数other指定的一个子串进行比较,其中,other指定的子串是从参数othertStart指定的位置开始,从other中取长度为length的一个子串。如果两个子串相同该方法就返回true,否则返回false。 使用该方法的重载方法: Public boolean regionMatches(boolean b,int fristStart,String other,int otherStart,int length) 可以通过参数b决定是否忽略大小写,当b取true是,忽略大小
9、写。,例5.2 Class Example5_2 public static void main(string arg) int number=0; String s=“studegnt;emtropy;engage,english,client”; for(int k=0,ks.length();k+) if (s.regionMatches (k,”en”,0,2) number+; System.out.printIn(“number=”+number); ,(5)public int compareTo(String s)方法,字符串对象可以使用String类中的compareTo(S
10、tring s)方法,按字典序与参数s指定的字符串比较大小。如果当前字符串与s相同,该方法返回值0;如果当前字符串对象大于s,该方法返回正值;如果小于s,该方法返回负值。 String str=“abcde” ; StrpareTo(“boy”)小于0; StrpareTo(“aba”)大于0; StrpareTo(“abcde”) 等于0; 按字典序比较两个字符串还可以使用public int compareToIgnoreCase(String s)方法,该方法忽略大小写。 例5.3,例子5.3 Class Example5_3 public static void main(string
11、 arg) string a=“door”, “apple”, “Applet”, “gril”, “boy”,; for (int i=0;ia.length-1;i+) for (int j=i+1;ja.length;j+) if(ajpareTo(ai)0) String temp=ai; ai= aj; aj=temp; , for (int i=0;ia.length;i+) symtem.out.print(“”+ai); ,(6)public int indexOf (String s),字符串调用方法indexOf (String s)从当前字符串的头开始检索字符串s,并返回
12、首次出现s的位置。如果没有检索到字符串s,该方法返回的值是-1。 字符串调用indexOf(String s ,int start)方法从当前字符串的start位置处开始检索字符串s,并返回首次出现s的位置。如果没有检索到字符串s,该方法返回的值是-1。 例:String tom=“I am a good cat”; tom.indexOf(“a”);的值 2 tom.indexOf(“good”,2);/的值 7 tom.lastIndexOf(“a”,7);/的值 13 tom.lastIndexOf(“w”,2);/的值 -1,(7)public String substring(int
13、 start),字符串对象调用该方法获得一个当前字符串的子串,该子串是从当前字符串的start处截取到字符串的末尾所得到的字符串。字符串对象调用substring(int start ,int end)方法获得一个当前字符串的子串,该子串是从当前字符串的start处截取到end处所得到的字符串,但不包括end处所对应的字符。 举例: String tom =“I love tom”; String s=tom.substring(2,5); S是lov。,(8)public String replaceAll(String oldString ,String newString) 字符串对象s
14、调用该方法可以获得一个串对象,这个串对象是通过用参数newString指定的字符串替换s中由oldString指定的所有字符串而得到的字符串。 另外,字符串对象s调用publish String replaceFirst replaceAll(String oldString ,String newString)方法可以获得一个串对象,这个串对象是通过用参数newString指定的字符串替换s中出现的第一个oldstring指定的字符串而得到的字符串。,(9)public String trim() 一个字符串s通过调用方法trim()得到一个字符串对象,该字符串对象是s去掉前后空格后的字符串
15、。 在下面的例子5.4中,使用了字符串的常用方法。 例子5.4 Class Example5_4 public static void main(string arg) String path=“c:myfile2000result.txt”; Int index=path.lastIndex(“”);,String flieName=path.substring(Index+1); String newName=lieName.replaceAll(“.tex”, “.jave”); Systrem.out.printIn(path); Systrem.out.printIn(flieNam
16、e); Systrem.out.printIn(newName); ,5.3 字符串与基本数据的相互转化,使用java.lang包中的Byte、Short、Integer 、 Long、Float、Double类调相应的类方法,可以将“数字”格式的字符串,转化为相应的基本数据类型。 public static byte parseByte(String s) throws NumberFormatException public static short parseShort(String s) throws NumberFormatException public static short parseInt(String s) throws NumberFormatException,public static long parseLong(String s) throws NumberForma