初识String类
- 字符、字节和字符串的相互转换
- 字符和字符串的相互转换
- 字节和字符串的相互转换
- 字符串的常见操作
- 字符串比较
- 字符串查找
- 字符串替换
- 字符串拆分
- 字符串截取
- 其他操作方法
- String和StringBuffer
字符、字节和字符串的相互转换
字符和字符串的相互转换
字符串的内部包含字符数组,故字符数组char[ ]和字符串String相互转换。
描述 | 方法名称 |
字符数组全部转换字符串 | public String(char value[]) |
字符数组部分转换字符串 | public String(char value[],offset,count) |
获取一个字符串中指定位置的字符,下标从0开始 | public char charAt() |
将字符串变为字符数组返回 | public char[] toCharArray() |
- 字符数组转换字符串
示例:
public class Test2 {
public static void main(String[] args) {
//字符数组的所有内容转字符串
char[] value = {'s','h','i','A','l','e','n','g','y','a'};
String str1 = new String(value);
System.out.println(str1); //shiAlengya
System.out.println(new String(value)); //shiAlengya
//字符数组的部分内容转换字符串
//value表示要转换的数组,3表示从下标为3的位置开始转换,5表示共转换5个字符
String str2 = new String(value,3,5); //Aleng
System.out.println(str2);
}
- 获取一个字符串中指定位置的字符,下标从0开始
示例:
public class Test2 {
public static void main(String[] args) {
//获取指定位置的字符
String string1 = "alengya";
char ch = string1.charAt(1); //读取1位置的字符,下标从0开始
System.out.println(ch);
}
}
- 将字符串变为字符数组返回
- 以字符串的形式输出字符数组;
- 遍历字符数组,将数组中的字符一个一个的输出
示例:
public class Test2 {
public static void main(String[] args) {
//将字符串变为字符数组返回
char[] chars = string1.toCharArray();
System.out.println(Arrays.toString(chars)); //以字符串的形式输出chars数组
//遍历chars数组,将在数组中的字符一个一个的输出
for (int i = 0;i<chars.length;i++){
System.out.print(chars[i]+" ");
}
System.out.println();
}
- 判断字符串是否全由数字所组成
思路:先将字符串转换成字符数组,再遍历字符数组判断字符是否为数字。
示例:
public class Test2 {
public static boolean isNum(String str){
char[] cha = str.toCharArray();
for (int i = 0;i<cha.length;i++){
if (cha[i]<'0'|| cha[i]>'9'){
return false;
}
}
return true;
}
public static void main(String[] args) {
String string = "Aleng21";
System.out.println(isNum(string)); //false
}
}
字节和字符串的相互转换
描述 | 方法名称 |
字节数组全部转换字符串 | public String(byte bytes[]) |
部分字节数组转换字符串 | public String(byte bytes[],offset,count) |
将字符串变为字节数组返回 | public char[] getByte() |
示例:
public class Test2 {
public static void main(String[] args) {
String str = "WoDeAleng" ;
// 字符串String 转 字节数组byte[]
byte[] bytes = str.getBytes();
for (int i = 0; i<bytes.length;i++){
System.out.print(bytes[i]+" ");
}
System.out.println();
// 字节数组byte[] 转 字符串String
System.out.println(new String(bytes));
//字节数组的部分内容转换字符串
String str = new String(bytes,4,5); //Aleng
System.out.println(str);
//将字符串变为字符数组返回
String string1 = "alengya";
byte[] bytes1 = string1.getBytes();
System.out.println(Arrays.toString(bytes1));
for (int i = 0;i<bytes1.length;i++){
System.out.print(bytes1[i]+" ");
}
System.out.println();
}
}
小结:
char[ ]:是将字符串String按照一个字符一个字符的处理,适合针对文本操作;
byte[ ]:是将字符串String按照一个字节一个字节的处理,适合针对二进制操作。
字符串的常见操作
字符串比较
描述 | 方法名称 |
区分大小写比较 | public boolean equals(Object anObject) |
不区分大小写比较 | public boolean equalsIgnoreCase(String anotherString) |
比较两个字符串的大小关系 | public int compareTo(String anotherString) |
示例:
public class Handle {
public static void main(String[] args) {
String str1 = "aleng";
String str2 = "Aleng";
//区分大小写比较
System.out.println(str1.equals(str2)); //false
//不区分大小写比较
System.out.println(str1.equalsIgnoreCase(str2)); //true
//比较两个字符串的大小关系
System.out.println("a".compareTo("A")); //32
System.out.println("A".compareTo("a")); //-32
}
}
字符串查找
描述 | 方法名称 |
判断一个字符串是否存在 | public boolean contains(CharSe) |
从开始查找指定字符串的位置 | public int indexOf(String str) |
从指定位置开始查找子字符串的位置 | public int indexOf(String str,int fromIndex) |
从后向前查找子字符串的位置 | public int lastIndexOf(String str) |
从指定位置由后向前查找 | public int lastIndexOf(String str, int formIndex) |
判断是否由指定字符串开头 | public boolean startWith(String str) |
从指定位置开始判断是否由指定字符串开头 | public boolean startWith(String str,int toffset) |
判断是否由指定字符串结尾 | public boolean endWith(String str) |
示例:
public class Handle {
public static void main(String[] args) {
/*字符串查找*/
//判断一个字符串是否存在
String string = "xuanaleng";
System.out.println(string.contains("a")); //true
//从开始查找指定字符串的位置
String str1 = "aleng";
String str2 = "cheng";
System.out.println(string.indexOf(str1)); //4
System.out.println(string.indexOf(str2)); //-1
//从指定位置开始查找子字符串的位置
System.out.println(string.indexOf(str1, 3)); //4
System.out.println(string.indexOf(str1, 5)); //-1
//从后向前查找子字符串的位置
System.out.println(string.lastIndexOf(str1)); //4
//从指定位置由后向前查找
System.out.println(string.lastIndexOf(str1, 3)); //-1
System.out.println(string.lastIndexOf(str1, 8)); //4
System.out.println(string.startsWith("a")); //false
System.out.println(string.startsWith("xuan")); //true
System.out.println(string.startsWith(str1, 4)); //true
System.out.println(string.endsWith(str1)); //true
System.out.println(string.endsWith("cheng"));
}
}
字符串替换
描述 | 方法名称 |
替换所有的指定内容 | public String replaceAll(String regex,String replacement) |
替换首个内容 | public String replaceFirst(String regex,String replacement) |
示例:
public class Handle {
public static void main(String[] args) {
/*字符串替换*/
String reg = "loveyou";
//替换所有的指定内容
System.out.println(reg.replaceAll("o", "i")); //liveyiu
//替换首个内容
System.out.println(reg.replaceFirst("o", "i")); //liveyou
}
}
字符串拆分
描述 | 方法名称 |
将字符串全部拆分 | public String[] split(String regex) |
将字符串部分拆分,该数组长度就是limit极限 | public String[] split(String regex,int limit) |
示例:
public class Handle {
public static void main(String[] args) {
/* 字符串拆分*/
String string1 = "woideiailengiduidu";
//将字符串全部拆分
String[] ret = string1.split("i");
System.out.println(Arrays.toString(ret)); //[wo, de, a, leng, du, du]
//将字符串部分拆分,该数组长度就是limit极限
String[] ret1 = string1.split("i",3);
System.out.println(Arrays.toString(ret1)); //[wo, de, ailengiduidu]
}
}
字符串截取
描述 | 方法名称 |
从指定索引截取到结尾 | public String subString(int beginIndex) |
截取部分内容 | public String subString(int beginIndex,int endIndex) |
public class Handle {
public static void main(String[] args) {
/*字符串截取*/
//从指定索引截取到结尾
String ret3 = string1.substring(7);
System.out.println(ret3);
//截取部分内容
String ret4 = string1.substring(8,12); //左开右闭,从8位置开始到12不包含12
System.out.println(ret4);
}
}
其他操作方法
描述 | 方法名称 |
去掉左右空格,不包含中间空格 | public String trim() |
字符串转大写 | public String toUpperCase() |
字符串转小写 | public String toLowerCase() |
字符串入池操作 | public native String intern(byte bytes[]) |
字符串连接,等同于+,不入池 | public String cocat(String str) |
取得字符串长度 | public int length() |
判断是否为空字符串,但不是null,而是长度为0 | public boolean isEmpty() |
public class Handle {
public static void main(String[] args) {
/*其他操作方法*/
//|去掉左右空格,不包含中间空格
String string3 = " aleng du du";
String string4 = " Hello World";
System.out.println(string3.trim()); //aleng du du
// 字符串转大写
System.out.println(string3.toUpperCase()); //ALENG DU DU
// 字符串转小写
System.out.println(string4.toLowerCase()); //hello world
// 字符串入池操作
string4.intern();
String string5 = " Hello World";
System.out.println(string4 == string5); //true 判断 Hello World是否入池
// 字符串连接,等同于+,不入池
System.out.println(string3.concat(string4)); // aleng du du Hello World
// |取得字符串长度|public char[] getByte()
System.out.println(string3.length()); //12
// 判断是否为空字符串,但不是null,而是长度为0
System.out.println(string3.isEmpty()); //false
}
}
String和StringBuffer
任何字符串都是String对象,String的常量是不可变的,如果改变对象的内容则只是改变引用的指向;
而为了方便的修改字符串,提供了StringBuffer类。
String通常使用“+”进行字符串连接,而在StringBuffer类中使用append()方法即可完成操作。
public class Handle {
public static void main(String[] args) {
StringBuffer stringBuffer = new StringBuffer("hello");
//使用append()方法对字符串进行修改操作
System.out.println(stringBuffer.append("world"));
//System.out.println(stringBuffer.reverse());
}
}
String和StringBuffer最大的区别在于:String类内容不可修改,StringBuffer类的内容可以修改。
StringBuffer类还有一些字符串反转、删除和插入等操作。
字符串反转调用StringBuffer类的reverse()方法。
public class Handle {
public static void main(String[] args) {
StringBuffer stringBuffer = new StringBuffer("hello");
//使用reverse()方法使字符串反转
System.out.println(stringBuffer.reverse());
}
}
删除指定范围的字符操作
public class Handle {
public static void main(String[] args) {
StringBuffer stringBuffer = new StringBuffer("helloworld");
//使用delete()方法删除指定范围字符
System.out.println(stringBuffer.delete(5,9));
}
}
插入数据
public class Handle {
public static void main(String[] args) {
StringBuffer stringBuffer = new StringBuffer("helloworld");
//使用delete()方法删除指定范围字符
System.out.println(stringBuffer.insert(0,"你好哇,"));
}
}