动画跟踪代码string sl "welcometoJava"String“Welcometo Java”的strings2=newstring("welcometoJava");限定字符串对象s3"welcome to Java"StringLiang.Introduction to JavaProgramming.EighthEdition,(c)2011Pearson Education,Inc.Allrightsreserved.0132130807
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 11 跟踪代码 String s1 = "Welcome to Java"; String s2 = new String("Welcome to Java"); String s3 = "Welcome to Java"; : String “Welcome to Java”的 限定字符串对象 s1 动 画
跟踪代码Sstringsl"welcome to Java":String“WelcometoJava”的string s2=new string("welcometo Java")限定字符串对象Strings3"welcometoJava":String“WelcometoJava”的字符串对象Liang.Introduction to JavaProgramming.EighthEdition,(c)2011Pearson Education,Inc.All12rightsreserved.0132130807
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 12 跟踪代码 String s1 = "Welcome to Java"; String s2 = new String("Welcome to Java"); String s3 = "Welcome to Java"; : String “Welcome to Java”的 限定字符串对象 : String “Welcome to Java”的 字符串对象 s1 s2
跟踪代码strings1"welcome to Java":String“WelcometoJava”的strings2=newstring("welcometoJava");限定字符串对象Strings3="welcome toJava"62LString“Welcome to Java”的字符串对象Liang.Introduction to JavaProgramming.EighthEdition,(c)2011Pearson Education,Inc.All13rightsreserved.0132130807
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 13 跟踪代码 String s1 = "Welcome to Java"; String s2 = new String("Welcome to Java"); String s3 = "Welcome to Java"; : String “Welcome to Java”的 限定字符串对象 : String “Welcome to Java”的 字符串对象 s1 s2 s3
字符串比较java.lang.String+equals(sl:String):boolean如果这个字符串等于字符串s1则返回true+equalsIgnoreCase(sl:String):如果不区分大小写这个字符串等于字符串s1则返回booleantrue返回一个大于0、等于0或小于0的整数以表明这个字+compareTo(sl: String): int符串是大于、等于还是小于s1+compareTolgnoreCase(sl: String):除了不区分大小写之外,其他都和CompareTo是一样int的+regionMatches(toffset:int,sl:String如果这个字符串指定的子域精确匹配字符串s1中指定offset:int,len:int):boolean的子域则返回true+regionMatches(ignoreCase:boolean,除了可以指定匹配是否是区分大小写的,其他都和前toffset: int, sl:String, offset: int,一个方法一样len:int):boolean如果这个字符串以指定前缀开始则返回true+startsWith(prefix:String):boolean如果这个字符串以指定前缀结束则返回true+endsWith(suffix:String):booleanLiang,Introduction toJava Programming.EighthEdition,(c)2011Pearson Education,Inc.All14rightsreserved.0132130807
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 14 字符串比较 java.lang.String +equals(s1: String): boolean +equalsIgnoreCase(s1: String): boolean +compareTo(s1: String): int +compareToIgnoreCase(s1: String): int +regionMatches(toffset: int, s1: String, offset: int, len: int): boolean +regionMatches(ignoreCase: boolean, toffset: int, s1: String, offset: int, len: int): boolean +startsWith(prefix: String): boolean +endsWith(suffix: String): boolean 如果这个字符串等于字符串 s1 则返回 true 如果不区分大小写这个字符串等于字符串 s1 则返回 true 返回一个大于 0、等于 0 或小于 0 的整数以表明这个字 符串是大于、等于还是小于 s1 除了不区分大小写之外,其他都和 CompareTo 是一样 的 如果这个字符串指定的子域精确匹配字符串 s1 中指定 的子域则返回 true 除了可以指定匹配是否是区分大小写的,其他都和前 一个方法一样 如果这个字符串以指定前缀开始则返回 true 如果这个字符串以指定前缀结束则返回 true
字符串比较equalsStrings1=String(wwelcome")newString s2 = "welcome";if(sl.equals(s2))(sl and s2 have the samecontentsif(sl == s2)sl and s2 have the same referenceLiang.Introduction to JavaProgramming.EighthEdition,(c)2011Pearson Education,Inc.All15rightsreserved.0132130807
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 15 字符串比较 equals String s1 = new String("Welcome“); String s2 = "welcome"; if (s1.equals(s2)){ // s1 and s2 have the same contents } if (s1 == s2) { // s1 and s2 have the same reference }