在JDK1.9前,所有的String都利用了字符數(shù)組實(shí)現(xiàn)了包裝的處理遏弱,所以在String類里面提供有相應(yīng)的轉(zhuǎn)換處理,這些方法包含構(gòu)造方法和普通方法兩類
構(gòu)造方法:
public String?(char[] value);
將傳入的全部字符數(shù)組轉(zhuǎn)換為字符串倦青;
public String?(char[] value,
int offset,
int count)祭饭;
將部分字符數(shù)組變?yōu)樽址?/p>
普通方法:
public char charAt?(int index)芜茵;
獲取指定索引位置的字符;
public char[] toCharArray?()倡蝙;
將字符串中的數(shù)據(jù)以字符數(shù)組的形式保存九串;
范例:觀察charAt()方法
package 面向?qū)ο?
public class StringDemo02 {
public static void main(String[] args) {
String str = "www.mldn.cn";
char c = str.charAt(5);
System.out.println(c);
}
}
程序中的索引下標(biāo)都是從0開(kāi)始的;
在程序語(yǔ)言中寺鸥,最早一直強(qiáng)調(diào)的就是字符串應(yīng)該使用字符數(shù)組進(jìn)行描述
范例:實(shí)現(xiàn)字符串與字符數(shù)組的轉(zhuǎn)換
package 面向?qū)ο?
public class StringDemo02 {
public static void main(String[] args) {
String str = "helloworld";
char[] res = str.toCharArray();
for (int i = 0; i < res.length; i++) {
res[i] -= 32;
}
//字符數(shù)組轉(zhuǎn)換成字符串
String newStr = new String(res);
System.out.println(res);
System.out.println(new String(res, 0, 5));
}
}
輸出結(jié)果:
HELLOWORLD
HELLO
范例:實(shí)現(xiàn)驗(yàn)證功能猪钮,判斷某一個(gè)字符串中的數(shù)據(jù)是否全部由數(shù)字組成。
這時(shí)可以采用以下的思路:
1胆建、最好將字符串變?yōu)樽址麛?shù)組
2烤低、可以判斷每個(gè)字符是否在數(shù)字的范圍之內(nèi)(0-9)
3、如果有一位不是數(shù)字則表示驗(yàn)證失敗
package 面向?qū)ο?
public class StringDemo02 {
public static void main(String[] args) {
String str = "helloworld";
System.out.println(isNumber(str) ? "由數(shù)字所組成" : "不是由數(shù)字所組成");
}
//該方法主要判斷字符串是否由數(shù)字組成
public static boolean isNumber(String str) {
char[] res = str.toCharArray(); //將字符串轉(zhuǎn)換為字符數(shù)組
for (int i = 0 ; i < res.length; i++) {
if (res[i] < '0' || res[i] > '9') {
return false; //后面不做判斷
}
}
return true;
}
}
執(zhí)行結(jié)果為:不是由數(shù)字所組成
在實(shí)際開(kāi)發(fā)中笆载,往往使用char類型處理中文扑馁,因?yàn)槠淇梢园形臄?shù)據(jù)。
字符串與字節(jié)數(shù)組的轉(zhuǎn)換
字符串與字節(jié)數(shù)組直接也可以實(shí)現(xiàn)轉(zhuǎn)換操作凉驻,需要注意腻要,當(dāng)進(jìn)行字符串與字節(jié)數(shù)組的轉(zhuǎn)換時(shí)主要目的是為了二進(jìn)制的數(shù)據(jù)傳輸或者進(jìn)行編碼轉(zhuǎn)換。
public String?(byte[] bytes)
將全部的字節(jié)數(shù)組變?yōu)樽址?/p>
public String?(byte[] bytes,
int offset,
int length)
將部分的字節(jié)數(shù)組轉(zhuǎn)換為字符串涝登;
public byte[] getBytes?()雄家;
將字符串轉(zhuǎn)為字節(jié)數(shù)組;這個(gè)必須掌握胀滚;
<pre style="white-space: pre-wrap; word-wrap: break-word; tab-size: 4; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; display: block; padding: 9.5px; margin: 0px 0px 10px; font-size: 13px; line-height: 1.428571429; color: rgb(51, 51, 51); word-break: break-all; background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;">public byte[] getBytes?([String](https://docs.oracle.com/javase/9/docs/api/java/lang/String.html "class in java.lang") charsetName)
throws [UnsupportedEncodingException](https://docs.oracle.com/javase/9/docs/api/java/io/UnsupportedEncodingException.html "class in java.io")</pre>
編碼轉(zhuǎn)換趟济;這個(gè)是最重要的方法,必須記字肓堋咙好;
范例:字符串編碼轉(zhuǎn)換
package 面向?qū)ο?
public class StringDemo02 {
public static void main(String[] args) {
String str = "helloworld";
byte[] data = str.getBytes(); //將字符串轉(zhuǎn)換為字節(jié)數(shù)組
for (int i = 0; i < data.length; i++) {
data[i] -= 32; //小寫變大寫字母
}
System.out.println(new String(data));
System.out.println(new String(data, 0, 5));
}
}
輸出結(jié)果:
HELLOWORLD
HELLO
注意:字節(jié)本身是有長(zhǎng)度限制的,一個(gè)字節(jié)最多可以保存的范圍是:-128~127之間