黑马程序员——String类

来源:互联网 发布:java调用文件管理器 编辑:程序博客网 时间:2024/05/14 17:45

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

 

String类介绍

 

String类概述

字符串(string)是由字符数组构成的一个序列,java中字符串是一个对象

例如:  “abcd” 就是一个字符串对象

 

String类对象一旦创建,他的值是不可以改变的

例如:

String s =“Java”;

s = “Hello”;

先创建了一个字符串对象”Java”,将对象地址指向引用s, 又将”Hello”对象的地址指向了s

“Java”对象依然存在,并没有被改变

 

可以通过多种方式来创建字符串对象,最长用的是 String str = “abcd”;

String类还提供了多种方法用于处理字符串对象,后面介绍一些常用的方法

 

String构造方法

1.      String(char[] value)将字符数组中的所有字符拼接成一个字符串

例如: char[] ch = {‘H’, ‘e’, ‘l’, ‘l’, ‘o’};

           String s =new String(ch);

           System.out.print(s);结果显示hello

 

2.      String(char[] ch, int offset,int count)将字符数组中下标为offset开始的count个字符拼接成一个字符串

例如: char[] ch = {‘H’, ’e’, ’l’, ‘l’, ‘e’, ‘J’, ‘a’, ‘v’, ‘a’};

         String s =new String(ch, 5, 4);

         System.out.ptint(s);结果显示 Java

 

3.      String(byte[] b)将数值对应的字符拼接成字符串

例如: byte[] b = {1, 2, 3, 4};

           String s =new String(b);

           System.out.print(s);结果显示 1234(这里的结果是”1234”)

 

4.      String(byte[] b, int offset,int count)将数组中下标为offset开始的count个数值拼接成字符串,原理跟String(char[] ch, int offset, int count)一样,例子参照String(char[]ch, int offset, int count)

 

5.      String() String类的无参构造方法

例如: String s = newString();   //这里new出来的对象其实就是s =””;

           s = “abcd”;

 

6.      String(String s)接受一个字符串的构造方法

例如: String s = newString(“abcd”);         //这里其实是 s =“abcd”

 

7.      String(int[] arr, int offset,int count)int[]里面下标为offset开始count个数值拼接成字符串,例子参照上面第2个构造方法

 

String类的成员方法

比较的方法

1.      equals()

声明: public boolean equals(String s)

这个方法重写了Object类里面的equals方法,比较的是两个字符串里面的内容是否相等, 如果内容相等,返回true, 否则返回false

2.      equalsIgnoreCase()

声明: public boolean equalsIgnoreCase(String s)

equals方法类似,但是这个方法忽略了字符串里面字符的大小写, 如果内容相等,返回true, 否则返回false

3.      contains()

声明: public boolean contains(String s)

查看当前字符串是否包含s, 如果包含, 返回true,否则false

4.      startsWith() endsWith()

4.1   声明: public boolean startsWith(String s)

查看当前字符串是否以s开头,如果是返回true, 否则false

4.2   声明: pulibc boolean endsWith(String s)

查看当前字符串是否以s结尾,如果是返回true, 否则false

5.      isEmpty()

声明: public boolean isEmpty()

查看当前字符是否为空(其实就是判断当前字符串长度是否为0,参考下面的length()方法),如果为空返回true, 否则false

转换的方法

1.      getBytes()

声明: public byte[] getBytes()

将当前字符串里面的字符转换到byte[]

2.      toCharArray()

声明: public char[] toCharArray()

将当前字符串里的字符转换到char[]

3.      valueOf()

声明: public static String valueOf(参数)

这里的参数可以是基本数据类型, char[], Object

返回基本类型的字符串形式, 例如”1”, “c”, “true”

char[] 可以单独传入得到从0开始到结束拼接的字符串,也可以指定传入字符的其实位置, char[] ch, int offset, int count,结果与构造方法结果一样

Object返回的是ObjecttoString()

4.      copyValueOf()

声明: publis static String copyOf(参数)

根据查看底层代码, valueOf用法一样

5.      toLowerCase() toUpperCase()

5.1   声明: public String toLowerCase()

将当前字符串里的字符全部转换为小写

例如: “AbCd”.toLowerCase();打印结果为”abcd”

5.2   声明: public String toUpperCase()

将当前字符串里的字符全部转换为大写

例如: “AbCd”.toUpperCase();打印结果为”ABCD”

 

获取的方法

1.      length()

声明: public int length()

得到当前字符串的长度, 有多少个字符长度就是多少

例如: “abcd”.length();结果是4

2.      charAt()

声明: public char charAt(int index)

得到字符串中第index个字符(字符串开始的位置是0)

3.      indexOf()

3.1   声明: public int indexOf(int code)

得到code相对应的字符第一次出现的位置,如果没有返回-1

3.2   声明: public int indexOf(int code, int fromIndex)

得到code相对应的字符从字符串下标为fromIndex开始第一次出现的位置,如没有, 返回-1

3.3   声明: public int indexOf(String s)

得到s第一次出现的位置下标,如果没有返回-1

3.4   声明: public int indexOf(String s, int fromIndex)

得到s从当前字符串的第fromIndex开始第一次出现的位置,如没有返回-1

 

注意: fromIndex为负数,相当于从第0为开始找,如果fromIndex>= length(), 那么返回-1

4.      lastIndexOf()

indexOf()相反,它是从后往前找, 找最后一次出现的位置,或者找在fromIndex之前最后一次出现的位置

注意: fromIndex>= length(),相当于从leng() – 1开始找,fromIndex为负数时,返回-1

5.      substring()

5.1   声明: public String substring(int start)

返回当前字符串从第start开始到结尾的新字符串

注意: statrt只能是0~length(),如果大于length(), 会产生StringIndexOutOfBoundException异常

5.2   声明: public String substring(int start, int end)

返回当前字符串从start开始到end-1结尾的新字符串,如果start > end, start小于0或者end > length(),都会产生StringIndexOutOfBoundException异常

其它方法

1.      replace()

1.1   声明: public String replace(char oldChar, char newChar)

返回将当前字符串中oldChar全部替换为newChar的新字符串

1.2   声明: public String replace(String oldString, String newString)

返回将当前字符串中oldString全部替换为newString的新字符串

2.      split()

声明: public String[] split(String s)

返回以s切割点的String[]

3.      trim()

声明: public String trim()

返回一个去掉当前字符串前后空格的新字符串

4.      compareTo()

声明: public int compareTo(String s)

重写了Compare接口中的CompareTo方法

返回当前字符串和s中相应的字符的差值,如果相同返回0

如果返回正数, 代表在字典中顺序在后

如果返回负数, 代表在字典中顺序在前

 

 

原创粉丝点击