Java: String 的常用方法
文章目录
鸡汤:
● 把烦恼清零,把快乐装满,用积极的心态拥抱每一天,平凡的日子也能开出不平凡的花朵。
● 记住,再微小的光也是光,再平凡的人也有他们人生中的高光时刻。愿你内心山河壮阔,始终相信人间值得。
String 的常用方法
1.1 String对象的比较
1.1.1 == 比较是否引用同一个对象
注意:对于内置类型,== 比较的是变量中的值;对于引用类型 ==比较的是引用中的地址。
例子:
public static void main(String[] args) {
String s1 = "abc";
String s2 = new String("abc");
String s3 = new String("def");
String s4 = s1;
System.out.println(s1 == s2);
System.out.println(s2 == s3);
System.out.println(s1 == s4);
}
结果:
1.1.2 equals 方法:按照字典序比较
字典序:字符大小的顺序
String类重写了父类Object中equals方法,Object中equals默认按照==比较,String重写equals方法后,按照如下规则进行比较,比如: s1.equals(s2)
例子:
public static void main(String[] args) {
String s1 = new String("hello");
String s2 = new String("hello");
String s3 = new String("hallo");
System.out.println(s1.equals(s2));
System.out.println(s1.equals(s3));
}
结果:
1.1.3 compareTo 方法: 按照字典序进行比较
与 equals 不同的是,equals 返回的是 boolean 类型,而 compareTo 返回的是 int 类型。具体比较方式:
1.先按照字典次序大小比较,如果出现不等的字符,直接返回这两个字符的大小差值
2.如果前k个字符相等(k为两个字符长度最小值),返回值两个字符串长度差值
例子:
public static void main(String[] args) {
String s1 = new String("abc");
String s2 = new String("ac");
String s3 = new String("abc");
String s4 = new String("abcdef");
System.out.println(s1.compareTo(s2));
System.out.println(s1.compareTo(s3));
System.out.println(s1.compareTo(s4));
}
结果:
1.1.4 compareToIgnoreCase 方法:与compareTo 方式相同,但是忽略大小写比较
例子:
public static void main(String[] args) {
String s1 = new String("abc");
String s2 = new String("ac");
String s3 = new String("ABc");
String s4 = new String("abcdef");
System.out.println(s1.compareToIgnoreCase(s2));
System.out.println(s1.compareToIgnoreCase(s3));
System.out.println(s1.compareToIgnoreCase(s4));
}
结果:与上面的一致
1.2 字符串查找

例子:
public static void main(String[] args) {
String s = "aaabbbcccaaabbbccc";
System.out.println(s.charAt(3)); // 'b'
System.out.println(s.indexOf('c')); // 6
System.out.println(s.indexOf('c', 10)); // 15
System.out.println(s.indexOf("bbb")); // 3
System.out.println(s.indexOf("bbb", 10)); // 12
System.out.println(s.lastIndexOf('c')); // 17
System.out.println(s.lastIndexOf('c', 10)); // 8
System.out.println(s.lastIndexOf("bbb")); // 12
System.out.println(s.lastIndexOf("bbb", 10)); // 3
}
1.3 转换
1.3.1 数值和字符串转化
例子:
public static void main(String[] args) {
// 数字转字符串
String s1 = String.valueOf(1234);
String s2 = String.valueOf(12.34);
String s3 = String.valueOf(true);
String s4 = String.valueOf(new Student("zhangsan", 18));
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
System.out.println(s4);
// 字符串转数字
int data1 = Integer.parseInt("1234");
double data2 = Double.parseDouble("12.34");
System.out.println(data1);
System.out.println(data2);
}
1.3.2 大小写转换
例子:
public static void main(String[] args) {
String s1 = "hello";
String s2 = "HELLO";
// ⼩写转⼤写
System.out.println(s1.toUpperCase());
// ⼤写转⼩写
System.out.println(s2.toLowerCase());
}
1.3.3 字符串转数组
例子:
public static void main(String[] args) {
String s = "hello";
// 字符串转数组
char[] ch = s.toCharArray();
for (int i = 0; i < ch.length; i++) {
System.out.print(ch[i]);
}
// 数组转字符串
String s2 = new String(ch);
System.out.println(s2);
}
1.3.4 格式化
例子:
public static void main(String[] args) {
String s = String.format("%d-%d-%d", 2019, 9,14);
System.out.println(s);
}
1.4 字符串替换
使用一个指定的新的字符串替换掉已有的字符串数据,可用的方法如下:

例子:
String str = "helloworld" ;
System.out.println(str.replaceAll("l", "_"));
System.out.println(str.replaceFirst("l", "_"));
注:
由于字符串是不可变对象,替换不修改当前字符串,而是产生一个新字符串
1.5 字符串拆分
可以将一个完整的字符串按照指定的分隔符划分为若干个子字符串。

例子:
String str = "hello world hello bit" ;
String[] result = str.split(" ") ; // 按照空格拆分
for(String s: result) {
System.out.println(s);
}
注:
字符需要转义
1.6 字符串截取
从一个完整的字符串之中截取出部分内容。
例子:
String str = "helloworld" ;
System.out.println(str.substring(5));
System.out.println(str.substring(0, 5));
注:
1.索引从0开始。
2.区间左闭右开, substring(0, 5) 表示包含 0 号下标的字符, 不包含 5 号下标。
1.7 去除左右两边的空格

例子:
String str = " hello world " ;
System.out.println("["+str+"]");
System.out.println("["+str.trim()+"]");
1.8 intern 方法
● 当调用 intern() 方法时,如果字符串常量池中已经包含一个等于此 String 对象的字符串(由 equals(Object)方法确定),则返回常量池中的字符串。
● 否则,将此String对象添加到常量池中,并返回此 String 对象的引用。
END
很多方法我也没用过
写的第二十篇,感谢大家的观看!
更多推荐




所有评论(0)