1.實(shí)例化String對(duì)象
直接為String類賦值,字符串是String類匿名對(duì)象,若一個(gè)字符串已經(jīng)被一個(gè)名稱所引用,以后再有相同的聲明時(shí),不會(huì)再重新開(kāi)辟空間
,即共享設(shè)計(jì),內(nèi)容重復(fù)時(shí),將對(duì)象指向已存在的實(shí)例空間
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時(shí),無(wú)論如何都會(huì)再開(kāi)辟空間
public class TestJava {
public static void main(String[] args) {
String name = new String("lixinghua");
System.out.print("姓名:"+name);
}
}
2.判斷字符串是否相等,不可用==,其比較的時(shí)是地址
可用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);
}
}
上面實(shí)際是將str斷開(kāi)在連接,原字符串并未改變。
但可用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ù)組風(fēng)格類似
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.取得字符串長(zhǎng)度
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個(gè)位置開(kāi)始查找
System.out.println(str1.indexOf("x")) ; // 沒(méi)有查到返回-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個(gè)位置開(kāi)始截取
System.out.println(str1.substring(0,5)) ; // 截取0~5個(gè)位置的內(nèi)容
}
};
11.按指定字符串拆分字符串
public class StringAPIDemo08{
public static void main(String args[]){
String str1 = "hello world" ; // 定義字符串
String s[] = str1.split(" ") ; // 按空格進(jìn)行字符串的拆分
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.判斷是否以指定字符串開(kāi)頭或結(jié)尾
public class StringAPIDemo10{
public static void main(String args[]){
String str1 = "**HELLO" ; // 定義字符串
String str2 = "HELLO**" ; // 定義字符串
if(str1.startsWith("**")){ // 判斷是否以“**”開(kāi)頭
System.out.println("(**HELLO)以**開(kāi)頭") ;
}
if(str2.endsWith("**")){ // 判斷是否以“**”結(jié)尾
System.out.println("(HELLO**)以**結(jié)尾") ;
}
}
};
14.不區(qū)分大小寫進(jìn)行字符串比較
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) ;
}
};