1、本次的预计讲解的知识
String类给出的各个方法要求要被下来。
2、具体内容
String在所有的项目开发里面一定要使用到,那么在String类里面提供了一系列的功能操作方法,本来学习大部分的操作方法,而对于有一些操作,需要等待全部的知识掌握之后才可以进行后续的学习。
对于烯烃类的的方法,一定要去查询文档,一些不常用的方法允许不知道,但是一定要会查,而对于String类的一些方法由于使用的情况比较多,为了方便开发必须背。
对于每一个文档的内容而言,它都有以下的部分所组成:
·类的定义以及相关的继承结构;
·类的一些简短的说明
·类的成员组成;
·类中所提到的构造方法;
·类中所提供的普通方法;
·成员、构造方法、普通方法的详细说明。
3.1、字符与字符串
很多语言之中都是利用了字符数组的概念来描述字符串的信息,这一点在Sring类的方法上也都有提示
No. | 方法名称 | 类型 | 描述 |
1 | public String (char[] value) | 构造 | 将组字符数组变为String类对象 |
2 | public String (char[] value,int offset,int count) | 构造 | 将组部分字符数组变为String类 |
3 | public char cahrAt(int index) | 普通 | 返回指定所索引对应的字符信息 |
4 | public char[] toCharArrary() | 普通 | 将字符串以字符数组的形式返回 |
范例:取出指定索引的字符
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 = "hello"; char [] data = str.toCharArray();//将字符串变成字符数组 for(int x=0; x<data.length; x++) { System.out.print(data[x] + "、"); } } } |
范例:将字符串转大写
public class StringDemo { public static void main (String [] args) { String str = "hello"; char [] data = str.toCharArray();//将字符串变成字符数组 for(int x=0; x<data.length; x++) { data[x] -= 32; } System.out.println(new String(data));//将全部字符数组变成String System.out.println(new String(data,1,2));//将部分字符数组变成String } } |
范例:给定一个字符串,要求判断其是否是数字组成
思路:如果整个字符串要判断是不是数字无法实现,但是可以将字符串变为字符数组,而后判断每一个字符的内容是否是数字,如果该字符的范围在(‘0’~’9’)指定的范畴之内,那么就是数字。
public class StringDemo { public static void main (String [] args) { String str = "12a3456"; if(isNumber(str)) { System.out.println("字符串由数字组成"); } else { System.out.println("字符串由非数字组成"); } } //判断字符串是否由数字所组成,如果是返回true,否则返回false public static boolean isNumber(String temp) { //将字符串变为字符数组,这样就可以取出每一个字符 char [] data = temp.toCharArray(); for(int x=0; x<data.length; x++) { if(data[x]>'9' || data[x]<'0') { return false;//后续不再判断 } } return true; //如果全部验证通过就返回true } } |
如果写的某一个方法返回的内容是boolean,那么习惯性的做法是将其以“isXxx”进行命名。
2.2、字节与字符串
字节使用byte描述,使用字节一般主要用于数据的传输或者编码转换的时候使用,而在String里面那就提供有将字符串变为字节数组的操作,目的就是为传输以及编码转换。
No. | 方法名称 | 类型 | 描述 |
1 | public String (byte[] bytes) | 构造 | 将全部的字节数组变为字符串 |
2 | public String (byte[] bytes,int offer,int length) | 构造 | 将部分的字节数组变为字符串 |
3 | public byte[] getBytes() | 普通 | 将字符串变为字节数组 |
4 | public byte[] getBytes(String cahrseName) throws UnsupportedEncondingException | 普通 | 进行编码转换 |
范例:观察字符串与字节数组的转换
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));//全部转换 System.out.println(new String(data,5,5));//部分转换 } } |
因为现在操作的英文字符,所以感觉与字符类似。
在以后讲解IO操作的时候睡牵扯到这种字节数组的操作,在讲解后续的开发的时候会逐步接触到乱码的问题。
2.3、字符串的比较
如果进行字符串的相等判断使用equals(),但是在String类里面定义的比较判断不止这一个
No. | 方法名称 | 类型 | 描述 |
1 | public boolean equals(String anObject) | 普通 | 进行相等判断,区分大小写 |
2 | Public boolean equalsIgnoreCase(String anotherString) | 普通 | 进行相等判断,不区分大小写 |
3 | public int compareTo(String anoterString) | 普通 | 判断两个字符串的大小(按照字符编码比较),此方法的返回值有如下三种结果: ·=0:表示要比较的两个字符内容相等; ·>0:表示大于的结果; ·<0:表示小于的结果。 |
范例:相等判断
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()方法
public class StringDemo { public static void main (String [] args) { String stra = "Hello"; String strb = "HEllo"; System.out.println(stra.compareTo(strb));//32; 大于0 //可以利用大小等于0的方式来判断大小输出 if(stra.compareTo(strb) > 0) { System.out.println("大于"); } } } |
现在只有String类的对象才具有大小的关系判断。
2.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 starsWith(String perfix) | 普通 | 判断是否以指定的字符串开头,如果是返回true,否则返回false |
7 | public boolean starsWith(String perfix,int toffser) | 普通 | 从指定位置判断是否以指定的字符串开头 |
8 | public boolean endsWith(String suffix) | 普通 | 判断是否以指定的字符串结尾 |
范例:indexOf等功能查找
public class StringDemo { public static void main (String [] args) { String str = "helloworld"; //返回满足条件单词的第一个字母的索引 System.out.println(str.indexOf("world")); //返回的是第一个查找到的 System.out.println(str.indexOf("l")); System.out.println(str.indexOf("l",5)); //从后开始查找 System.out.println(str.lastIndexOf("l")); } } |
以上的过程都只是返回了位置。但是在一些程序之中需要甘肃用户的是有没有的结果,最早的做法是判断查询结果是否是“-1”来实现的。
public class StringDemo { public static void main (String [] args) { String str = "helloworld"; if(str.indexOf("world") != -1) {//能找到 System.out.println("可以查询到数据"); } } } |
但是动JDK1.5开始之后出现了contains()方法可以直接返回boolean
public class StringDemo { public static void main (String [] args) { String str = "helloworld"; if(str.contains("world")) {//能找到 System.out.println("可以查询到数据"); } } } |
使用contains()更加的简单,并且在整个的Java里面contians已经成为查询的代名词。
范例:开头或结尾判断
public class StringDemo { public static void main (String [] args) { String str = "##@@hello**";//能找到 System.out.println(str.startsWith("##")); System.out.println(str.startsWith("@@",2)); System.out.println(str.endsWith("**")); } } |
这些开头或结尾的判断往往可以作为一些标记定在程序之中出现。
2.5、字符串替换
指的是使用一个新的字符串替换掉旧的字符串数据,支持的方法有如下几个:
No. | 方法名称 | 类型 | 描述 |
1 | public String repaceAll(String regex(正则),String replacement) | 普通 | 用新的内容替换掉全的旧的 |
2 | public String repaceFirst(String regex,String replacement) | 普通 | 替换首个满足条件的内容 |
范例:观察替换结果
public class StringDemo { public static void main (String [] args) { String str = "helloworld";//能找到 String resultA = str.replaceAll("l", "_");//全部替换 String resultB = str.replaceFirst("l", "_");//替换首个 System.out.println(resultA); System.out.println(resultB); } } |
对于替换的操作后续还会有更加完善的讲解。
2.6、字符串的截取
从一个完整的字符串之中可以截取部分的字符串的数据,支持的方法如下:
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 = "helloworld"; String resultA = str.substring(5); String resultB = str.substring(0, 5); System.out.println(resultA); System.out.println(resultB); } } |
一定要记住,数据库中的数据由于考虑到有可能是非专业的的人员进行使用,所以代码有些做了一些调整,但是程序是要求严谨性的,所以不可能使用负数作为截取的开始点。
2.7、字符串拆分
讲一个完整的字符串,按照指定的内容拆分为字符串数组(对象数组,String对象)方法如下:
No. | 方法名称 | 类型 | 描述 |
1 | public String split(String regex) | 普通 | 按照指定的字符串进行全部拆分 |
2 | public String split(String regex,int limit) | 普通 | 按照指定的字符串进行进行拆分,最后的数组长度就是有limit决定的(最多为limit的值),即:前面拆,后面不拆作为整体 |
范例:进行全部拆分
public class StringDemo { public static void main (String [] args) { String str = "hello world nihao mldn"; String result[] = str.split(" "); for(int x=0; x<result.length; x++) { System.out.println(result[x]); } } } |
如果在拆分的时候只是写了一个空字符串(“”不是null)按照每一个字符进行拆分。
范例:部分拆分
public class StringDemo { public static void main (String [] args) { String str = "hello world nihao mldn"; String result[] = str.split(" ",3); for(int x=0; x<result.length; x++) { System.out.println(result[x]); } } } |
范例:实现IPv4地址拆分
public class StringDemo { public static void main (String [] args) { String str = "192.168.1.2"; String result[] = str.split("\\."); for(int x=0; x<result.length; x++) { System.out.println(result[x]); } } } |
如果是敏感字符(正则标记)严格来讲是拆不了,如果真遇见了拆分不了的情况,那么使用“\\(就是(\))”进行转义后才可以拆分。
在实际的开发之中,拆分的操作是非常常见的,因为很多的时候会传递一组数据到程序之中进行处理,例如:现在有如下的一个字符:“张三:20|李四:21|王五:22...”(姓名:年龄|姓名:年龄|...)。当接受到此数据时必须要对数据进行拆分
public class StringDemo { public static void main (String [] args) { String str = "张三:20|李四:21|王五:22"; String result[] = str.split("\\|"); for(int x=0; x<result.length; x++) { String temp[] = result[x].split(":"); System.out.println("姓名:" + temp[0] + ",年龄:" + temp[1]); } } } |
以后的格式可以有根据需要任意的组合。
2.8、其他方法
以上给出的方法是可以归类的,但是在String里面也有一部分方法是不能够归类的:
No. | 方法名称 | 类型 | 描述 |
1 | public String concat(String str) | 普通 | 字符串的连接,与“+”类似 |
2 | public String toLowerCase() | 普通 | 转小写 |
3 | public String toUpperCase() | 普通 | 转大写 |
4 | public String trim() | 普通 | 去掉字符串左右两边的空格,中间空格保留 |
5 | public length() | 普通 | 取得字符串长度 |
6 | public intern() | 普通 | 数据入池 |
7 | public boolean isEmpty() | 普通 | 判断是否是空字符串(不是null,而是””长度是0) |
范例:字符串的连接
public class StringDemo { public static void main (String [] args) { String stra = "hello"; String strb = stra + "world";//String strb = stra.concat("world");等价 String strc = "hello world"; System.out.println(strb == strc); } } |
范例:转小写和大写操作
public class StringDemo { public static void main (String [] args) { String str = "(*XXhello(*(*"; System.out.println(str.toLowerCase()); System.out.println(str.toUpperCase()); } } |
所有的非字母数据不会进行任何的转换操作。
范例:去掉空格
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 world "; System.out.println(str.length()); } } |
在某些情况下要求用户输入的数据长度是有限制的,可以利用此方式判断。数组里也有一个length属性,但是调用的形式不同:
·数组对象.length;
·字符串.length()。
范例:判断是否是空字符串
public class StringDemo { public static void main (String [] args) { String str = " hello world "; System.out.println(str.isEmpty());//false System.out.println("".isEmpty());//true } } |
以后觉得isEmpty()不方便可以使用“””.equals(str)”。
String类提供了大量的支持的方法,但是缺少了一个重要的方法 ——initcap()的功能,首字母大写,其余小写,而这样的功能只能自己实现
public class StringDemo { public static void main (String [] args) { String str = "stuName"; System.out.println(initcap(str)); } public static String initcap(String temp) { return temp.substring(0, 1).toUpperCase() + temp.substring(1); } } |
虽然Java的类库里面没有此功能,但是一些第三方法组件包会提供,例如:apache的commons组件包。
3、总结
所有的讲解过的方法要求:记下方法名称、方法作用、参数的类型及个数、返回值类型,必须背下来,否则在写代码的时候受到很大的挫折感。
No. | 方法名称 | 类型 | 描述 |
1 | public String (char[] value) | 构造 | 将组字符数组变为String类对象 |
2 | public String (char[] value,int offset,int count) | 构造 | 将组部分字符数组变为String类 |
3 | public cahr cahrAt(int index) | 普通 | 返回指定所索引对应的字符信息 |
4 | public char[] toCharArrary() | 普通 | 将字符串以字符数组的形式返回 |
5 | public String (byte[] bytes) | 构造 | 将全部的字节数组变为字符串 |
6 | public String (byte[] bytes,int offer,int length) | 构造 | 将部分的字节数组变为字符串 |
7 | public byte[] getBytes() | 普通 | 将字符串变为字节数组 |
8 | public byte[] getBytes(String cahrseName) throws UnsupportedEncondingException | 普通 | 进行编码转换 |
9 | public boolean equals(String anotherString) | 普通 | 进行相等判断,区分大小写 |
10 | Public boolean equalsIgnoreCase(String anotherString) | 普通 | 进行相等判断,不区分大小写 |
11 | public int compareTo(String anoterString) | 普通 | 判断两个字符串的大小(按照字符编码比较),此方法的返回值有如下三种结果: ·=0:表示要比较的两个字符内容相等; ·>0:表示大于的结果; ·<0:表示小于的结果。 |
12 | public boolean contains(String s) | 普通 | 判断指定的内容是否内存 |
13 | public int indeOf(String str) | 普通 | 由前向后查找指定字符串的位置,如果查找到了则返回(第一个字母)位置的索引,如果找不到返回-1 |
14 | public int indeOf(String str,int fromIndex) | 普通 | 从指定位置由前向后查找指定字符串的位置,找不到返回-1 |
15 | public int lastIndexOf(String str) | 普通 | 由后向前查找指定字符串的位置,找不到返回-1 |
16 | public int lastIndexOf(String str,int fromIndex) | 普通 | 从指定位置由后向前查找指定字符串的位置,找不到返回-1 |
17 | public boolean starsWith(String perfix) | 普通 | 判断是否以指定的字符串开头,如果是返回true,否则返回false |
18 | public boolean starsWith(String perfix,int toffser) | 普通 | 从指定位置判断是否以指定的字符串开头 |
19 | public boolean endsWith(String suffix) | 普通 | 判断是否以指定的字符串结尾 |
20 | public String repaceAll(String regex(正则),String replacement) | 普通 | 用新的内容替换掉全的旧的 |
21 | public String repaceFirst(String regex,String replacement) | 普通 | 替换收个满足条件的内容 |
22 | public String substring(int beginIndex) | 普通 | 从指定索引截取到结尾 |
23 | public String substring(int beginIndex,int endIndex) | 普通 | 截取部分字符串的数据 |
24 | public String split(String regex) | 普通 | 按照指定的字符串进行全部拆分 |
25 | public String split(String regex,int limit) | 普通 | 按照指定的字符串进行进行拆分,最后的数组长度就是有limit决定的(最多为limit的值),即:前面拆,后面不拆作为整体 |
26 | public String concat(String str) | 普通 | 字符串的连接,与“+”类似 |
27 | public String toLowerCase() | 普通 | 转小写 |
28 | public String toUpperCase() | 普通 | 转大写 |
29 | public String trim() | 普通 | 去掉字符串左右两边的空格,中间空格保留 |
30 | public length() | 普通 | 取得字符串长度 |
31 | public intern() | 普通 | 数据入池 |
32 | public boolean isEmpty() | 普通 | 判断是否是空字符串(不是null,而是””长度是0) |