一、字符串String詳解
1晨汹、實例化
實例化String對象:直接賦值终议、使用關鍵字new來進行開辟空間
代碼
public class Test41 {
public static void main(String[] args) {
String str = "Hello"; //直接賦值
System.out.println(str);
//使用new關鍵字
String str1 = new String("Hello1");
System.out.println(str1);
}
}
結果:
Hello
Hello1
String使用new關鍵字.jpg
由上圖可以看出虫埂,使用new關鍵字實例化對象祥山,在內(nèi)存中開辟了兩個空間用來存儲他們,其中一個是無用的掉伏,所以使用第一種直接賦值方式更合理一些缝呕,可以更省略一些空間。
2岖免、String的內(nèi)容比較
由以下代碼進行說明
代碼
public class Test42 {
public static void main(String[] args) {
int a = 10;
int b = 10;
System.out.println(a==b);
String str = "Hello";
String str1 = new String("Hello"); //開辟了兩個空間地址
System.out.println(str==str1); //"==" 比較的是地址
System.out.println(str.equals(str1)); //"equals"比較的是內(nèi)容
}
}
結果:
true
false
true
3岳颇、字符串內(nèi)容不可更改
字符串.png
代碼
public class Test43 {
public static void main(String[] args) {
String str = "hello";
String str1 = str + " world!";
System.out.println(str1);
}
}
結果:
hello world!
二照捡、String字符串常用方法
1颅湘、字符串長度
length()方法
代碼
public class Test44 {
public static void main(String[] args) {
String str = "helloworld";
System.out.println("str字符串的長度:"+str.length());
}
}
結果:
str字符串的長度:10
2、字符串轉換數(shù)組
toCharArray()
代碼
public class Test44 {
public static void main(String[] args) {
String str = "helloworld";
// 字符串轉換成數(shù)組
char data[] = str.toCharArray();
for (int i = 0; i < data.length; i++) {
System.out.print(data[i]+" ");
}
}
}
結果:
h e l l o w o r l d
3栗精、從字符串中取出指定位置的字符
charAt()
代碼
public class Test44 {
public static void main(String[] args) {
String str = "helloworld";
// 從字符串中取出指定位置的字符
System.out.println(str.charAt(7));
}
}
結果:
r
4闯参、字符串與byte數(shù)組的轉換
getBytes()
代碼
public class Test44 {
public static void main(String[] args) {
String str = "helloworld";
//字符串與byte數(shù)組的轉換
byte bytes[] = str.getBytes();
for (int i = 0; i < bytes.length; i++) {
System.out.println(new String(bytes)+"\t");
}
}
}
結果:
helloworld
helloworld
helloworld
helloworld
helloworld
helloworld
helloworld
helloworld
helloworld
helloworld
5、過濾字符串中存在的字符
indexOf()
代碼
public class Test44 {
public static void main(String[] args) {
String str = "helloworld@163.com";
System.out.println("@字符所在的位置:"+str.indexOf("@"));
}
}
結果:
@字符所在的位置:10
6悲立、去掉字符串的前后空格
trim()
代碼
public class Test44 {
public static void main(String[] args) {
String str = " hello world";
System.out.println(str);
System.out.println(str.trim());
}
}
結果:
hello world
hello world
7鹿寨、從字符串中取出子字符串
subString()
代碼
public class Test44 {
public static void main(String[] args) {
String str = "helloworld";
System.out.println(str.substring(4));
System.out.println( str.substring(5));
}
}
結果:
oworld
world
8、大小寫轉換
toLowerCase()
薪夕、toUpperCase()
代碼
public class Test44 {
public static void main(String[] args) {
String str = "hello world";
String str1 = "SEC";
System.out.println(str.toUpperCase());
System.out.println(str1.toLowerCase());
}
}
結果:
HELLO WORLD
sec
9脚草、判斷字符串的開頭結尾字符
endsWith()
、startWith()
代碼
public class Test44 {
public static void main(String[] args) {
String email = "sec_hello@126.com";
System.out.println(email.startsWith("sec"));
System.out.println(email.endsWith("com"));
System.out.println(email.endsWith("163.com"));
}
}
結果:
true
true
false
10原献、替換String字符串中的一個字符
replace()
代碼
public class Test44 {
public static void main(String[] args) {
String email = "sec_hello@126.com";
System.out.println("將字符串中的e替換成m:"+email.replace('e', 'm'));
}
}
結果:
將字符串中的e替換成m:smc_hmllo@126.com
三馏慨、StringBuffer方法
1、認識StringBuffer
緩沖區(qū)姑隅,本身也是操作字符串写隶,但是與String不同,StringBuffer是可以更改的讲仰。
StringBuffer是一個操作類慕趴,所以必須通過實例化進行操作。
代碼
public class Test45 {
public static void main(String[] args) {
StringBuffer sBuffer = new StringBuffer();
sBuffer.append("hello");
System.out.println("StringBuffer更改前:"+sBuffer.toString());
tell(sBuffer);
System.out.println("StringBuffer更改后:"+sBuffer.toString());
String string = "android";
System.out.println("String更改前:"+string);
tell1(string);
System.out.println("String更改后:"+string);
}
public static void tell(StringBuffer s) {
s.append(" I love sec");
}
public static void tell1(String str) {
str = "java";
}
}
結果:
StringBuffer更改前:hello
StringBuffer更改后:hello I love sec
String更改前:android
String更改后:android
2鄙陡、StringBuffer常用方法
append()
追加
示例
public class Test46 {
public static void main(String[] args) {
StringBuffer sBuffer = new StringBuffer();
sBuffer.append("hello ");
sBuffer.append("world");
System.out.println(sBuffer.toString());
}
}
結果:
hello world
insert()
插入
示例
public class Test46 {
public static void main(String[] args) {
StringBuffer sBuffer = new StringBuffer();
sBuffer.append("hello ");
sBuffer.append("world");
System.out.println(sBuffer.toString());
sBuffer.insert(6, "love "); //從第7個位置開始插入字符love
System.out.println(sBuffer.toString());
}
}
結果:
hello world
hello love world
replace()
替換
示例
public class Test46 {
public static void main(String[] args) {
StringBuffer sBuffer = new StringBuffer();
sBuffer.append("hello ");
sBuffer.append("world");
System.out.println(sBuffer.toString());
sBuffer.replace(1, 4, "wwtcom");//從第2個位置到第4個位置替換成"wwtcom"
System.out.println(sBuffer.toString());
}
}
結果:
hello world
hwwtcomo world
indexOf()
字符串存在的位置
示例
public class Test46 {
public static void main(String[] args) {
StringBuffer sBuffer = new StringBuffer();
sBuffer.append("hello@");
sBuffer.append("world");
System.out.println(sBuffer.toString());
System.out.println("@字符存在的位置:"+sBuffer.indexOf("@"));
}
}
結果:
hello@world
@字符存在的位置:5
3冕房、StringBuffer類的應用
代碼
public class Test47 {
public static void main(String[] args) {
String str = "hello";
for (int i = 0; i < 100; i++) {
str = str + i;
}
System.out.println(str); //使用String需要開辟101個空間,很耗資源
StringBuffer sBuffer = new StringBuffer();
sBuffer.append("hello");
for (int i = 0; i < 100; i++) {
sBuffer.append(i);
}
System.out.println(sBuffer.toString());//使用StringBuffer運行很快趁矾,不需要重新開辟空間
}
}
結果:
hello0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
hello0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
四耙册、StringBuider用法
- 一個可變的字符序列,該類被設計作用StringBuffer的一個簡易替換愈魏,用在字符串緩沖區(qū)被單個線程使用的時候觅玻。建議優(yōu)先考慮該類想际,速度比StringBuffer要快。
- 但是如果設計到線程安全溪厘,建議使用StringBuffer胡本。
- 常用方法:
append()
、insert()
使用方式與StringBuffer是一樣的畸悬。
代碼
public class Test48 {
public static void main(String[] args) {
StringBuilder sBuilder = new StringBuilder();
sBuilder.append("Hello ");
sBuilder.append("world");
System.out.println("sBuilder更改前:"+sBuilder.toString());
sBuilder.insert(6, "love "); //從第7個位置開始插入字符love
System.out.println("sBuilder更改后:"+sBuilder.toString());
}
}
結果:
sBuilder更改前:Hello world
sBuilder更改后:Hello love world