String类补充

来源:互联网 发布:怎么查看淘宝信誉积分 编辑:程序博客网 时间:2024/05/22 01:55

1:字符串

       (1)多个字符组成的一个序列,叫字符串。

          生活中很多数据的描述都采用的是字符串的。而且我们还会对其进行操作。

      所以,java就提供了这样的一个类供我们使用。

       (2)创建字符串对象

              A:String():无参构造

                       String s = new String();

              B:String(byte[]bys):传一个字节数组作为参数      

                       byte[] bys = {97,98,99,100,101};

              C:String(byte[]bys,int index,int length):把字节数组的一部分转换成一个字符串      

                       byte[] bys = {97,98,99,100,101};

                       String s = new String(bys,1,2);

              D:String(char[] chs):传一个恩字符数组作为参数     

                      char[] chs = {'a','b','c','d','e'};

                       String s = new String(chs);

              E:String(char[] chs,int index,intlength):把字符数组的一部分转换成一个字符串      

                       char[] chs = {'a','b','c','d','e'};

                       String s = new String(chs,1,2);

              F:String(String str):把一个字符串传递过来作为参数

                       char[] chs = {'a','b','c','d','e'};

                       String s = new String(chs,1,2);

                       String ss = new String(s);

              G:直接把字符串常量赋值给字符串引用对象   

                       String s = "hello";

       (3)面试题

              A:请问String s = new String("hello");创建了几个对象。

               两个。一个"hello"字符串对象,一个s对象。

              B:请写出下面的结果

                     String s1 = newString("abc");

                     Strign s2 = newString("abc");

                     String s3 ="abc";

                     String s4 ="abc";

                     sop(s1==s2);  //false

                     sop(s1==s3);  //false

                     sop(s3==s4);  //true

              C:字符串对象一旦被创建就不能被改变。

                     指的是字符串常量值不改变。

       (4)字符串中各种功能的方法

              A:判断

                boolean equals(Object anObject):判断两个字符串的内容是否相同

                boolean equalsIgnoreCase(String anotherString):判断两个字符串的内容是否相同,不区分大小写

                boolean contains(String s):判断一个字符串中是否包含另一个字符串

                     boolean endsWith(Stringsuffix):测试此字符串是否以指定的后缀结束

                     boolean startsWith(Stringsuffix):测试此字符串是否以指定的前缀开始

                     boolean isEmpty():测试字符串是否为空

              B:获取

                int length():返回此字符串的长度

                char charAt(int index):返回指定索引处的 char值

                int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引。

                     int indexOf(int ch, intfromIndex):返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索。

                     int indexOf(String str):返回指定子字符串在此字符串中第一次出现处的索引。

                     int indexOf(String str, intfromIndex):返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始。

                       int lastIndexOf(int ch):返回指定字符在此字符串中最后一次出现处的索引。

                     int lastIndexOf(int ch, intfromIndex)

                            返回指定字符在此字符串中最后一次出现处的索引,从指定的索引处开始进行反向搜索。

                     int lastIndexOf(String str)

                            返回指定子字符串在此字符串中最右边出现处的索引。

                     int lastIndexOf(String str,int fromIndex)

                            返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索。

                      Stringsubstring(int beginIndex)

                            返回一个新的字符串,它是此字符串的一个子字符串。

                     String substring(intbeginIndex, int endIndex)

                            返回一个新字符串,它是此字符串的一个子字符串。

              C:转换

                      byte[]getBytes():从字符串到字节数组的方法

                      char[]toCharArray():从字符串到字符数组的方法

                      static StringcopyValueOf(char[] data)

                            返回指定数组中表示该字符序列的 String。

                     static StringcopyValueOf(char[] data, int offset, int count)

                            返回指定数组中表示该字符序列的 String。

                      staticString valueOf(数据类型):把该数据类型的数据转换成字符串。

                       String toLowerCase():把字符串转换成小写

                     String toUpperCase():把字符串转换成大写

                       字符串的连接

                     String concat(String str):将指定字符串连接到此字符串的结尾。

              D:替换

                     String replace(charoldChar, char newChar):用新字符替换旧字符

                     String replace(String target,String replacement):用新的子串换旧串

              E:分割

                     String[] split(Stringregex):根据指定的字符串把一个字符串分割成一个字符串数组

              F:    Stringtrim():去除字符串的前后空格

              G:   intcompareTo(String anotherString)

                            按字典顺序比较两个字符串。

                     intcompareToIgnoreCase(String str)

                            按字典顺序比较两个字符串,不考虑大小写。

      

原创粉丝点击