目录
9 、大小写转换 toUpperCase() toLowerCase()
10 、判断是否以指定的字符串开头或结尾 startsWith() endsWith()
11 、不区分大小写的比较 equalsIgnoreCase()
1 、字符串与字符数组的转换 toCharArray()
public class StringAPIDemo01{
public static void main(String args[]){
String str1 = "hello" ; // 定义字符串
char c[] = str1.toCharArray() ; // 将一个字符串变为字符数组
for(int i=0;i<c.length;i++){ // 循环输出
System.out.print(c[i] + "、") ;
}
System.out.println("") ; // 换行
String str2 = new String(c) ; // 将全部的字符数组变为String
String str3 = new String(c,0,3) ; // 从第0个开始,把前3个字符数组变为String
System.out.println(str2) ; // 输出字符串
System.out.println(str3) ; // 输出字符串
}
};
程序运行结果:
2 、从字符串中取出指定位置的字符 charAt()
public class StringAPIDemo02{
public static void main(String args[]){
String str1 = "hello" ; // 定义String对象
System.out.println(str1.charAt(3)) ; // 取出字符串中第四个字符
}
};
程序运行结果:
3 、字符串与 byte 数组的转换 getBytes()
public class StringAPIDemo03{
public static void main(String args[]){
String str1 = "hello" ; // 定义字符串
byte b[] = str1.getBytes() ; // 将字符串变为byte数组
System.out.println(new String(b)) ; // 将全部的byte数组变为字符串
System.out.println(new String(b,1,3)) ; // 将部分的byte数组变为字符串
}
};
程序运行结果:
4、取得一个字符串的长度 length()
public class StringAPIDemo04 {
public static void main(String[] args) {
String str = "Hello World";
System.out.println("\"" + str + "\"的长度是:" + str.length());
}
}
程序运行结果:
5 、查找指定的字符串是否存在 indexOf()
public class StringAPIDemo05{
public static void main(String args[]){
String str1 = "abcdefgcgh" ; // 声明字符串
System.out.println(str1.indexOf("c")) ; // 查到返回位置
System.out.println(str1.indexOf("c",3)) ; // 查到返回位置,从第4个位置开始查找
System.out.println(str1.indexOf("x")) ; // 没有查到返回-1
}
};
程序运行结果:
6 、去掉空格 trim()
public class StringAPIDemo06{
public static void main(String args[]){
String str1 = " hello " ; // 定义字符串
System.out.println(str1.trim()) ; // 去掉左右空格后输出
}
};
7 、字符截取 substring()
public class StringAPIDemo07{
public static void main(String args[]){
String str1 = "hello world" ; // 定义字符串
System.out.println(str1.substring(6)) ; // 从第7个位置开始截取,包括第7个
System.out.println(str1.substring(0,5)) ; // 从第0个开始,截取5个
}
};
程序运行结果:
8 、拆分字符串 split()
public class StringAPIDemo08{
public static void main(String args[]){
String str1 = "hello world" ; // 定义字符串
String s[] = str1.split(" ") ; // 按空格进行字符串的拆分
for(int i=0;i<s.length;i++){ // 循环输出
System.out.println(s[i]) ;
}
}
};
程序运行结果:
9 、大小写转换 toUpperCase() toLowerCase()
public class StringAPIDemo09{
public static void main(String args[]){
System.out.println("将\"hello world\"转成大写:" + "hello world".toUpperCase()) ;
System.out.println("将\"HELLO WORLD\"转成小写:" + "HELLO WORLD".toLowerCase()) ;
}
};
程序运行结果:
10 、判断是否以指定的字符串开头或结尾 startsWith() endsWith()
public class StringAPIDemo10{
public static void main(String args[]){
String str1 = "**HELLO" ; // 定义字符串
String str2 = "HELLO**" ; // 定义字符串
if(str1.startsWith("**")){ // 判断是否以“**”开头
System.out.println("(**HELLO)以**开头") ;
}
if(str2.endsWith("**")){ // 判断是否以“**”结尾
System.out.println("(HELLO**)以**结尾") ;
}
}
};
程序运行结果:
11 、不区分大小写的比较 equalsIgnoreCase()
(此方法适用于大写字符串和小写字符串内容完全一样,只是大小写不同的情况下 )
public class StringAPIDemo11{
public static void main(String args[]){
String str1 = "HELLO" ; // 定义字符串
String str2 = "hello" ; // 定义字符串
System.out.println("\"HELLO\" equals \"hello\" " + str1.equals(str2)) ;
System.out.println("\"HELLO\" equalsIgnoreCase \"hello\" "
+ str1.equalsIgnoreCase(str2)) ; // 不区分大小写的比较
}
};
程序运行结果:
12 、替换字符串 replaceAll()
public class StringAPIDemo12{
public static void main(String args[]){
String str = "hello" ; // 定义字符串
String newStr = str.replaceAll("l","x") ; // 现在将所有的l替换成x
System.out.println("替换之后的结果:" + newStr) ;
}
};
程序运行结果: