1.實例化String對象
直接為String類賦值,字符串是String類匿名對象,若一個字符串已經(jīng)被一個名稱所引用,以后再有相同的聲明時槐臀,不會再重新開辟空間
,即共享設計宦搬,內(nèi)容重復時建炫,將對象指向已存在的實例空間
public class TestJava {
public static void main(String[] args) {
String name = "LiXingHua";
System.out.print("姓名:"+“hello".equals("hello"));
}
}
public class TestJava {
public static void main(String[] args) {
String str1 = "hello";
String str2 = new String("hello");
String str3 = "hello";
System.out.println(str1 == str2);
System.out.println(str1 == str3);
}
}
直接調(diào)用構(gòu)造方法元媚,使用new時,無論如何都會再開辟空間
public class TestJava {
public static void main(String[] args) {
String name = new String("lixinghua");
System.out.print("姓名:"+name);
}
}
2.判斷字符串是否相等弯予,不可用==,其比較的時是地址
可用equals(str)
public class TestJava {
public static void main(String[] args) {
String str1 = "hello";
String str2 = new String("hello");
System.out.print(str1.equals(str2));
}
}
3.字符串一旦聲明戚宦,不可改變,
public class TestJava {
public static void main(String[] args) {
String str1 = new String("hello");
String str2 = "hello";
str1 = str1 + "world";
str2 = str2 + "world";
System.out.println(str1);
System.out.println(str2);
}
}
上面實際是將str斷開在連接,原字符串并未改變锈嫩。
但可用StringBuffer類完成
4.字符串與字符數(shù)組的轉(zhuǎn)換
public class TestJava {
public static void main(String[] args) {
String str1 = new String("hello");
char c[] = str1.toCharArray();
for(int i = 0; i < c.length; i++) {
System.out.print(c[i]+"\t");
}
System.out.println("");
String str2 = new String(c);
String str3 = new String(c,0,3);
System.out.println(str1);
System.out.println(str2);
System.out.println(str3);
}
}
5.從字符串取出特定字符
public class TestJava {
public static void main(String[] args) {
String str1 = new String("hello");
System.out.println(str1.charAt(0));
}
}
6.與byte數(shù)組的轉(zhuǎn)換,與char數(shù)組風格類似
public class TestJava {
public static void main(String[] args) {
String str1 = new String("hello");
byte b[] = str1.getBytes();
System.out.println(new String(b));
System.out.println(new String(b,1,3));
}
}
7.取得字符串長度
str1.length();
8.查找指定字符是否存在
public class StringAPIDemo05{
public static void main(String args[]){
String str1 = "abcdefgcgh" ; // 聲明字符串
System.out.println(str1.indexOf("c")) ; // 查到返回位置
System.out.println(str1.indexOf("c",3)) ; // 查到返回位置受楼,從第4個位置開始查找
System.out.println(str1.indexOf("x")) ; // 沒有查到返回-1
}
};
9.去掉左右空格
public class StringAPIDemo06{
public static void main(String args[]){
String str1 = " hello " ; // 定義字符串
System.out.println(str1.trim()) ; // 去掉左右空格后輸出
}
};
10.字符串截取
public class StringAPIDemo07{
public static void main(String args[]){
String str1 = "hello world" ; // 定義字符串
System.out.println(str1.substring(6)) ; // 從第7個位置開始截取
System.out.println(str1.substring(0,5)) ; // 截取0~5個位置的內(nèi)容
}
};
11.按指定字符串拆分字符串
public class StringAPIDemo08{
public static void main(String args[]){
String str1 = "hello world" ; // 定義字符串
String s[] = str1.split(" ") ; // 按空格進行字符串的拆分
for(int i=0;i<s.length;i++){ // 循環(huán)輸出
System.out.println(s[i]) ;
}
}
};
12.字符串大小寫轉(zhuǎn)換
public class StringAPIDemo09{
public static void main(String args[]){
System.out.println("將\"hello world\"轉(zhuǎn)成大寫:" + "hello world".toUpperCase()) ;
System.out.println("將\"HELLO WORLD\"轉(zhuǎn)成小寫:" + "HELLO WORLD".toLowerCase()) ;
}
};
13.判斷是否以指定字符串開頭或結(jié)尾
public class StringAPIDemo10{
public static void main(String args[]){
String str1 = "**HELLO" ; // 定義字符串
String str2 = "HELLO**" ; // 定義字符串
if(str1.startsWith("**")){ // 判斷是否以“**”開頭
System.out.println("(**HELLO)以**開頭") ;
}
if(str2.endsWith("**")){ // 判斷是否以“**”結(jié)尾
System.out.println("(HELLO**)以**結(jié)尾") ;
}
}
};
14.不區(qū)分大小寫進行字符串比較
public class StringAPIDemo11{
public static void main(String args[]){
String str1 = "HELLO" ; // 定義字符串
String str2 = "hello" ; // 定義字符串
System.out.println("\"HELLO\" equals \"hello\" " + str1.equals(str2)) ;
System.out.println("\"HELLO\" equalsIgnoreCase \"hello\" "
+ str1.equalsIgnoreCase(str2)) ; // 不區(qū)分大小寫的比較
}
};
15.將指定字符串替換成其他字符串
public class StringAPIDemo12{
public static void main(String args[]){
String str = "hello" ; // 定義字符串
String newStr = str.replaceAll("l","x") ; // 現(xiàn)在將所有的l替換成x
System.out.println("替換之后的結(jié)果:" + newStr) ;
}
};