A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© zwb810497207 初级黑马   /  2016-11-14 21:05  /  1004 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

String类
               字符串是常量,他的值在创建之后(存放在内存中的常量池中)就不可以改变。
               解释:其实就是一旦这个字符串确定了,那么就会在内存区域的常量池中生成这个字符串,字符串本身不能不能改变,但 (str) 变量中记录的地址值可以改变。
                    String的对象是不可改变的,可以共享。当再次创建字符串时,首先先在常量池中查找是否有这个字符串存在,如果有,则把这个地址值传给这个变量,如果没有则创建。
               String里面可以放任意值。
               方法: String  s="abcdef";
                     ①.构造方法
                                   String();里面可以放入任意值
                        ②.length()     求字符串长度  返回int 的数字
                                   public int length()   使用:int i=s.length();//6
                        ③.substring(int beginIndex)  从 beginIndex开始到结束,组成一个新的数组,返回一个新的数组
                                   public String substring(int beginIndex)   使用:String s2=s.substring(2);//cdef
                                   substring(int beginIndex,int endIndex) 从begin(包含)开始到endIndex(不包含)
                        ④.startsWith(String str)  测试字符串是否以  str  开头 ,返回值为boolean类型
                                   startsWith(String str,int i)  测试字符串是否以  str  开头 并且是否以 i 索引开始   返回值为boolean类型
                        
                                   public boolean startsWith(String prefix)   使用:boolean b=s.startsWith("ab"); //   true
                                   endsWith(String str)   测试字符串是否以 str 结束,返回boolean类型
                                   endsWith(String str,int i)   测试字符串是否以 str 结束并且是否以 i 索引结尾,返回boolean类型
                                   public boolean endsWith(String suffix)    使用: boolean b=s.endsWith("def");  // true
                        ⑤.contains(String s)  当且仅当此字符串包含指定的 char 值序列时,返回 true。
                                   public boolean contains(String s )   使用: boolean b=s.contains("cd")  //true
                        ⑥.indexOf(String s)     返回指定子字符串在此字符串中第一次出现处的索引。返回的整数
                                   public int indexOf(String s); 使用: int i=s.indexOf("c");    //2
                        ⑦.getBytes()   使用平台的默认字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。
                                   public byte[] getBytes()      使用; byte[] b=s.getBytes[];   //得到的是一个byte[]数组。(用遍历数组打印)979899100101
                                                                             注意btye[]数组存的是数字,进来转化为数字
                        ⑧.toCharArry( )     将此字符串转换为一个新的字符数组。(特殊 打印的不是地址值而是字符)
                                   public char[] toCharArray()     使用: char[] c=s.toCharArray();//得到是一个char[]数组。(存的是字符型)
                        ⑨.equals(object otherObject)  将此字符串与指定对象比较(比较的是两个内容,不是地址值,因为String重写了String)返回时boolean类
                                   public boolean equals(Object anObject)   使用:boolean=s.equals(new String("abc"))  //false
                        10.eaualsIgnoreCase(String antherString)  将此字符串与指定对象比较不区分大小写(比较的是两个内容,不是地址值,因为String重写了String)返回时boolean类
                                   public boolean equalsIgnoreCase(String anotherString)   使用: boolean b=s.equalsIgnoreCase(new String("ABCDEF"));  //true
                        11.toString();       返回字符串本身(字符串)
                                   public String toString();       使用: String o=s.toString();     //abcdef
                        12.isEmpty();      当且仅当 length() 为 0 时返回 true。
                                   public boolean isEmpty();   使用: boolean b=s.isEmpty()    // false
                        13.charAt(int index)    返回指定索引处的 char 值。索引范围为从 0 到 length() - 1。
                                   public char charAt(int index);  使用: char c=s.charAt(2);  //c
                        14.toLowerCase()     使用默认语言环境的规则将此 String 中的所有字符都转换为小写
                                   public String toLowerCase();   使用:String p=s.toLowerCase();
                        15.toUpperCase()       使用默认语言环境的规则将此 String 中的所有字符都转换为大写
                                   public String toUpperCase();   使用: String p=s.toUpperCase();     //ABCDEF
                        16.trim()        返回字符串的副本,忽略前导空白和尾部空白(去掉前后空格)
                                   public String trim();
                     

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马