赞
踩
乐观学习,乐观生活,才能不断前进啊!!!
我的主页:optimistic_chen
我的专栏:c语言 ,Java
欢迎大家访问~
创作不易,大佬们点赞鼓励下吧~
在C语言中已经涉及到字符串了,但是在C语言中要表示字符串只能使用字符数组或者字符指针,可以使用标准库提供的字符串系列函数完成大部分操作,但是这种将数据和操作数据方法分离开的方式不符合面相对象的思想,而字符串应用又非常广泛,因此Java语言专门提供了String类。
public static void main(String[] args) {
// 使用常量串构造
String s1 = "hello";
System.out.println(s1);
// 直接newString对象
String s2 = new String("hello");
System.out.println(s1);
// 使用字符数组进行构造
char[] array = {'h','e','l','l','o'};
String s3 = new String(array);
System.out.println(s1);
}
注意:
1. ==比较是否引用同一个对象
对于内置类型,== 比较的是变量中的值; 对于引用类型 == 比较的是引用中的地址。
public static void main(String[] args) { int a = 10; int b = 20; int c = 10; // 对于基本类型变量,==比较两个变量中存储的值是否相同 System.out.println(a == b); // false System.out.println(a == c); // true // 对于引用类型变量,==比较两个引用变量引用的是否为同一个对象 String s1 = new String("hello"); String s2 = new String("hello"); String s3 = new String("world"); String s4 = s1; System.out.println(s1 == s2); // false System.out.println(s2 == s3); // false System.out.println(s1 == s4); // true }
2. boolean equals(Object anObject) 方法:按照字典序 (字符大小的顺序) 比较
String类重写了父类Object中equals方法,Object中equals默认按照==比较,String重写equals方法后,按照如下规则进行比较:
public boolean equals(Object o) { // 1. 先检测this 和 anObject 是否为同一个对象比较,如果是返回true if (this == o) { return true; } // 2. 检测o是否为String类型的对象,如果是继续比较,否则返回false if (o instanceof String) { // 将o向下转型为String类型对象 String anotherString = (String)o; int n = value.length; // 3. this和o两个字符串的长度是否相同,是继续比较,否则返回false if (n == anotherString.value.length) { char v1[] = value; char v2[] = anotherString.value; int i = 0; // 4. 按照字典序,从前往后逐个字符进行比较 while (n-- != 0) { if (v1[i] != v2[i]) return false; i++; } return true; } } return false; }
利用重写的equals方法,实现String类对象的比较
public static void main(String[] args) {
String s1 = new String("hello");
String s2 = new String("hello");
String s3 = new String("Hello");
// s1、s2、s3引用的是三个不同对象,因此==比较结果全部为false
System.out.println(s1 == s2); // false
System.out.println(s1 == s3); // false
// equals比较:String对象中的逐个字符
System.out.println(s1.equals(s2)); // true
System.out.println(s1.equals(s3)); // false
}
总结:
虽然s1与s2引用的不是同一个对象,但是两个对象中放置的内容相同,因此输出true
s1与s3引用的不是同一个对象,而且两个对象中内容也不同,因此输出false
3. int compareTo(String s) 方法: 按照字典序进行比较
与equals不同的是,equals返回的是boolean类型,而compareTo返回的是int类型。
具体比较方式:
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)); // 不同输出字符差值-1
System.out.println(s1.compareTo(s3)); // 相同输出 0
System.out.println(s1.compareTo(s4)); // 前k个字符完全相同,输出长度差值 -3
}
4. int compareToIgnoreCase(String str) 方法:与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)); // 不同输出字符差值-1
System.out.println(s1.compareToIgnoreCase(s3)); // 相同输出 0
System.out.println(s1.compareToIgnoreCase(s4)); // 前k个字符完全相同,输出长度差值 -3
}
String类提供的常用查找的方法:
方法 | 功能 |
---|---|
char charAt(int index) | 返回index位置上字符,如果index为负数或者越界,抛出IndexOutOfBoundsException异常 |
int indexOf(int ch) | 返回ch第一次出现的位置,没有返回-1 |
int indexOf(int ch,int fromIndex | 从fromIndex位置开始找ch第一次出现的位置,没有返回-1 |
int indexOf(String str) | 返回str第一次出现的位置,没有返回-1 |
int indexOf(String str, int fromIndex) | 从fromIndex位置开始找str第一次出现的位置,没有返回-1 |
int lastIndexOf(int ch) | 从后往前找,返回ch第一次出现的位置,没有返回-1 |
int lastIndexOf(int ch, int fromIndex) | 从fromIndex位置开始找,从后往前找ch第一次出现的位置,没有返回-1 |
int lastIndexOf(String str) | 从后往前找,返回str第一次出现的位置,没有返回-1 |
int lastIndexOf(String str, int fromIndex) | 从fromIndex位置开始找,从后往前找str第一次出现的位置,没有返回-1 |
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
}
}
public static void main(String[] args) { // 数字转字符串 String s1 = String.valueOf(1234); String s2 = String.valueOf(12.34); String s3 = String.valueOf(true); System.out.println(s1); System.out.println(s2); System.out.println(s3); // 字符串转数字 // 注意:Integer、Double等是Java中的包装类型 int data1 = Integer.parseInt("1234"); double data2 = Double.parseDouble("12.34"); System.out.println(data1); System.out.println(data2); }
public static void main(String[] args) {
String s1 = "hello";
String s2 = "HELLO";
// 小写转大写
System.out.println(s1.toUpperCase());
// 大写转小写
System.out.println(s2.toLowerCase());
}
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]);
}
System.out.println();
// 数组转字符串
String s2 = new String(ch);
System.out.println(s2);
}
public static void main(String[] args) {
String s = String.format("%d-%d-%d", 2024, 7,21);
System.out.println(s);
}
方法 | 功能 |
---|---|
String replaceAll(String regex, String replacement) | 替换所有的指定内容 |
String replaceFirst(String regex, String replacement) | 替换首个内容 |
public static void main(String[] args) {
String str = "helloworld" ;
System.out.println(str.replaceAll("l", "_"));
System.out.println(str.replaceFirst("l", "_"));
}
注意事项: 由于字符串是不可变对象(final), 替换不修改当前字符串, 而是产生一个新的字符串
方法 | 功能 |
---|---|
String[] split(String regex) | 将字符串全部拆分 |
String[] split(String regex, int limit) | 将字符串以指定的格式,拆分为limit组 |
public static void main(String[] args) {
String str = "hello world";
String[] result = str.split(" "); // 按照空格拆分
for (String s : result) {
System.out.println(s);
}
}
public static void main(String[] args) {
String str = "hello world hello " ;
String[] result = str.split(" ",2) ;
for(String s: result) {
System.out.println(s);
}
}
public static void main(String[] args) {
String str = "name=zhangsan&age=18" ;
String[] result = str.split("&") ;
for (int i = 0; i < result.length; i++) {
String[] temp = result[i].split("=") ;
System.out.println(temp[0]+" = "+temp[1]);
}
}
方法 | 功能 |
---|---|
String substring(int beginIndex) | 从指定索引截取到结尾 |
String substring(int beginIndex, int endIndex) | 截取部分内容 |
public static void main(String[] args) {
String str = "helloworld" ;
System.out.println(str.substring(5));
System.out.println(str.substring(0, 5));
}
注意事项:
因为String类是不能修改的,所有的修改都会创建新对象,所以对String类进行修改时,效率是非常慢的。因此:尽量避免对String的直接修改,如果要修改建议尽量使用StringBuffer或者StringBuilder。
public static void main(String[] args) {
StringBuilder sb1 = new StringBuilder("hello");
StringBuilder sb2 = sb1;
sb1.append(' ');
sb1.append("world");
sb1.append(123);
System.out.println(sb1);
sb1.reverse();
str = sb1.toString(); // 将StringBuffer以String的方式返回
System.out.println(str);
}
从上述例子可以看出:String和StringBuilder最大的区别在于String的内容无法修改,而StringBuilder的内容可
以修改。频繁修改字符串的情况考虑使用StringBuilder
注意:
1. String变为StringBuilder: 利用StringBuilder的构造方法或append()方法
2. StringBuilder变为String: 调用toString()方法。
public static void main(String[] args) { //String long start=System.currentTimeMillis(); String s = ""; for (int i = 0; i < 10000; i++) { s+=i; } long end = System.currentTimeMillis(); System.out.println(end-start); //StringBuffer start=System.currentTimeMillis(); StringBuffer sbf=new StringBuffer(""); for (int i = 0; i < 10000; i++) { sbf.append(i); } end = System.currentTimeMillis(); System.out.println(end-start); //StringBuilder start=System.currentTimeMillis(); StringBuilder sbd=new StringBuilder(""); for (int i = 0; i < 10000; i++) { sbd.append(i); } end = System.currentTimeMillis(); System.out.println(end-start); }
好了,这期的分享到这里就结束了~
如果这篇博客对你有帮助的话,可以点一个免费的赞并收藏起来哟~
可以点点关注,避免找不到我~
我们下期不见不散~~
下期预告:java—异常
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。