1.简介
字符串或串(String)是由数字、字母、下划线组成的一串字符。由双引号括起来,如:x=“hello, java”。在Java中字符串常做String类型的对象处理,String类位于java.lang包下,默认情况下,该包自动导入所有程序。
2.存储原理图
public class Hello{
public static void main(String[] args) {
String s1="Hello ";//编译期创建
String s2="World";
String str1 = new String("Hello "); //运行期new出来,存储在堆中
String str2 = new String("World");
str2=str2+str1;
System.out.println(str2);
}
}
3.String类常用方法
方法名 | 作用 |
---|---|
int length() | 返回此字符串的长度 |
int indexOf(int ch) | 返回指定字符在此字符串中第一次出现处的索引 |
int lastIndexOf(int ch) | 返回指定字符在此字符串中最后一次出现处的索引 |
char charAt(int index) | 返回指定索引处的 char 值 |
int compareTo(Object o) | 把这个字符串和另一个对象比较 |
String concat(String str) | 将指定字符串连接到此字符串的结尾 |
String replace(char oldChar, char newChar) | 返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的 |
String toString() | 返回此对象本身(它已经是一个字符串!) |
String toUpperCase() | 使用默认语言环境的规则将此 String 中的所有字符都转换为大写 |
isEmpty() | 判断字符串是否为空 |
int hashCode() | 返回此字符串的哈希码 |
boolean equals(Object anObject) | 将此字符串与指定的对象比较 |
使用案例:
public class TextComparePosition {
/**
* 字符串常用方法
* @param args
*/
public static void main(String[] args) {
String str1="look for,here this";
int lastposition=str1.lastIndexOf("this");//str.lastindexOf(strname)字符strname在str中最后一次出现的位置
if (lastposition==-1) {
System.out.println("没有找到该字符床");
} else {
System.out.println("该字符最后一次出现的位置为:"+lastposition);
}
int fristposition=str1.indexOf("o");//str.indexOf(strname)字符strname在str中出现的位置
System.out.println("该字符首次出现的位置;"+fristposition);
int position1=str1.indexOf("o", 2);
int position2=str1.indexOf("o", 3);
System.out.println("从第2个位置开始o第一次出现的位置:"+position1);
System.out.println("从第3个位置开始o第一次出现的位置:"+position2);
System.out.println("-----------------------------------------------");
String str2="the room is very good!";
String str3="THE DOG IS BAD!";
String strupper=str2.toUpperCase();
System.out.printf("转换大写前的字符串:%s",str2+"\n");
System.out.printf("转换大写后的字符串:%s",strupper+"\n");
String strlower=str3.toLowerCase();
System.out.println("转换小写前的字符串:"+str3);
System.out.println("转换小写后的字符串:"+strlower);
System.out.println("-----------------------------------------------");
String str4="work hard,day day up!";
String str5="WORK HARD,DAY DAY UP!";
Object objstr=str1;
System.out.println(str4.compareTo(str5));//通过compareTo(String)函数比较返回并str1和str2的差值
System.out.println(str4.compareToIgnoreCase(str5));//通过compareToIgnoreCase(String)函数(忽略大小写)比较返回并str1和str2的差值
System.out.println(str4.compareTo(objstr.toString()));//通过compareTo(String)函数比较返回并str1和objstr的差值
System.out.println("-----------------------------------------------");
String s1="this is a beautiful village";
String s2=s1.substring(3);//删除字符串3位置以前的所有字符,返回剩余字符
String s3=s1.substring(2, 6);//删除字符串2-5的所有字符,返回删除字符
System.out.println(s2);
System.out.println(s3);
System.out.println("-----------------------------------------------");
String r1="look, he here";
String r2="看,他在哪里!";
String reverse1=new StringBuffer(r1).reverse().toString();//反转字符串
String reverse2=new StringBuffer(r2).reverse().toString();
System.out.println("反转字符串:"+reverse1);
System.out.println("反转字符串:"+reverse2);
System.out.println("-----------------------------------------------");
String str6="this food is delicious!";
String str7="this food is very delicious!";
System.out.println(str6.replace("i", "o"));//用o替换i
System.out.println(str7.replace(str6, str7));//用str6替换str7
System.out.println(str6.replaceAll(str6, str7));
}
}
运行结果:
4. String、StringBuilder 与 StringBuffer
(1)、这三个类之间的区别主要是在两个方面,即运行速度和线程安全这两方面。
(2)、运行速度由快到慢为:StringBuilder > StringBuffer > String
String最慢的原因:String为字符串常量,而StringBuilder和StringBuffer均为字符串变量,即String对象一旦创建之后该对象是不可更改的,但后两者的对象是变量,是可以更改的。
(3)、在线程安全上,StringBuilder是线程不安全的,而StringBuffer是线程安全的。
如果一个StringBuffer对象在字符串缓冲区被多个线程使用时,StringBuffer中很多方法可以带有synchronized关键字,所以可以保证线程是安全的,但StringBuilder的方法则没有该关键字,所以不能保证线程安全,有可能会出现一些错误的操作。所以如果要进行的操作是多线程的,那么就要使用StringBuffer,但是在单线程的情况下,还是建议使用速度比较快的StringBuilder。
(4)、使用环境:
String:适用于少量的字符串操作的情况
StringBuilder:适用于单线程下在字符缓冲区进行大量操作的情况
StringBuffer:适用多线程下在字符缓冲区进行大量操作的情况
5. equals() 和 “==”
-
“==” 的作用:
基本类型(byte,short,char,int,long,float,double,boolean):比较的就是值是否相同;
引用类型(接口、类、数组等非基本数据类型):比较的就是地址值是否相同; -
equals 的作用:
引用类型:默认情况下,比较的是地址值,重写该方法后比较对象的成员变量值是否相同
-
对于String、Integer、Date等覆盖了equals()方法的类型,”==”比较的是存放的内存地址。而equals()的结果则由覆盖后的代码决定。
-
string类型中,equals()比较的是两字符串内容是否相同。
public class TextString{
public static void main(String[] args) {
String a="12";
String b="12";
String c=new String("12");
System.out.println(a==b);//true
System.out.println(a==c);//false
System.out.println(a.equals(b));//true
System.out.println(a.equals(c));//true
}
}