String类中的方法
字符串与字符
pulbic String (char[] value)
//将字符数组转换为全部字符串,该方法为构造方法
public String (char[] value, int offset.int count)
//将字符数组,从指定的索引下转换为指定个数的字符串,该方法为构造方法
public String charAt(int index)
//获得字符串指定索引下的字符
public Char[] toChatArray()
//将字符串装换为字符数组
- 范例 字符数组和字符串的互转换
public class YootkDemo{
public static void main(String arge[]){
String message="www.baidu.com";//定义一个字符串
char[] temp=message.toCharArray();//将字符串装换为字符数组
for(char x: temp){
System.out.print(x+"、");//输出字符数组
}
System.out.println();//换行
String result=new String(temp);//调用构造方法,将字符数组转换为字符串
System.out.println(result);
}
}
//w、w、w、.、b、a、i、d、u、.、c、o、m、
//www.baidu.com
- 范例 将字符数组,从指定的索引下转换为指定个数的字符串
public class YootkDemo{
public static void main(String arge[]){
String message="www.baidu.com";//定义一个字符串
char[] temp=message.toCharArray();//将字符串装换为字符数组
for(char x: temp){
System.out.print(x+"、");//输出字符数组
}
System.out.println();//换行
String result=new String(temp,4,5);//从第四个索引开始,转换5个字符,转换为字符串
System.out.println(result);
}
}
//w、w、w、.、b、a、i、d、u、.、c、o、m、
//baidu
- 范例 获得字符串指定索引下的字符
public class YootkDemo{
public static void main(String arge[]){
String message="www.baidu.com";//定义一个字符串
System.out.println(message.charAt(4)); //获取索引为4的字符
System.out.println(message.charAt(55));//获取索引为55的字符,出现数组下标越界异常
}
}
//b
//StringIndexOutOfBoundsException:
在使用该方法是,要注意字符串的索引值,避免出现数组下标越界的异常
字符串与字节
public String (byte[] bytes)
//将字节数组全部转换为字符串
public String (byte[] bytes,int offset,int count)
//将字节数组指定下的索引,装换为指定长度的字符串
public byte[] getBytes();
//将字符串转换为字节数组
- 范例
public class YootkDemo{
public static void main(String arge[]){
String message="www";//定义一个字符串
byte[] result= message.getBytes();//将字符串数组转换为字节数组
for(int x=0;x<result.length;x++){
result[x]-=32;//转换为大写
}
System.out.println(new String(result));//将字节数组转换为字符串
System.out.println(new String(result,0,2));//将字节数组,从0号索引开始,转换2个位字符串
}
}
//WWW
//WW
字符串的比较
public boolean equals(Object anObject)
//判断两个字符串是否相同 区分大小写
public boolean equalsIgnoreCase(Object object)
//判断两个字符串是否相同 不区分大小写
public int compareTo(String anotherString)
//比较两个字符串的大小值,返回其两个字符串的差值 区分大小写
public int compareTOIgnoreCase(String str)
//比较两个字符串的大小值,返回其两个字符串的差值 不区分大小写
- 范例
public class YootkDemo{
public static void main(String arge[]){
String messageA="w";//定义一个小写字母的字符串
String messageB="W";//定义一个大写字母的字符串
System.out.println(messageA.equals(messageB));//比较两个字符串是否相同 区分大小写
System.out.println(messageA.equalsIgnoreCase(messageB));//比较两个字符串是否相同 不区分大小写
System.out.println(messageA.compareTo(messageB));//比较两个字符串的大小值 返回其差值 区分大小写
System.out.println(messageA.compareToIgnoreCase(messageB));//比较两个字符串的大小值 返回其差值 不区分大小写
}
}
//false
//true
//32
//0
字符串的查找
public boolean comtains(charsequence s)
//判断该字符串中是否存在其子字符串
public int indexOf(String s)
//从第一个索引开始,从前往后查找是否存在其子字符串,找到放回其索引值,找不到返回-1
public int indexOf(String s,int formIndex)
//从指定的索引开始,从前往后查找是否存在其子字符串,找到放回其索引值,找不到返回-1
public int lastIndexOf(String s)
//从第最后一个索引开始,从后往前查找是否存在其子字符串,找到放回其索引值,找不到返回-1
public int lastIndexOf(String s,int formIndex)
//从第最后一个索引开始,从后往前查找是否存在其子字符串,找到放回其索引值,找不到返回-1
public boolean startsWich(String prefix)
//判断是否已该字符串开头
public boolean endsWich(String saffix)
//判断是否已该字符串结尾
- 范例
public class YootkDemo{
public static void main(String arge[]){
String message="www.bai.qq.com www.baidu.com";//定义一个小写字母的字符串
System.out.println(message.contains("qq"));//判断字符串中是否存在其子字符串
System.out.println(message.indexOf("com"));//从前往后查找该字符串所在的索引值
System.out.println(message.indexOf("com",13));//从指定位置往前查找该字符串的索引值
System.out.println(message.lastIndexOf("com"));//从后往前查找该字符串的索引值
System.out.println(message.lastIndexOf("com",23));//从指定位置往后查找该字符串的索引值
System.out.println(message.startsWith("www"));//判断是否由此字符串开头
System.out.println(message.endsWith("com"));//判断时候由此字符串结尾
}
}
/*
true
11
25
25
11
true
true
*/
字符串的替换
public String replaceAll(String regex,String replacement)
//将指定字符串全部替换成新的字符串
public String replaceFirst(String regex,String replacement)
//将指定字符串的第一次替换成新的字符串
- 范例
public class YootkDemo{
public static void main(String arge[]){
String message="www.bai.qq.com www.baidu.com";
//全部替换
System.out.println(message.replaceAll("www","Andy"));
//替换第一个
System.out.println(message.replaceFirst("www","Andy"));
}
}
/*
Andy.bai.qq.com Andy.baidu.com
Andy.bai.qq.com www.baidu.com
*/
字符串的拆分
public String[] split(String regex)
//将字符串按条件全部拆分为一个字符串数组
public String[] split(String regex,int linit)
//将字符串按条件拆分为指定长度的字符串数组
- 范例
public class YootkDemo{
public static void main(String arge[]){
String message="www.bai.qq.com www.Andy.com www.qq.com www.Andy.qq.com";
String[] resultA=message.split(" ");//按空格进行全部的拆分
for(String temp : resultA){
System.out.print("【"+temp+"】");
}
System.out.println();//换行
String[] resultB=message.split("\\.");//直接使用小数点无法进行拆分,若遇到无法拆分的情况,使用转义字符(\\)进行转义,就可以恢复正常
for(String temp : resultB){
System.out.print("【"+temp+"】");
}
System.out.println();//换行
String[] resultC=message.split(" ",3);//按空格进行全部的拆分为数组长度为3的字符串数组
for(String temp : resultC){
System.out.print("【"+temp+"】");
}
}
}
/*
【www.bai.qq.com】【www.Andy.com】【www.qq.com】【www.Andy.qq.com】
【www】【bai】【qq】【com www】【Andy】【com www】【qq】【com www】【Andy】【qq】【com】
【www.bai.qq.com】【www.Andy.com】【www.qq.com www.Andy.qq.com】
*/
字符串的截取
public String substring(int begIndex)
//从指定位置开始截取到最后
public String substring(int begIndex,int endIndex)
//截取指定范围的字符串
- 范例
public class YootkDemo{
public static void main(String arge[]){
String message="www.bai.qq.com";
System.out.println(message.substring(message.indexOf("b")));//通过indexOf()的方法,获取要截取的索引值,截取到字符串的内容
System.out.println(message.substring(message.indexOf("b"),message.indexOf("i")+1));//通过indexOf()的方法,获取要截取的索引值,截取到字符串的内容
}
}
/*
bai.qq.com
bai
*/
字符串格式化
- 常用的格式化符号为:字符串(%s) 整数(%d) 浮点数(%f) 字符(%c)
public static String format (String format,Object... object)
//使用可变参数的特性,格式化结果拼凑
- 范例
public class YootkDemo{
public static void main(String arge[]){
String name="Andy鑫";
int age = 18;
double score=99.9556;
char grade='优';
//使用正常的连接字符进行输出
System.out.println("姓名为:"+name+"、年龄为:"+age+"、分数为:"+score+"、评级为:"+grade);
//使用format()方法进行格式化拼接,%5.2f 为保留两位小数点,且进行四舍五入
System.out.println(String.format("姓名为:%s、年龄为:%d、分数为:%5.2f、评级为:%c",name,age,score,grade));
}
}
/*
姓名为:Andy鑫、年龄为:18、分数为:99.9556、评级为:优
姓名为:Andy鑫、年龄为:18、分数为:99.96、评级为:优
*/
其他字符串方法
public boolean isBlank()
//判断字符串的内容是否为空,与字符串的长度无关 JDK11推出
public boolean isEmpty()
//判断字符串是否为空字符串,与字符串的长度有关
public int length()
//获取字符串的长度
public String strip()
//清除字符串前后两边的空格,但保留UINICODE的空白 JDK11推出
public String tirm()
//清除字符串两边所有的空格
public String toLowerCase()
//将所有的字母全部转为小写
public String toUpperCase()
//将所有的字母全部转为大写
- 范例 判断空字符
public class YootkDemo{
public static void main(String arge[]){
String messageA="";
String messageB = "\n\t";
//输出字符串的长度
System.out.println("【字符串的长度】"+messageA.length());
System.out.println("【字符串的长度】"+messageB.length());
//使用isBlank进行判断
System.out.println("【isBlank】"+messageA.isBlank());
System.out.println("【isBlank】"+messageB.isBlank());
//使用isEmpty进行判断
System.out.println("【isEmpty】"+messageA.isEmpty());
System.out.println("【isEmpty】"+messageB.isEmpty());
}
}
/*
【字符串的长度】0
【字符串的长度】2
【isBlank】true
【isBlank】true
【isEmpty】true
【isEmpty】false
*/
- 范例 清除空白字符
public class YootkDemo{
public static void main(String arge[]){
String message=" 百度一下: www.baidu.COM\u0000\u0000\u0000";
System.out.println("【"+message+"】");//原始的输出
System.out.println("【"+message.strip()+"】");//清除前后的空格,但保留UNICODE的空白字符,中间的空格不清除
System.out.println("【"+message.trim()+"】");//清除前后所有的空格,中间的空格不清除
}
}
/*
【 百度一下: www.baidu.COM 】
【百度一下: www.baidu.COM 】
【百度一下: www.baidu.COM】
*/
- 范例 转换大小写
public class YootkDemo{
public static void main(String arge[]){
String message="百度一下:www.baidu.COM";
System.out.println(message.toLowerCase());//全部字母转换为小写字母
System.out.println(message.toUpperCase());//全部字母转换为大写字母
}
}
/*
百度一下:www.baidu.com
百度一下:WWW.BAIDU.COM
*/