文章目录
1 字符与字符串的相互转换1.1 将字符数组转为字符串1.2 将字符串转为字符数组1.3 将字符串转为单个字符1.4 判断一个字符串是否由数字组成?
2 字节与字符串的相互转换2.1 将字节数组转为字符串2.2 将字符串转为字节数组
3 字符串比较3.1 不区分大小相等比较3.2 比较两个字符串的大小
4 字符串查找4.1 判断字符串在源字符串中是否存在4.2 判断字符串是否以指定的字符串开始4.3 从指定位置开始判断是否以指定的字符串开头4.4 判断是否以指定的字符串结尾4.5 查找字符串的位置
5 字符串替换5.1 替换全部内容5.2 替换首个内容
6 字符串拆分6.1 将字符串全部拆分6.2 将字符串部分拆分6.3 一个拆分的实例
7 字符串截取7.1 从指定位置开始截取到文件末尾7.1 从指定位置开始截取部分内容
8 字符串的其他操作8.1 去掉左右空格,保留中间空格8.2 转大小写8.3 判断是否为空字符串8.4 首字母大写
1 字符与字符串的相互转换
字符串就是一个字符数组,所以在String类里面支持有字符数组转为字符串以及字符串转为字符的操作方法。
//取得字符串长度:public int length();
//数组的长度:数组名.length;
1.1 将字符数组转为字符串
用String类的构造方法!
/**
1. public String(char [] value);将字符数组value中的所有内容变为字符串
2. public String(char [] value,int offset,int count);将字符数组value中的部分内容变为字符串
offset为开始索引、count为个数
均为成员方法,通过对象调用!!!
*/
public class Test{
public static void main(String[] args)
{
//静态初始化一个字符串数组
char [] arr = new char[]{'h','e','l','l','o','&','1','3','1','4'};
//将字符数组中的所有内容变为字符串
String str1=new String(arr);
System.out.println(str1);
//从下表为5的索引开始,将之后的5个字符变为字符串
String str2=new String(arr,5,5);
System.out.println(str2);
//如果,字符的个数超出了数组的范围,运行时会有数组越界异常(StringIndexOutOfBoundsException)
String str3=new String(arr,5,10);
System.out.println(str3);
}
}
1.2 将字符串转为字符数组
/**
将字符串该为字符数组
1. public char[] tocharArray();
*/
public class Test{
public static void main(String[] args)
{
String str="hello";
char [] result=str.toCharArray();
//for-each循环
for(char i:result)
{
System.out.print(i+" ");
}
}
}
1.3 将字符串转为单个字符
/**
1. public char charAt(int index);取得指定索引位置上的字符
index:索引
*/
public class Test{
public static void main(String[] args)
{
//取得索引为0的位置的字符
System.out.println("hello".charAt(0));
//如果索引越界,会报数组越界异常StringIndexOutOfBoundsException
System.out.println("hello".charAt(6));
}
}
1.4 判断一个字符串是否由数字组成?
/**
判断一个字符串是否由数组组成
取得字符串长度:public int length();
取得数组的长度:数组名.length
*/
public class Test{
public static boolean isNumber1(String str)
{
for(int i=0;i
{
//将字符串根据索引转成单个字符
char result=str.charAt(i);
if(result'9')
{
return false;
}
}
return true;
}
public static boolean isNumber2(String str)
{
//将字符串转为字符数组
char [] data=str.toCharArray();
for(int i=0;i
{
if(data[i]'9')
{
return false;
}
}
return true;
}
public static void main(String[] args)
{
String str="14263";
System.out.println(isNumber1(str));
System.out.println(isNumber2(str));
}
}
2 字节与字符串的相互转换
2.1 将字节数组转为字符串
用String类的构造方法!
/**
1. public String(byte[] value);将字节数组中的所有内容转为字符串
2. public String(byte[] value,int offset,int count);将字节数组中的部分内容转为字符串
offset为开始索引,count为个数
*/
public class TestByte{
public static void main(String[] args)
{
byte [] data=new byte[]{1,2,3,4,5};
//将字节数组中的所有内容变为字符串
String str1=new String(data);
System.out.println(str1);
//将字节数组中的部分内容变为字符串offset为开始点,count为个数
String str2=new String(data,2,3);
System.out.println(str2);
//如果字符的个数超出了数组的索引,会报数组越界异常
String str3=new String(data,2,4);
System.out.println(str3);
}
}
2.2 将字符串转为字节数组
/**
//将字符串改为字节数组
1. public byte[] getBytes();
*/
public class TestByte{
public static void main(String[] args)
{
byte [] result="HELLO".getBytes();
for(byte i:result)
{
System.out.print(i+" ");
}
}
}
字节不适合中文,只有字符适合处理中文!
3 字符串比较
3.1 不区分大小相等比较
/**
不区分大小写的相等比较
public boolean equalsIgnoreCase(String anotherString)
区分大小写
public boolean equals(String anotherString)
*/
public class TestByte{
public static void main(String[] args)
{
String str1="Hello";
//不区分大小写
System.out.println("hello".equalsIgnoreCase(str1));
//区分大小写
System.out.println("hello".equals(str1));
}
}
3.2 比较两个字符串的大小
/**
比较两个字符串的大小
public int compareTo(String anotherString)
返回值:(返回值正好是两个字符的差)
① >0 表示该字符串大于比较对象
② <0 表示该字符串小于比较对象
③ =0 表示该字符串等于比较对象
*/
public class TestByte{
public static void main(String[] args)
{
System.out.println("ABCD".compareTo("ABCD"));
System.out.println("abcd".compareTo("aBcd"));
System.out.println("abCd".compareTo("abcD"));
System.out.println("AB".compareTo("AC"));
}
}
4 字符串查找
4.1 判断字符串在源字符串中是否存在
/**
判断字符串str在源字符串中是否存在
public boolean contains(String str);
*/
public class TestByte{
public static void main(String[] args)
{
String str="helloworld";
System.out.println(str.contains("hello"));
System.out.println(str.contains("he"));
System.out.println(str.contains("hep"));
}
}
4.2 判断字符串是否以指定的字符串开始
/**
判断字符串是否以指定的字符串开始
public boolean startsWith(String str);
*/
public class TestByte{
public static void main(String[] args)
{
String str="helloworld";
System.out.println(str.startsWith("hello"));
System.out.println(str.startsWith("he"));
System.out.println(str.startsWith("hep"));
}
}
4.3 从指定位置开始判断是否以指定的字符串开头
/**
从指定位置开始判断是否以指定的字符串开头
public boolean startsWith(String str,intdex);
*/
public class TestByte{
public static void main(String[] args)
{
String str="helloworld";
System.out.println(str.startsWith("hello",0));
System.out.println(str.startsWith("wo",5));
System.out.println(str.startsWith("hep",3));
}
}
4.4 判断是否以指定的字符串结尾
/**
判断是否以指定的字符串结尾
public boolean endsWith(String str);
*/
public class TestByte{
public static void main(String[] args)
{
String str="helloworld";
System.out.println(str.endsWith("hello"));
System.out.println(str.endsWith("wo"));
System.out.println(str.endsWith("world"));
System.out.println(str.endsWith("ld"));
}
}
4.5 查找字符串的位置
public int indexOf (String str)从头开始查找指定字符串的位置,查到了返回位置的开始索引,查不到返回-1public int indexOf (String str,int fromIndex)从指定位置开始查找指定字符串的位置public int lastIndexOf(String str)从最后一个元素开始由后向前查找字符串的位置public int lastIndexOf(String str,int fromIndex)从指定位置由后向前查找字符串的位置
public class TestByte{
public static void main(String[] args)
{
String str="helloworld";
//从头开始查找字符串hlo的位置 没有找到返回-1
System.out.println(str.indexOf("hlo"));
//从头开始查找字符串llo的位置 返回2
System.out.println(str.indexOf("llo"));
//从下表为4的位置开始,向后找字符串h的位置 没有找到返回-1
System.out.println(str.indexOf("h",4));
//从下表为4的位置开始,向后找字符串w的位置 返回5
System.out.println(str.indexOf("w",4));
//从最后一个元素开始由后向前查找字符串world的位置,返回5
System.out.println(str.lastIndexOf("world"));
//从第4个元素开始由后向前查找字符串world的位置,没有找到返回-1
System.out.println(str.lastIndexOf("world",4));
//从第6个元素开始由后向前查找字符串world的位置,找到返回5
System.out.println(str.lastIndexOf("world",6));
//从第6个元素开始由后向前查找字符串l的位置,返回3
System.out.println(str.lastIndexOf("l",6));
}
}
5 字符串替换
5.1 替换全部内容
/**
替换全部内容
public String replaceAll(String regex,String replacement)
*/
public class TestReplace{
public static void main(String[] args)
{
String str="hello";
String result = str.replaceAll("l","*");
System.out.println(result);
}
}
5.2 替换首个内容
/**
替换首个内容
public String replaceFirst(String regex,String replacement)
*/
public class TestReplace{
public static void main(String[] args)
{
String str="hello";
String result = str.replaceFirst("l","*");
System.out.println(result);
}
}
6 字符串拆分
6.1 将字符串全部拆分
/**
将字符串按照指定格式全部拆分
public String[] split(String regex);
*/
public class TestSplit{
public static void main(String[] args)
{
String str="192 168 106 1";
String [] result = str.split(" ");
for(String i:result)
{
System.out.println(i);
}
}
}
/**
将字符串部分拆分
public String[] split(String regex,int limit);
拆分后数组的长度为limit
*/
public class TestSplit{
public static void main(String[] args)
{
String str="192.168.106.1";
//转义字符
String [] result = str.split("\\.");
for(String i:result)
{
System.out.println(i);
}
}
}
6.2 将字符串部分拆分
/**
将字符串部分拆分
public String[] split(String regex,int limit);
拆分后数组的长度为limit
*/
public class TestSplit{
public static void main(String[] args)
{
String str="192 168 106 1";
String [] result = str.split(" ",3);
for(String i:result)
{
System.out.println(i);
}
}
}
6.3 一个拆分的实例
public class TestSplit{
public static void main(String[] args)
{
String str="苗:20|萌:22|昕:10";
//需要转义字符
String [] result = str.split("\\|");
for(int i=0;i
{
String [] data = result[i].split(":");
System.out.println(data[0]+" "+data[1]);
}
}
}
7 字符串截取
7.1 从指定位置开始截取到文件末尾
/**
字符串截取
public String substring (int beginIndex)从指定位置截取到文件末尾
*/
public class TestSubString{
public static void main(String[] args)
{
String str="helloworld";
System.out.print(str.substring(3));
}
}
7.1 从指定位置开始截取部分内容
/**
字符串截取
public String substring (int beginIndex,int endIndex) 从指定位置开始截取部分内容(左闭右开)
*/
public class TestSubString{
public static void main(String[] args)
{
String str="helloworld";
System.out.print(str.substring(3,7));
}
}
8 字符串的其他操作
8.1 去掉左右空格,保留中间空格
/**
去掉左右空格,保留中间空格:public String trim();
*/
public class Test2{
public static void main(String[] args)
{
//去掉左右空格保留中间空格
String str=" ha ha ";
System.out.println(str.trim());
}
}
8.2 转大小写
/**
转大写:public String toUpperCase();
转小写:public String toLowerCase();
*/
public class Test2{
public static void main(String[] args)
{
String str1="abc";
String str2="AB";
System.out.println(str1.toUpperCase());
System.out.println(str2.toLowerCase());
}
}
8.3 判断是否为空字符串
/**
判断是否为空字符串:public boolean isEmpty();
*/
public class Test2{
public static void main(String[] args)
{
//只能判断字符串是否是空字符串,而不是null
String str1="";
System.out.println(str1.isEmpty());
}
}
8.4 首字母大写
public class Test2{
public static void main(String[] args)
{
System.out.println(firstUpper("abcADDFAdie"));
System.out.println(firstUpper("*bcADDFAdie"));
System.out.println(firstUpper("苗bcADDFAdie"));
}
public static String firstUpper(String str)
{
//判断字符串为空
if(str.isEmpty()||str==null)
{
return str;
}
return str.substring(0,1).toUpperCase()+str.substring(1);
}
}