字符串常见操作
1 、字符串比较
代码示例:
1、区分大小写的比较
boolean ret=s.equals(s1);
public static void main(String[] args) {
String s="abcd";
System.out.println(s.equals("abcd"));//true
System.out.println(s.equals("Abcd"));//false
}
2、不区分大小写的比较
boolean ret=s.equalsIgnoreCase(s1);
public static void main(String[] args) {
String s="abcd";
System.out.println(s.equalsIgnoreCase("abcd"));//true
System.out.println(s.equalsIgnoreCase("Abcd"));//true
}
3、比较两个字符串的大小关系
int ret=s.compareTo(s1);
- 相等:返回0
- 大于:返回大于0的数
- 小于:返回小于0的数
public static void main(String[] args) {
String s="abcd";
System.out.println(s.compareTo("abcd"));//0
System.out.println(s.compareTo("abcde"));//-1
System.out.println(s.compareTo("abc"));//1
}
2 、字符串查找
1、判断一个子字符串是否存在
boolean ret=s.contains(s1);
public static void main(String[] args) {
String s="hello world" ;
//判断字符串s中是否存在字符串“hello”或“Hello”
System.out.println(s.contains("hello"));//true
System.out.println(s.contains("Hello"));//false
}
2、从头开始查找指定字符串的位置
int ret = s.indexOf(str);
- 返回值: int
- 找到返回下标,找不到返回-1
- indexOf 相当于找指定字符串在源字符串中首次出现的位置
String s = "abc,abc";
//在字符串s中从头查找字符串abc第一次出现的位置
System.out.println(s.indexOf("abc"));//0
System.out.println(s.indexOf("abcd"));//-1
3、从指定位置查找指定字符串的位置
int ret=s.indexOf(str,fromIndex);
- 返回值: int
- str : 指定字符串;from:查找的起始位置
- 找到返回下标,找不到返回-1
public static void main(String[] args) {
String s="hello world" ;
//在字符串s中,分别从 0和4 号下标起,字符串“llo”的起始位置
System.out.println(s.indexOf("llo",0));//2
System.out.println(s.indexOf("llo",4));//-1
}
4、由后向前查找子字符串的位置
int ret=s.lastIndexOf(str);
- 返回值: int
- 找到返回下标,找不到返回-1
- lastIndexOf 相当于找指定字符串在源字符串中最后一次出现的位置
String s = "abc,abc";
//在字符串s中从后向前查找字符串abc第一次出现的位置
System.out.println(s.lastIndexOf("abc"));//2
System.out.println(s.lastIndexOf("abcd"));//-1
5、从指定位置起由后向前查找子字符串的位置
int ret=s.lastIndexOf(str,fromIndex);
- 返回值: int
- 找到返回下标,找不到返回-1
- str: 要查找的字符串; fromIndex:查找的起始位置
String s = "abc,abc";
//在字符串s中,从位置0与6起由后向前查找字符串abc第一次出现的位置
System.out.println(s.lastIndexOf("abc",0));//0
System.out.println(s.lastIndexOf("abc",6));//4
6、判断是否以指定字符串开头
boolean ret=s.startsWith(str);
String s = "abc,abc";
//判断字符串s是否以ab开头
System.out.println(s.startsWith("ab"));//true
System.out.println(s.startsWith("dw"));//false
7、从指定位置起判断是否以指定字符串开头
boolean ret=s.startsWith(str,toffset);
- toffset:指定位置
- 返回值:boolean类型
tring s = "abc,abc";
//分别从位置0,3起,判断字符串s是否以ab开头
System.out.println(s.startsWith("ab",0));//true
System.out.println(s.startsWith("ab",3));//false
8、判断字符串是否以指定字符串结尾
String s = "abc,abc";
System.out.println(s.endsWith("abc"));//true
System.out.println(s.endsWith("ab"));//false
3、字符串替换
注意事项: 由于字符串是不可变对象, 替换不修改当前字符串, 而是产生一个新的字符串。
- replace与replaceAll实现的功能一样
String str = "helloworld" ;
System.out.println(str.replaceAll("l", "_"));//he__owor_d
System.out.println(str.replaceFirst("l", "_"));//he_loworld
4、字符串拆分
1、全部拆分
String[] ret=s.split("\\+");
public static void main(String[] args) {
String s = "1+2+3+4+5" ;
String[] ret=s.split("\\+");
System.out.println(Arrays.toString(ret));
}//[1, 2, 3, 4, 5]
2、部分拆分
String[] ret1=s.split("\\+",3);
public static void main(String[] args) {
String s = "1+2+3+4+5" ;
String[] ret1=s.split("\\+",3);//[1, 2, 3+4+5]
String[] ret2=s.split("\\+",8);//[1, 2, 3, 4, 5]最多只能拆出五个
System.out.println(Arrays.toString(ret1));
System.out.println(Arrays.toString(ret2));
}
代码示例: 多次拆分
取得用户名和密码:
public class TestString {
public static void main(String[] args) {
String s = "username=August&passward=123" ;
String[] ret1=s.split("&");
for (int i=0;i<ret1.length;i++) {
String[] ret2 = ret1[i].split("=");
for(int j=0;j<ret2.length;j++){
if(j%2!=0) {
System.out.println(ret2[j]);
}
}
}
}
}
//August
123
注意事项:
1. 字符"|","*","+"都得加上转义字符,前面加上"\";
2. 而如果是"",那么就得写成"\\";
3. 如果一个字符串中有多个分隔符,可以用"|"作为连字符。
public static void main(String[] args) {
String str = "name=zhangsan&age=18" ;
String[] ret=str.split("=|&");
System.out.println(Arrays.toString(ret));
}//[name, zhangsan, age, 18]
5、
字符串截取
String ret1=s.substring(2); //从2号下标起全部截取
String ret2=s.substring(2,4); //从2号下标起,到4下标部分截取,包含2 下标,不包含4 下标。
public static void main(String[] args) {
String s="abcdef";
String ret1=s.substring(2);
System.out.println(ret1);//cdef
String ret2=s.substring(2,4);
System.out.println(ret2);//cd
}
注:
1. 索引从0开始
2. 注意前闭后开区间的写法, substring(0, 5) 表示包含 0 号下标的字符, 不包含 5 号下标
6、其他
1、去除字符串左右两边的空格,保留中间空格。
public static void main(String[] args) {
String s=" a b ";
System.out.println(s);
System.out.println(s.trim());
}
//
a b
a b
2、大小写转换
public static void main(String[] args) {
String str = "AbCd" ;
System.out.println(str.toUpperCase());//ABCD
System.out.println(str.toLowerCase());//abcd
}
代码示例: 首字母大写
public static void main(String[] args) {
System.out.println(fistUpper("yuisama"));
System.out.println(fistUpper(""));
System.out.println(fistUpper("a"));
}
public static String fistUpper(String str) {
if ("".equals(str)||str==null) {
return str ;
}
if (str.length()>1) {
return str.substring(0, 1).toUpperCase()+str.substring(1) ;
}
return str.toUpperCase() ;
}