3.1、字符与字符串
很多的编程语言都会强调利用字符数组来描述字符串,实际上在Java里面也存在有类似的概念,在String类中也提供有一系列与字符操作有关的方法。
No. |
方法名称 |
类型 |
描述 |
1 |
public String(char[] value) |
构造 |
将全部的字符数组作为String的内容 |
2 |
public String(char[] value, int offset, int count) |
构造 |
将部分的字符数组变为字符串,设置字符数组的开始索引与使用个数 |
3 |
public char charAt(int index) |
普通 |
返回指定索引位置上的字符 |
4 |
public char[] toCharArray() |
普通 |
将字符串以字符数组的形式返回 |
范例:验证charAt()方法
public class StringDemo { public static void main(String args[]) { String str = "hello" ; char c = str.charAt(0) ; System.out.println(c) ; } }
在程序中字符串索引都是从0开始的。
而后最为关键的是实现字符串与字符数组间的互相转换。
范例:将字符串变为字符数组
public class StringDemo { public static void main(String args[]) { String str = "helloworld" ; char [] data = str.toCharArray() ;// 将字符串变为字符数组 for (int x = 0 ; x < data.length ; x ++) { System.out.print(data[x] + "、") ; } } }
当字符串变为字符数组之后就可以针对于里面的每一个字符进行操作。
范例:将字符串中的小写字母变为大写字母
· 小写字母的编码 > 大写字母的编码,差了32,也就是说每一个字符都减去32即可。
public class StringDemo { public static void main(String args[]) { String str = "helloworld" ; char [] data = str.toCharArray() ; // 将字符串变为字符数组 for (int x = 0 ; x < data.length ; x ++) { data[x] -= 32 ; // 编码减去32,成大写 } System.out.println(new String(data)) ; System.out.println(new String(data,5,5)) ; // 第5个索引,之后取5个长度的内容 } }
下面可以利用此操作功能判断某一个字符串是否全部由数字所组成。
· 将字符串变为字符数组,这样做的目的可以进行每个字符的依次判断;
· 判断每一个字符的范围是否是数字:'0' ~ '9'之间。
范例:实现判断
public class StringDemo { public static void main(String args[]) { String str = "123" ; System.out.println(isNumber(str)) ; } public static boolean isNumber(String temp) { char data [] = temp.toCharArray() ; // 将字符串变为字符数组 for (int x = 0 ; x < data.length ; x ++) { if (data[x] < '0' || data[x] > '9') { // 不是数字 return false ; } } return true ; } }
3.2、字节与字符串
除了字符可以与字符串进行互相转换之外,字节也可以进行转换,但是这样的转换往往会出现在实际的开发环节中。
No. |
方法名称 |
类型 |
描述 |
1 |
public String(byte[] bytes) |
构造 |
将全部字节数组变为字符串 |
2 |
public String(byte[] bytes, int offset, int length) |
构造 |
将部分字节数组变为字符串 |
3 |
public byte[] getBytes() |
普通 |
将字符串变为字节数组 |
4 |
public byte[] getBytes(String charsetName) throws UnsupportedEncodingException |
普通 |
编码转化 |
首先对于byte一定要清楚,虽然它的范围要比char小,但是byte还是可以明确的描述出字母的范围的。
范例:利用字节数组实现小写字母变为大写字母的操作
public class StringDemo { public static void main(String args[]) { String str = "helloworld" ; byte data [] = str.getBytes() ; // 将字符串变为字节数组 for (int x = 0 ; x < data.length ; x ++) { data[x] -= 32 ; // 改变编码 } System.out.println(new String(data)) ; } }
3.3、字符串比较
现在至少应该知道了字符串比较有一个equals()方法,但是此方法本身是属于区分大小写的比较,为此在Java里面针对于字符串的比较实际上还有其他方法。
No. |
方法名称 |
类型 |
描述 |
1 |
public boolean equals(String anObject) |
普通 |
区分大小写的字符串比较 |
2 |
public boolean equalsIgnoreCase(String anotherString) |
普通 |
不区分大小写的字符串比较 |
3 |
public int compareTo(String anotherString) |
普通 |
比较字符串大小 |
4 |
public int compareToIgnoreCase(String str) |
普通 |
不区分大小写比较字符串大小 |
范例:观察equals()的问题
public class StringDemo { public static void main(String args[]) { String strA = "hello" ; String strB = "HEllo" ; System.out.println(strA.equals(strB)) ; // false System.out.println(strA.equalsIgnoreCase(strB)) ; // true } }
验证码操作中可真的部分大写或者是小写字母。
在进行字符串相等的比较操作之中,最为关键的一个方法是:compareTo(),这个方法本身返回一个int型数据,而这个int型数据有三种结果:大于(>0)、小于(<0)、等于(=0)。
范例:比较字符串大小
public class StringDemo { public static void main(String args[]) { String strA = "A" ; String strB = "a" ; System.out.println(strA.compareTo(strB)) ; // -32 System.out.println(strA.compareToIgnoreCase(strB)) ; // 0 } }
在compareTo()方法之中要进行比较是依据编码进行相减得来的。所以利用compareTo()返回值范围进行判断就可以区分大小关系了。
public class StringDemo { public static void main(String args[]) { String strA = "a" ; String strB = "a" ; System.out.println(strA.compareTo(strB)) ; // -32 System.out.println(strA.compareToIgnoreCase(strB)) ; // 0 if (strA.compareTo(strB) == 0) { System.out.println("两个字符串的内容相同!") ; } } }
3.4、字符串查找
从一个完整的字符串之中查找一些子字符串,而可以实现这种字符串查找功能的方法有如下几个:
No. |
方法名称 |
类型 |
描述 |
1 |
public boolean contains(String s) |
普通 |
判断某一个字符串是否存在 |
2 |
public int indexOf(String str) |
普通 |
取得某一个子字符串的索引位置,找不到返回-1 |
3 |
public int indexOf(String str, int fromIndex) |
普通 |
从指定索引位置开始检索子字符串位置,找不到就返回-1 |
4 |
public int lastIndexOf(String str) |
普通 |
从后向前查找指定子字符串的位置,找不到返回-1 |
5 |
public int lastIndexOf(String str, int fromIndex) |
普通 |
从指定位置由后向前查找子字符串的位置,找不到返回-1 |
6 |
public boolean startsWith(String prefix) |
普通 |
判断是否以某个字符串开头 |
7 |
public boolean startsWith(String prefix, int toffset) |
普通 |
从指定位置判断是否以某个字符串开头 |
8 |
public boolean endsWith(String suffix) |
普通 |
是否以指定的字符串结尾 |
如果要查找中间的内容,那么现在基本上都使用contains()方法了。
范例:使用contains()方法
public class StringDemo { public static void main(String args[]) { String str = "hello" ; // 0:h、1:e、2:l、3:l、4:o。 if (str.contains("l")) { // 此方法直接返回true或者是false System.out.println("已查找到!") ; } } }
contains()方法虽然现在用的比较多,但是其是在JD K1.5后才提供的。而在JD 1.5之前那么只能够通过indexOf()方法实现。
范例:利用indexOf()方法判断
public class StringDemo { public static void main(String args[]) { String str = "hello" ; // 0:h、1:e、2:l、3:l、4:o。 System.out.println(str.indexOf("l")) ; // 2,第一个满足的位置 if (str.indexOf("l") != -1) { // 现在表示索引查找到了 System.out.println("内容已经查找到!") ; } } }
默认情况下indexOf()都是从第一个字母开始查找。那么也可以利用其的重载方法从指定索引位置查找。
范例:其他查找
public class StringDemo { public static void main(String args[]) { String str = "hello" ; // 0:h、1:e、2:l、3:l、4:o。 System.out.println(str.indexOf("l",3)) ; // 3 System.out.println(str.lastIndexOf("l")) ; // 3 } }
在字符串查找操作里面还可以判断开头或者是结尾。
范例:判断开头或结尾
public class StringDemo { public static void main(String args[]) { String str = "**@@hello##" ; System.out.println(str.startsWith("**")) ; // true System.out.println(str.startsWith("@@",2)) ; // true System.out.println(str.endsWith("##")) ; // true } }
这几个查询现在出现最多的就是contains()、startsWith()、endsWith()。
3.5、字符串截取
可以通过一个完整的字符串截取里面的子字符串,那么这个时候使用方法如下:
No. |
方法名称 |
类型 |
描述 |
1 |
public String substring(int beginIndex) |
普通 |
从指定索引位置截取到结尾 |
2 |
public String substring(int beginIndex, int endIndex) |
普通 |
截取指定索引范围的内容 |
范例:观察截取操作
public class StringDemo { public static void main(String args[]) { String str = "helloworldmldn" ; System.out.println(str.substring(5)) ; // worldmldn System.out.println(str.substring(5,10)) ; // world System.out.println(str.substring(0,5)) ; // hello } }
实际的工作之中,这种截取操作很常用。
3.6、字符串替换
将一个指定的字符串替换为其他内容,就属于替换功能,在String类中有如下的方法支持替换操作。
No. |
方法名称 |
类型 |
描述 |
1 |
public String replaceAll(String regex, String replacement) |
普通 |
字符串的全部替换 |
2 |
public String replaceFirst(String regex,String replacement) |
普通 |
替换第一个内容 |
范例:观察替换操作
public class StringDemo { public static void main(String args[]) { String str = "helloworld" ; System.out.println(str.replaceAll("l","_")) ; System.out.println(str.replaceFirst("l","_")) ; } }
实际上利用替换的功能可以消除掉数据中的全部空格。
public class StringDemo { public static void main(String args[]) { String str = "h e l l o w o rl d" ; System.out.println(str.replaceAll(" ","")) ; } }
3.7、字符串拆分
可以将一个完整的字符串根据指定的内容进行拆分,拆分后的结果就是多个字符串,也就是一个字符串的对象数组,可以使用的操作方法有如下几个:
No. |
方法名称 |
类型 |
描述 |
1 |
public String[] split(String regex) |
普通 |
全部拆分 |
2 |
public String[] split(String regex, int limit) |
普通 |
部分拆分 |
范例:观察split()方法的使用
public class StringDemo { public static void main(String args[]) { String str = "hello world mldn nihao" ; String result [] = str.split(" ") ; // 全拆分 for (int x = 0 ; x < result.length ; x ++) { System.out.println(result[x]) ; } } }
现在是按照空格全部进行了拆分。
范例:拆分部分内容
public class StringDemo { public static void main(String args[]) { String str = "hello world mldn nihao" ; String result [] = str.split(" ",2) ; // 全拆分 for (int x = 0 ; x < result.length ; x ++) { System.out.println(result[x]) ; } } }
现在如果给你一个ip地址,那么一定是按照“.”拆。但是这个“.”不能够直接使用,因为它属于正则表达式的定义范畴,所以日后如果发现不能够拆分的数据必须进行转义,使用“\\”。
范例:拆分IP
public class StringDemo { public static void main(String args[]) { String str = "192.168.1.1" ; String result [] = str.split("\\.") ; // 全拆分 for (int x = 0 ; x < result.length ; x ++) { System.out.println(result[x]) ; } } }
在字符串的组成之中,也可能包含有“|”问题。
public class StringDemo { public static void main(String args[]) { String str = "192|168|1|1" ; String result [] = str.split("\\|") ; // 全拆分 for (int x = 0 ; x < result.length ; x ++) { System.out.println(result[x]) ; } } }
3.8、其他操作方法
以上是一些可以进行归类的操作方法,但是有些操作方法在日后的课程里面还会有详细的讲解,而还有一些小的操作方法。
No. |
方法名称 |
类型 |
描述 |
1 |
public int length() |
普通 |
取得字符串的长度 |
2 |
public boolean isEmpty() |
普通 |
判断是否为空字符串(不是null) |
3 |
public String toLowerCase() |
普通 |
转小写 |
4 |
public String toUpperCase() |
普通 |
转大写 |
5 |
public String trim() |
普通 |
去掉左右空格,但是不去掉中间内容 |
6 |
public String concat(String str) |
普通 |
字符串连接,与“+”一样 |
范例:计算字符串长度
public class StringDemo { public static void main(String args[]) { String str = "士大夫成都市第三,富斗富地方" ; System.out.println(str.length()) ; } }
因为Java使用了Unicode编码,所以中文与英文的长度相同,这一点极大的方便了开发者。
String类中使用的是length()方法,而数组中使用的是“length”属性。
范例:判断是否为空字符串
public class StringDemo { public static void main(String args[]) { String str = "阿斯顿法国和经济,发达是十分" ; System.out.println(str.isEmpty()) ; // false System.out.println("".isEmpty()) ; // true } }
范例:转大、小写操作
public class StringDemo { public static void main(String args[]) { String str = "Hello World !!! (*)*&&^^&*^*^*^*%$&^*(" ; System.out.println(str.toLowerCase()) ; System.out.println(str.toUpperCase()) ; } }
所有字母的内容都发生了转变,但是非字母的内容不会发生改变。
范例:观察trim()方法
public class StringDemo { public static void main(String args[]) { String str = " Hello World " ; System.out.println("字符串内容:【"+str+"】") ; System.out.println("字符串内容:【"+str.trim()+"】") ; } }
范例:字符串连接
public class StringDemo { public static void main(String args[]) { String str = "Hello ".concat("World") ; System.out.println("字符串内容:【"+str+"】") ; // 字符串内容:【Hello World】 } }
基本上开发中不会使用concat()实现字符串的连接,都会使用“+”描述。
非常遗憾的是在整个String类设计的过程之中并没有提供有类似于数据库中的initcap()函数的功能,那么下面可以自己来实现一个首字母大写的操作形式。
范例:实现首字母大写
public class StringDemo { public static void main(String args[]) { String att = "name" ; System.out.println(initcap(att)) ; // Name } public static String initcap(String str) { if (str == null || str.length() == 0) { return str ; } return str.substring(0,1).toUpperCase() + str.substring(1) ; } }
4、总结
No. |
方法名称 |
类型 |
描述 |
1 |
public String(char[] value) |
构造 |
将全部的字符数组作为String的内容 |
2 |
public String(char[] value, int offset, int count) |
构造 |
将部分的字符数组变为字符串,设置字符数组的开始索引与使用个数 |
3 |
public char charAt(int index) |
普通 |
返回指定索引位置上的字符 |
4 |
public char[] toCharArray() |
普通 |
将字符串以字符数组的形式返回 |
5 |
public String(byte[] bytes) |
构造 |
将全部字节数组变为字符串 |
6 |
public String(byte[] bytes, int offset, int length) |
构造 |
将部分字节数组变为字符串 |
7 |
public byte[] getBytes() |
普通 |
将字符串变为字节数组 |
8 |
public byte[] getBytes(String charsetName) throws UnsupportedEncodingException |
普通 |
编码转化 |
9 |
public boolean equals(String anObject) |
普通 |
区分大小写的字符串比较 |
10 |
public boolean equalsIgnoreCase(String anotherString) |
普通 |
不区分大小写的字符串比较 |
11 |
public int compareTo(String anotherString) |
普通 |
比较字符串大小 |
12 |
public int compareToIgnoreCase(String str) |
普通 |
不区分大小写比较字符串大小 |
13 |
public boolean contains(String s) |
普通 |
判断某一个字符串是否存在 |
14 |
public int indexOf(String str) |
普通 |
取得某一个子字符串的索引位置,找不到返回-1 |
15 |
public int indexOf(String str, int fromIndex) |
普通 |
从指定索引位置开始检索子字符串位置,找不到就返回-1 |
16 |
public int lastIndexOf(String str) |
普通 |
从后向前查找指定子字符串的位置,找不到返回-1 |
17 |
public int lastIndexOf(String str, int fromIndex) |
普通 |
从指定位置由后向前查找子字符串的位置,找不到返回-1 |
18 |
public boolean startsWith(String prefix) |
普通 |
判断是否以某个字符串开头 |
19 |
public boolean startsWith(String prefix, int toffset) |
普通 |
从指定位置判断是否以某个字符串开头 |
20 |
public boolean endsWith(String suffix) |
普通 |
是否以指定的字符串结尾 |
21 |
public String substring(int beginIndex) |
普通 |
从指定索引位置截取到结尾 |
22 |
public String substring(int beginIndex, int endIndex) |
普通 |
截取指定索引范围的内容 |
23 |
public String replaceAll(String regex, String replacement) |
普通 |
字符串的全部替换 |
24 |
public String replaceFirst(String regex,String replacement) |
普通 |
替换第一个内容 |
25 |
public String[] split(String regex) |
普通 |
全部拆分 |
26 |
public String[] split(String regex, int limit) |
普通 |
部分拆分 |
27 |
public int length() |
普通 |
取得字符串的长度 |
28 |
public boolean isEmpty() |
普通 |
判断是否为空字符串(不是null) |
29 |
public String toLowerCase() |
普通 |
转小写 |
30 |
public String toUpperCase() |
普通 |
转大写 |
31 |
public String trim() |
普通 |
去掉左右空格,但是不去掉中间内容 |
32 |
public String concat(String str) |
普通 |
字符串连接,与“+”一样 |