31字符串— string:基本方法 4.比较两个字符串是否相等的方法: public boolean equals( object anobject ■5比较两个字符串在不区分大小写时是否相等的方法: public boolean equals Ignore Case( Object anobject ■6.比较两个字符串大小的方法 public int compareTo( Object anobject 如: s1 compareTo(S2) 如果s1等于s2,方法返回0;如果按字典顺序s1小于s2,方法 返回一个小于0的值;如果按字典顺序s1大于s2,方法返回 个大于0的值。 ■7检査在指定范围内是否有指定前缀内容的方法: public boolean startsWith(String pre int toffset)
◼ 4.比较两个字符串是否相等的方法: public boolean equals ( Object anObject) ◼ 5.比较两个字符串在不区分大小写时是否相等的方法: public boolean equals IgnoreCase( Object anObject) ◼ 6.比较两个字符串大小的方法: public int compareTo ( Object anObject) 如:s1.compareTo ( s2) 如果s1等于s2,方法返回0;如果按字典顺序s1小于s2,方法 返回一个小于0的值;如果按字典顺序s1大于s2,方法返回一 个大于0的值。 ◼ 7.检查在指定范围内是否有指定前缀内容的方法: public boolean startsWith (String pre , int toffset) 3.1字符串——String:基本方法
31字符串— string:基本方法 ■8连接两个字符串的方法 public String concat( String str) 如:“Helo,“, concat(java). concat(Word!y) 返回” Hello, java World! ■9返回指定字符和指定字符串在字符串中第一次出现的位置值的方法 public int indexof(char ch) public int indexof( String str) ■10.在字符串中使用指定字符进行替换的方法: public String replace( char oldchar, char new Char 11.取子串的方法: public String substring(int start, int end) 12把字符串中字符转换为大小写的方法: public string toUpperCase public String toLowerCase
◼ 8.连接两个字符串的方法: public String concat ( String str) 如:“Hello, “ . concat (“ java”) . concat (“ World!”) 返回” Hello, java World!” 。 ◼ 9.返回指定字符和指定字符串在字符串中第一次出现的位置值的方法: public int indexOf (char ch) public int indexOf ( String str ) ◼ 10.在字符串中使用指定字符进行替换的方法: public String replace ( char oldchar , char newChar ) ◼ 11.取子串的方法: public String substring (int start , int end) ◼ 12.把字符串中字符转换为大小写的方法: public String toUpperCase ( ) public String toLowerCase ( ) 3.1字符串——String:基本方法
31字符串— String:示例 public class String Demo2 i public static void main(String args[l) t String strobl="First String String strob2=Second String", String strob3= strObl System. out. println("Length of strObl: +strObl. length) System. out. println("Char at index 3 in strobl: +strObl. charAt(3)) if(strobl. equals(strob2)) System. out println (strObl== strOb2) else System. out. println(strobl!= strOb2 ) if(strObl.equals(strOb3)) System. out. printIn(strobl==strOb3") else System. out. printIn(strobl!=strob3");
public class StringDemo2 { public static void main(String args[]) { String strOb1 = "First String"; String strOb2 = "Second String"; String strOb3 = strOb1; System.out.println("Length of strOb1: " +strOb1.length()); System.out.println("Char at index 3 in strOb1: " +strOb1.charAt(3)); if(strOb1.equals(strOb2)) System.out.println("strOb1 = = strOb2"); else System.out.println("strOb1 != strOb2"); if(strOb1.equals (strOb3)) System.out.println("strOb1 == strOb3"); else System.out.println("strOb1 != strOb3"); } } 3.1字符串——String:示例
31字符串— string:示例 运行结果为: Length of strobl: 12 Char at index 3 in strobl: S strobl!= strOb2strOb1== strOb3
运行结果为: Length of strOb1: 12Char at index 3 in strOb1: s strOb1 != strOb2strOb1 == strOb3 3.1字符串——String:示例
31字符串— String Buffer:创建 字符串一旦创建,它的值就固定了,但在实际应用中,经常需要对字 符串的内容做动态修改,Java类的替代品 String Buffer类(字符串缓 冲器类型)可以实现该操作。 String Buffer类比 String类更灵活,可以 在字符串缓冲区中添加、插入、追加新内容。 1.构造一个字符缓冲区 public String Buffer( 建立一个空串的缓冲区,长度为16。 String Buffer int length 建立一个长度为 length的空串缓冲区。 String Buffer( String str) 初始化缓冲区内容为给定的字符串str,并提供另16个字符的空间供 再次分配。类似于 Vector
字符串一旦创建,它的值就固定了,但在实际应用中,经常需要对字 符串的内容做动态修改,Java类的替代品StringBuffer类(字符串缓 冲器类型)可以实现该操作。StringBuffer类比String类更灵活,可以 在字符串缓冲区中添加、插入、追加新内容。 1.构造一个字符缓冲区 public StringBuffer( ) 建立一个空串的缓冲区,长度为16。 StringBuffer ( int length) 建立一个长度为length的空串缓冲区。 StringBuffer ( String str) 初始化缓冲区内容为给定的字符串str,并提供另16个字符的空间供 再次分配。 类似于Vector 3.1字符串——StringBuffer:创建