常用方法】"/>
【String类的常用方法】
文章目录
- 字符串构造
- String对象的比较
- 字符串查找
- charAt
- indexof
- 转化
- 1. 数值和字符串转化
- 2.大小写转换 toUpperCase toLowerCase
- 3.字符串转数组 toCharArray
- 4.数组转字符串
- 5.格式化 format
- 字符串替换
- 替换所有的指定内容
- 替换首个内容
- 字符串拆分
- 以空格拆分
- 特殊字符拆分
- 多个分隔符拆分
- 字符串的截取
- 截取部分内容
- 从指定索引截取到结尾
- 其他操作
- 去掉字符串中的左右空格,保留中间空格
字符串构造
public class Test {public static void main(String[] args) {String str = "hello";System.out.println(str);String str2 = new String("hehehe");System.out.println(str2);char[] array = {'a','b','c'};String str3 = new String(array);System.out.println(str3);}
}
public class Test {public static void main(String[] args) {String s1 = new String("hello");String s2 = new String("world");String s3 = s1;System.out.println(s3);String s4 = "";//存储0System.out.println(s4.length());System.out.println(s4.isEmpty());String s5 = null;//不存任何数System.out.println(s5.length());System.out.println(s5.isEmpty());}
- String是引用类型,内部并不存储字符串本身,
String对象的比较
1.==
2. equals
3pareTo
4pareToIgnoreCase
public class Test {public static void main(String[] args) {String s1 ="Student";String s2 ="Student";System.out.println(s1==s2);//比较值String s3 = new String("JAVA");String s4 = new String("JAVA");System.out.println(s3 == s4);//比较地址一样不一样System.out.println(s3.equals(s4));//比较值String s5 ="abc";String s6 ="adc";//s5>s6 返回正数//s5==s6 返回0//s5<s6 返回负数System.out.println(s5pareTo(s6));//忽略大小写比较String s7 ="abc";String s8 ="ABC";System.out.println(s7pareToIgnoreCase(s8));}public static void main3(String[] args) {String s1 = new String("hello");String s2 = new String("world");String s3 = s1;// System.out.println(s3);String s4 = "";//存储0System.out.println(s4.length());System.out.println(s4.isEmpty());String s5 = null;//不存任何数// System.out.println(s5.length());System.out.println(s5.isEmpty());}public static void main2(String[] args) {String str = "hello";System.out.println(str);String str2 = new String("hehehe");System.out.println(str2);char[] array = {'a','b','c'};String str3 = new String(array);System.out.println(str3);}}
}
字符串查找
charAt
找到字符串某个字符的下标的元素
public class Test {public static void main(String[] args) {//charAtString s1 ="English";char ch = s1.charAt(3);//3下标的值System.out.println(ch);//遍历字符串for (int i = 0; i <s1.length() ; i++) {char sh = s1.charAt(i);//3下标的值System.out.println(sh);}}
}
indexof
int indexOf(int ch)
在字符串中 第一次出现某个字符的下标,没有返回-1
public class Test {public static void main(String[] args) {String str = "abacabc";int index = str.indexOf('a');//字符串中第一次出现字符a的下标System.out.println(index);}
}
int indexOf(int ch, int fromIndex)
从fromIndex位置开始找字符第一次出现的位置 没有返回-1
public class Test {public static void main(String[] args) {String str = "abacabc";int index = str.indexOf('a',3);System.out.println(index);}
}
int indexOf(String str)
返回字符串第一次出现的位置,没有返回-1
public class Test {public static void main(String[] args) {String str = "abacabc";int index = str.indexOf("abc");System.out.println(index);//4}
}
int indexOf(String str, int fromIndex)
从fromIndex位置开始找字符串第一次出现的位置,没有返回-1
public class Test {public static void main(String[] args) {String str = "acabacb";int index = str.indexOf("ab",2);System.out.println(index);}
}
int lastIndexOf(int ch)
从后往前找,返回字符第一次出现的位置,没有返回-1
public class Test {public static void main(String[] args) {String str = "acabacb";int index = str.lastIndexOf('b');System.out.println(index);//6}
}
int lastIndexOf(int ch, int fromIndex)
从fromIndex位置开始找,从后往前找字符第一次出现的位置,没有返
回-1
public class Test {public static void main(String[] args) {String str = "acabacb";int index = str.lastIndexOf('b',5);System.out.println(index);//3}
}
int lastIndexOf(String str)
从后往前找,返回字符串第一次出现的位置,没有返回-1
public class Test {public static void main(String[] args) {String str = "acabacb";int index = str.lastIndexOf("ac");System.out.println(index);//4}
}
int lastIndexOf(String str, int fromIndex)
从fromIndex位置开始找,从后往前找字符串第一次出现的位置,没有返
回-1
public class Test {public static void main(String[] args) {String str = "acabacb";int index = str.lastIndexOf("ac",3);System.out.println(index);//0}
}
转化
1. 数值和字符串转化
数字转字符串
public class Test {public static void main(String[] args) {//数字转字符串String s1 = String.valueOf(1919);//整型String s2 = String.valueOf(19.9);//浮点型System.out.println(s1);//变成字符串了System.out.println(s2);}
字符串转数字
public class Test {public static void main(String[] args) {int date = Integer.parseInt("198");//整型转字符串double date2 = Double.parseDouble("15.55");//浮点型转字符串System.out.println(date);System.out.println(date2);}
}
2.大小写转换 toUpperCase toLowerCase
public class Test {public static void main(String[] args) {String s1 = "hello";System.out.println(s1.toUpperCase());//HELLOString s2 = "WORLD";System.out.println(s2.toLowerCase());//world}
3.字符串转数组 toCharArray
public class Test {public static void main(String[] args) {String s1 = "hello";char[] array = s1.toCharArray();System.out.println(Arrays.toString(array));//Arrays.toString( )是打印数组的}
}
4.数组转字符串
public class Test {public static void main(String[] args) {char[] array ={'h','e','l','l','o'};String s1 = new String(array);System.out.println(s1);}
}
5.格式化 format
public class Test {public static void main(String[] args) {String s =String.format("%d-%d-%d",2023,11,06);System.out.println(s);//2023-11-06}
字符串替换
String replaceAll(String regex, String replacement)
替换所有的指定内容
public class Test {public static void main(String[] args) {String str = "abbaacca";System.out.println(str.replaceAll("b","q"));//aqqaaccaSystem.out.println(str);//abbaacca}
}
String replaceFirst(String regex, String replacement)
替换首个内容
public class Test {public static void main(String[] args) {String str = "abbaacca";String s1 = str.replaceFirst("b","88");System.out.println(s1);}
注意事项: 由于字符串是不可变对象, 替换不修改当前字符串, 而是产生一个新的字符串
字符串拆分
以空格拆分
public class Test {public static void main(String[] args) {String str = "hello beautiful world";String [] s1 = str.split(" ");for (int i = 0; i < s1.length; i++) {System.out.println(s1[i]);}}
以空格分开,分成两组
public class Test {public static void main(String[] args) {String str = "hello beautiful world";String [] s1 = str.split(" ",2);for (int i = 0; i < s1.length; i++) {System.out.println(s1[i]);}}
特殊字符拆分
特殊字符作为分割符可能无法正确切分, 需要加上转义 \ \
public class Test {
// 拆分IP地址public static void main(String[] args) {String str = "198.153.1.2";String [] s1 = str.split("\\.");for (int i = 0; i < s1.length; i++) {System.out.println(s1[i]);}}
多个分隔符拆分
如果一个字符串中有多个分隔符,可以用"|"作为连字符
public class Test {public static void main(String[] args) {String str2 = "wo=he=zhangsan&zai&xuexi ";String [] array =str2.split("=|&");for (int i = 0; i < array.length; i++) {System.out.println(array[i]);}}}
字符串的截取
从一个完整的字符串之中截取出部分内容。
String substring(int beginIndex, int endIndex)
截取部分内容
public class Test {public static void main(String[] args) {String str3 = "abcaac";String s4 = str3.substring(0,3);System.out.println(s4);//abc}
}
String substring(int beginIndex)
从指定索引截取到结尾
public class Test {public static void main(String[] args) {String str3 = "abcaac";String s4 = str3.substring(3);System.out.println(s4);//aac}
其他操作
String trim()
去掉字符串中的左右空格,保留中间空格
public class Test {public static void main(String[] args) {String str4 = " abc aac ";String s5 = str4.trim();System.out.println(s5);//abc aac }
更多推荐
【String类的常用方法】
发布评论