天天看点

黑马程序员 String封装类

------- android培训、java培训、期待与您交流! ----------

1:字符串(重点)

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

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

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

 (2)创建字符串对象

  A:String():无参构造

   **举例:

     String s = new String();

     s = "hello";

     sop(s);

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

   **举例

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

     String s = new String(bys);

     sop(s);

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

   **举例

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

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

     sop(s);

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

   **举例

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

     String s = new String(chs);

     sop(s);

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

   **举例

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

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

     sop(s); 

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

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

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

     String ss = new String(s);

     sop(ss);

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

   **举例

     String s = "hello";

     sop(s);

 (3)面试题

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

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

  B:请写出下面的结果

   String s1 = new String("abc");

   Strign s2 = new String("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(String suffix):测试此字符串是否以指定的后缀结束

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

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

  B:获取

  ***** int length():

          返回此字符串的长度

  ***** char charAt(int index):

          返回指定索引处的 char值

  ***** int indexOf(int ch):

         返回指定字符在此字符串中第一次出现处的索引。

          int indexOf(int ch, int fromIndex):

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

         int indexOf(String str):

         返回指定子字符串在此字符串中第一次出现处的索引。

         int indexOf(String str, int fromIndex):

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

    *** int lastIndexOf(int ch):

         返回指定字符在此字符串中最后一次出现处的索引。

         int lastIndexOf(int ch, int fromIndex)

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

         int lastIndexOf(String str)

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

         int lastIndexOf(String str, int fromIndex)

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

 ***** String substring(int beginIndex) 

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

         String substring(int beginIndex, int endIndex) 

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

  C:转换

  ***** byte[] getBytes():

          从字符串到字节数组的方法

  ***** char[] toCharArray():

          从字符串到字符数组的方法

  **** static String copyValueOf(char[] data)

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

   static String copyValueOf(char[] data, int offset, int count)

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

  ***** static String valueOf(数据类型):

          把该数据类型的数据转换成字符串。

  *** String toLowerCase():

          把字符串转换成小写

   String toUpperCase():

         把字符串转换成大写

  *** 字符串的连接

   String concat(String str):

     将指定字符串连接到此字符串的结尾。

  D:替换

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

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

  E:分割

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

  F: 

   String trim():去除字符串的前后空格

  G: 

   int compareTo(String anotherString)

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

   int compareToIgnoreCase(String str)

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

 (5)练习

  1:模拟登录,给三次机会,并提示还有几次.

  默认的用户名和密码为admin。 区分大小写。

  自己从键盘输入用户名和密码。

  2:给定一个字符串统计,统计大写字母,小写字母,数字出现的个数.

  ***注意:不包括特殊字符

  从键盘输入一个不包含特殊字符的字符串(只有26个字母和0-9组成)。

  3:给定一个字符串,把它变成首字母大写,其他字母小写的字符串.

  从键盘输入一个字符串,全部26个字母组成的。

  4:子串在整串中出现的次数。

  也就是说:获取一个字符串中,指定的字串在该字符串中出现的次数.

  例如:

  "nbasdnbafllgnbahjnbakqqqqlnba"  在这个字符串中,多有个nba.

  5:对字符串中字符进行自然顺序排序。

  "basckd"-->"abcdks"

  先留做思考内容:

  6:两个字符串的最大相同子串。

  两个字符串的最大相同子串。

  比如:

  "sadabcdfghjkl"

  werabcdtyu"

1:StringBuffer

 (1)字符串的缓冲区,是一个容器。

 (2)它和String的区别

  它是缓冲区可变长度的。

 (3)构造方法

  StringBuffer()

  StringBuffer(int num)

  StringBuffer(String str)

 (4)常用方法

  A:增加数据

   **append

   **insert

  B:删除数据

   **deleteCharAt

   **delete 还可以用于清空StringBuffer的缓冲区

  C:替换

   **replace

  D:获取

   **charAt

  E:长度和容量

   **length 元素的个数

   **capacity 元素的理论值

  F:获取元素的位置

   **indexOf

   **lastIndexOf

  G:截取

   **substring(int start)

   **substring(int start,int end)

  H:反转

   **reverse

 (5)字符串和StringBuffer的转换

  String--StringBuffer通过构造

  StringBuffer--String通过toString方法

2:StringBuilder

 和StringBuffer的功能是一样的,但是有区别:

 StringBuffer是线程安全的。

 StringBuilder不保证线程安全。

 一般来说,我们写的程序都是单线程的,所以,用StringBuilder。

JDK版本的升级原则:

A:提高效率

B:提高安全性

C:简化书写

3:基本数据类型的对象包装类

 (1)为了更方便的操作每个基本数据类型,java对其提供了很多的属性和方法供我们使用。

 (2)用途:

  A:方便操作

  B:用于和字符串进行相互转换

 (3)基本数据类型和对象类型的对应

    int Integer

    char Character

 (4)构造方法

  Integer(int num)

  Inreger(String s) 注意:s必须是纯数字的字符串。否则会有异常。

                          NumberFormatException

 (5)几个常用的方法

  Integer.toBinaryString);

  Integer.toOctalString();

  Integer.toHexString();

  static int Integer.parseInt(String s);

  static int Integer.parseInt(String s,int basic);

  int intValue();

  static Integer valueOf(int num)

  static Integer valueOf(String s)

 (6)类型转换

  int -- Integer

   int num = 20;

   A:Integer i = new Integer(num);

   B:Integer i = Integer.valueOf(num);

  Integer -- int

   Integer i = new Integer(20);

   A:int num = i.intValue();

  int -- String

   int num = 20;

   A:String s = String.valueOf(num);

   B:String s = ""+num;

   C:String s = Integer.toString(num);

  String -- int

   String s = "20";

   A:int num = Integer.parseInt(s);

   B:Integer i = new Integer(s);或者Integer i = Integer.valueOf(s);

     int num = i.intValue();