String类(下)
1.字符串查找
从一个完整的字符串之中可以判断指定内容是否存在,查找方法如下:
No. | 方法名称 | 类型 | 描述 |
---|---|---|---|
1. | public boolean contains(CharSequence s) | 普通 | 判断一个子字符串是否存在 |
2. | public int indexOf(String str) | 普通 | 从头开始查找指定字符串位置,查到了返回开始位置索引,如果查不到返回-1 |
3. | public int indexOf(String str,int fromIndex) | 普通 | 从指定位置开始查找指定字符串位置 |
4. | public int lastIndexOf(String str) | 普通 | 由后向前查找子字符串位置 |
5. | public int lastIndexOf(String str,int fromIndex) | 普通 | 从指定位置向后查找指定字符串出现位置 |
6. | public boolean startsWith(String prefix) | 普通 | 判断是否以指定字符串开头 |
7. | public boolean startsWith(String prefix),int toffset) | 普通 | 从指定位置开始判断是否以指定字符串开头 |
8. | public boolean endsWith(String suffix) | 普通 | 判断是否以指定字符串结尾 |
字符串查找最方便的是contains(),它是JDK1.5之后的新特性,在此前要实现必须借助indexOf()方法实现(常用)
String str = "helloworld";
System.out.println(str.contains("world"));//true
使用indexOf()方法进行位置
String str = "helloworld";
System.out.println(str.indexOf("world"));//true
System.out.println(str.indexOf("lili"));//-1
if(str.indexOf("hello") != -1){
System.out.println("可以查到指定字符串");//可以查到指定字符串
}
//注:使用indexOf()需要注意的是,如果出现内容重复它只返回查找到的第一个位置
String str = "helloworld";
System.out.println(str.indexOf("l"));//2
System.out.println(str.indexOf("l",5));//8
System.out.println(str.lastIndexOf("l"));//8
在查找字符串时往往会判断开头或结尾
String str = "**@@helloworld!!";
System.out.println(str.startsWith("**"));//true
System.out.println(str.startsWith("@@"));//false
System.out.println(str.startsWith("@@",2));//true
System.out.println(str.endsWith("!!"));//true
System.out.println(str.endsWith("d"));//false
2.字符串替换
使用一个新的字符串替换掉已有的字符串数据,可用的方法如下:
No. | 方法名称 | 类型 | 描述 |
---|---|---|---|
1. | public String replaceAll(String regex,String replacement) | 普通 | 替换所有指定内容 |
2. | public String replaceFirst(String regex,String replacement) | 普通 | 替换首个指定内容 |
字符串替换处理还与正则表达式有关,这里先不细讲。
//字符串的替换处理
String str = "helloworld";
System.out.println(str.replaceAll("l","-"));//he--owor-d
System.out.println(str.replaceFirst("l","-"));// he-loworld
3.字符串拆分
将一个完整的字符串按照指定的分隔符划分为若干个子字符串,方法如下:
No. | 方法名称 | 类型 | 描述 |
---|---|---|---|
1. | public String[] split(String regex) | 普通 | 将字符串全部拆分 |
2. | public String[] split(String regex,int limit) | 普通 | 将字符串部分拆分,该数组长度就是limit极限 |
实现字符串拆分处理
String str = "hello world hello bit";
String[] result = str.split(" ");//按照空格拆分
for(String s : result){
System.out.println(s);
}
//输出
//hello
//world
//hello
//bit
将字符串的部分拆分
String str = "hello world hello bit";
String[] result = str.split(" ",2);
for(String s : result){
System.out.println(s);
}
//输出:
//hello
//world hello bit
有些内容无法拆分就需要使用""转义
//eg:拆分IP地址
String str = "192.168.1.1";
String[] result = str.split("\\.");
for(String s : result){
System.out.println(s);
}
//输出
//191
//168
//1
//1
比较常用的是多次拆分
String str = "zoujier:18|zoudapao:81";
String[] result = str.split("\\|");
for(int i = 0; i < result.length; i ++){
String[] tmp = result[i].split(":");
System.out.println(temp[0] + "=" +temp[1]);
}
//输出
//zoujier=18
//zoudapao=81
4.字符串截取
从一个完整的字符串中取出部分内容
No. | 方法名称 | 类型 | 描述 |
---|---|---|---|
1. | public String substring(int beginIndex) | 普通 | 从指定索引截取到结尾 |
2. | public String substring(int beginIndex,int endIndex) | 普通 | 截取部分内容 |
观察字符串截取
String str = "helloworld";
System.out.println(str.substring(5));//world
System.out.println(str.substring(3,5));//lo
注:第二种截取方式从beginIndex索引开始截取,beginIndex对应的位置要截取上,到endIndex结束,endIndex对应的位置不截取。
5.字符串其他操作方法
No. | 方法名称 | 类型 | 描述 |
---|---|---|---|
1. | public String trim() | 普通 | 去掉字符串中的左右空格,保留之间空格 |
2. | public String toUpperCase() | 普通 | 字符串转大写 |
3. | public String toLowerCase() | 普通 | 字符串转小写 |
4. | public native String intern() | 普通 | 字符串入池操作 |
5. | public String concat(String str) | 普通 | 字符串拼接,等同于"+",不入池 |
6. | public int length() | 普通 | 取得字符串长度 |
7. | public boolean isEmpty() | 普通 | 判断是否为空字符串,但不是null,而是长度0 |
trim()方法
String str = " hello world";
System.out.println("[" + str + "]");//[ hello world]
System.out.println("[" + str.trim() + "]");//[hello world]
大小写转换(这两个方法只转换字母)
String str = " hello%$$&%%&*WORLD zoujierchibaba ";
System.out.println(str.toUpperCase());// HELLO%$$&%%&*WORLD ZOUJIERCHIBABA
System.out.println(str.toLowerCase());// hello%$$&%%&*world zoujierchibaba
数组长度使用数组名称.length是属性,而String中的length()是方法。
String str = "hello" ;
System.out.println(str.length());//5
isEmpty()方法
System.out.println("hello".isEmpty());//false
System.out.println("".isEmpty());//true
System.out.println(new String().isEmpty());//true
6.首字母大写操作
String类中没有提供首字母大写操作,需要自己实现
public static void main(String[] args) {
//首字母大写
System.out.println(fistUpper("zoujier"));//Zoujier
System.out.println(fistUpper(""));//
System.out.println(fistUpper("z"));//Z
}
public static String fistUpper(String str) {
//字符串为null或者长度为0时
if ("".equals(str)||str==null) {
return str ;
}
//字符串长度大于1时
if (str.length()>1) {
return str.substring(0, 1).toUpperCase()+str.substring(1) ;//取出第一个字符转大写再拼接第一个字母之后的内容
}
//字符串只要一个字符时
return str.toUpperCase() ;
}