字符与字符串
很多的语言之中都是利用了字符数组的概念来描述字符串的信息,这一点很多语言都用到了。
No
方法名称
类型
描述
1
public String (char [] value)
构造
将字符数组转换为字符串
2
public String (char [] value,int offset,int count)
构造
将部分字符数组转换为字符串
3
public char charAt(int index)
普通
返回制定索引的字符串
4
public char[] toCharArray()
普通
将字符串以字符数组形式返回
范例:取出制定索引的字符
public class StringDemo{
public static void main(String []args){
String str = "hello";
char c = str.charAt(0);
System.out.println(c);
}
}
范例:将字符串变成字符数组
public class StringDemo{
public static void main(String []args){
String str = "hello";
char[] charArray = str.toCharArray();
}
}
范例:将字符串转大写
public class StringDemo{
public static void main(String []args){
String str = "hello";
char[] charArray = str.toCharArray();
for(int i = 0;i < charArray.length; i ++){
//小写变为大写,只需要减去32即可
charArray[i] -= 32;
}
System.out.println(new String(charArray));
System.out.println(new String(charArray,1,2));
}
}
范例:给定一个字符串,要求判断是否由数字组成
public class StringDemo{
public static void main(String [] args){
String str = "123321";
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
if(data[x] > '9' || data[x] < '0'){
return false;
}
}
return true;
}
}
字节与字符串
字节使用byte描述,使用字节一般主要用于数据的传输或者是编码的转换的时候使用,而在String里面,就提供了将字符串变为字节数组的操作,目的就是为了传输以及编码转换。
No
方法名称
类型
描述
1
public String (byte[] bytes)
构造
将全部的字节数组变为字符串
2
public Strint(byte[] bytes,int offset,int length)
构造
将部分字节数组变为字符串
3
public byte[] getBytes()
普通
将字符串变为字节数组
4
public byte[] getBytes(String chasetName) throws UnsupportedEncodingException
普通
进行编码转换
范例:观察字符串与字节数组的转换
public class StringDemo{
public static void main(String []args){
String str = "HelloWorld";
byte []data = str.getBytes();//将字符串变为字节数组
for(int i = 0 ; i < data.length ; i++){
data[x] -= 32;//变为大写
}
System.out.println(new String(data));
System.out.println(new String(data,5,5));
}
}
因为现在操作的是因为字母,所以感觉与字符类似,
在以后讲解IO操作的时候会牵连到这种字节数组的操作,会涉及到乱码转码的问题
字符串的比较
如果要进行字符串内容相等的判断使用equals(),但是在String类里面定义的比较判断不止这一个。
】
No
方法名称
类型
描述
1
public boolean equals(String object)
普通
进行相等的判断,它区分大小写
2
public boolean equalsIgnoreCase(String temp)
普通
进行相等的判断,不区分大小写
3
public int compareTo(String anotherString)
普通
判断两个字符串的大小(按照字符编码比较),此方法的返回值有三种结果: = 0:表示要比较的两个字符串内容相等;:>0:表示大于的结果;<0:表示小于的结果
4
5
6
范例:观察comparaTo(String temp)方法
public class StringDemo{
public static void main(String []args){
String stra = "hello";
String strb = "HELLO";
System.out.println(stra.comparaTo(strb));
//32,结果是 stra > strb
}
}
现在只有String类的对象才具有大小的关系判断。
字符串查找
从一个完整的字符串之中要判断某一个子字符串是否存在,这个功能可以使用如下的方法完成。
No
MethodName
Type
Description
1
public boolean contains(String temp)
普通
判断指定的内容是否存在,如果存在返回true
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)
普通
判断是否以指定的字符串开头,如果是返回true,否则返回false
7
public boolean startsWith(String prefix, int toffset)
普通
从指定为位置开始,判断是否以指定字符串开头
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"));
}
}
从JDK1.5之后出现了contains()方法,这个方法可以直接返回boolean,这样省略了我们很多代码,我们也推荐使用这样的方法来判断是否包含该字符串。
字符串替换
指的就是使用心得字符串替换掉旧的字符串数据,支持的方法有如下。
No
MethodName
Type
Description
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","_"));
}
}
字符串截取
从一个完整的字符串之中可以截取部分的子字符串数据,支持的方法如下
No
MethodName
Type
Description
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);
}
}
一定要记住,数据库中的函数由于考虑到是非专业人员使用操作,所以有些代码尽可能做了一些调整,但是程序是要求严谨性的,所以负数是不可能使用负数作为截取的开始点。
字符串拆分
将一个完整的字符串,按照指定的内容,拆分为字符串数组(对象数组,String对象),方法如下。
No
MethodName
Type
Description
1
public String[] split(String regex)
普通
按照指定字符串拆分为字符串数组
2
public String[] split(String regex)
普通
按照指定字符串进行部分拆分,最后的一个数组的长度由limit决定的。
范例:
public class StringDemo{
public static void main(String [] args){
String str = "HelloWorld";
//根据空格拆分字符串数组
String[] result = str.split(" ");
for(int i = 0 ; i < result.length ; i++ ){
System.out.println(result[x]);
}
}
}
如果在拆分的时候只是写了一个空字符串("",不是null),那么就会按照每一个字符拆分
范例:拆分IP地址
public class StringDemo{
public static void main(String [] args){
String str = "192.168.1.2";
String[] result = str.split(".");
for(int i = 0 ;i < result.length ; i++){
System.out.println(result[i]);
}
}
}
然后一运行发现,并没有拆分开来,没有输出任何东西,原因:需要转义,系统识别不了
public class StringDemo{
public static void main(String [] args){
String str = "192.168.1.2";
String[] result = str.split("\\.");
for(int i = 0 ;i < result.length ; i++){
System.out.println(result[i]);
}
}
}
如果是一写敏感字符(正则标记),严格来说是拆分不了的,如果真的遇见了拆分不了的情况,那么使用 \\(就是转义),进行转义后,才可以拆分。
范例:进行以下字符串
public class StringDemo{
public static void main(String [] args){
String str = "张三:18|李四:20|王五:24";
String [] result = str.split("\\|");
for(int i : 0 ; i < result.length ; i++){
String temp[] = result[i].split(":");
System.out.println("姓名:"+temp[0]+",年龄:"+temp[0]);
}
}
}
以后的格式可以由你根据需要任意的组合。
其他方法
以上给出的方法是可以归类的,但是在String里面也有一部分的方法是不能归类的
No
MethodName
Type
Description
1
public String concat(String str)
普通
字符串的链接,与“ + ”类似
2
public String toLowerCase()
普通
字符串转小写
3
public String toUpperCase()
普通
字符串转大写
4
public String trim()
普通
去掉字符串两边空格,留下中间空格
5
public int length()
普通
取得字符串长度
6
public String intern()
普通
字符串入池
7
public boolean isEmpty()
普通
判断是否是空字符串(空字符串不是null,而是"",长度0)
8
9
范例:字符串的连接
public class StringDemo{
public static void main(String[] args){
String stra = "hello";
stra.concat("");
String strb = "hello "+"world";
String strc = "hello world"
System.out.println(strb == strc);
}
}
范例: 转小写与大写
public class StringDemo{
public static void main(String[] args){
String stra = "hello*(*)"
System.out.println(stra.toUpperCase());
System.out.println(stra.toLowerCase());
}
}
非字母字符不会被转义
范例:去掉空格
public class StringDemo{
public static void main(String[] args){
String str = " hello ";
System.out.println("【"+str+"】");
System.out.println("【"+str.trim()+"】");
}
}
一般在用户进行输入的时候,有可能会带有无用的空格内容,那么接受到这些数据后,就会消除掉所有的空格内容
范例:取得字符串的长度
public class StringDemo{
public static void main(String[] args){
String str = "helloworld";
System.out.println(str.length());
}
}
在某些情况下,我们会要求用户输入的数据长度是有限的,可以利用此方法判断数据长度。
数组对象.length
String对象.length()
范例:判断是否为空字符串
public class StringDemo{
public static void main(String[] args){
String str = "helloworld";
// false
System.out.println(str.isEmpty);
// true
System.out.println("".isEmpty);
}
}
如果觉得isEmpty()不方便,可以使用 "".equals(str)
String类虽然提供了大量的支持的方法,但是却少了一个很重要的方法,而这样的方法只能由我们自己实现。
public class StringDemo{
public static void main(String [] args){
String str = "helloWorld";
System.out.println(initcap(str));
}
// 首字母大写
public static String initcap(String temp){
return temp.substring(0,1).toUpperCase()+temp.substring(1);
}
}
虽然Java的类库里面没有此类功能,但是很多第三方的组件包会提供。