Java String类
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
开发工具与关键技术:
作者:陈剑波
撰写时间:2019年07月09日
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
String类
String类也叫字符串常量,在java中数据类型分为基本数据类型和引用数据类型,所以理论上String类也属于引用数据类型。
String类在java.lang包中,java使用String类创建一个字符串变量,字符串变量属于对象。java把String类声明的final类,不能有类。String类对象创建后不能修改,由0或多个字符组成,包含在一对双引号之间。
示例代码:
import java.io.UnsupportedEncodingException; |
import java.nio.charset.Charset; |
|
public class StringDemo { |
public static void main(String[] args) { |
// 一、String类常用构造器 |
// String(String original):初始化一个新创建的String对象,使其表示一个与参数相同的 |
// 字符序列;换句话说,新创建的字符串是该参数字符串的副本。 |
// 创建String类对象 |
String string = new String("字符串常量"); |
// 把字符串常量值转成字符串对象 |
// String():初始化一个新创建的String对象,使其表示一个空字符序列。 |
String string2 = new String(); |
// String(byte[] bytes):通过使用平台的默认字符集解码指定的byte数组,构造一个新的String。 |
byte[] bytes = new byte[] { 65, 66, 67, 68, 69, 70, 70, 72, 73 }; |
String string3 = new String(bytes); |
System.out.println("0 " + string3); |
// String(byte[] bytes, Charset |
// charset):通过使用指定的charset解码指定的byte数组,构造一个新的 String。 |
// 这串byte数组是使用GBK编码生成的 |
byte[] bs2 = new byte[] { -43, -30, -54, -57, -42, -48, -50, -60 }; |
// 由于当前的默认字符集编码是UTF‐8,使用默认默认字符集编码转换时就会乱码 。 |
System.out.println("1 " + new String(bs2)); |
String string4 = new String(bs2, Charset.forName("GBK")); |
|
// String(byte[] bytes, String |
// charsetName):通过使用指定的 charset 解码指定的 byte 数组,构造一个新的 String。 |
System.out.println("2 " + string4); |
try { |
string4 = new String(bs2, "GBK"); |
System.out.println("3 " + string4); |
} catch (UnsupportedEncodingException e) { |
// TODO: handle exception |
e.printStackTrace(); |
} |
// String(byte[] bytes, int |
// offset, int length):通过使用平台的默认字符集解码指定的byte子数组,构造一个 新的String。 |
String string5 = new String(bytes, 0, 6); |
System.out.println("4 " + new String(string5)); |
// String(byte[] bytes, int offset, int length, Charset |
// charset):通过使用指定的charset解码指定的byte子数组,构造一个新的String。 |
String string6 = new String(bytes, 0, 6, Charset.forName("UTF-8")); |
System.out.println("5 " + string6); |
// String(byte[] bytes, int offset, int length, String |
// charsetName):通过使用指定的字符集解码指定的byte子数组,构造一个新的 String。 |
try { |
string6 = new String(bytes, 0, 6, "UTF-8"); |
System.out.println("6 " + string6); |
} catch (UnsupportedEncodingException e) { |
// TODO: handle exception |
e.printStackTrace(); |
} |
// String(char[] value);String(char[] value, int offset, int |
// count):分配一个新的String,它包含取自字符数组参数一个子数组的字符。 |
char[] cs = new char[] { 'a', 'b', 'c', '1', '2', '3' }; |
String string7 = new String(cs); |
System.out.println("7 " + string7); |
string7 = new String(cs, 1, 4); |
System.out.println("8 " + string7); |
// 二、String常用判断方法 |
String string8 = "abcd"; |
String string9 = "Abcd"; |
|
// equals(Object obj):比较字符串的内容是否相同,区分大小写 |
System.out.println("9 " + string8.equals(string9)); |
// equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写 |
System.out.println("10 " + string8.equalsIgnoreCase(string9)); |
// contains(String str):判断大字符串中是否包含小字符串,区分大小写 |
System.out.println("11 " + string8.contains("b")); |
// startsWith(String str):判断字符串是否以某个指定的字符串开头,区分大小写 |
System.out.println("12 " + string8.startsWith("ab")); |
// endsWith(String str):判断字符串是否以某个指定的字符串结尾,区分大小写 |
System.out.println("13 " + string9.endsWith("d")); |
// isEmpty():判断字符串是否为空。 |
System.out.println("14 " + string9.isEmpty()); |
// 三、String类常用的获取方法 |
String string10 = "zxcvbnmn 123456789"; |
// length():获取字符串的长度。 |
System.out.println("length:" + string10.length()); |
// charAt(int index):获取指定索引位置的字符 |
System.out.println("charAt:" + string10.charAt(5)); |
// indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引。 |
System.out.println("15 " + string10.indexOf('n')); |
// indexOf(int ch,int fromIndex):返回在此字符串中第一次出现指定字符处的索引,从指定的 索引开始搜索。 |
System.out.println("16 " + string10.indexOf('n', 7)); |
// indexOf(String str):返回指定子字符串在此字符串中第一次出现处的索引。 |
System.out.println("17 " + string10.indexOf("nm")); |
// indexOf(String str,int fromIndex):返回指定子字符串在此字符串中第一次出现处的索引,从指 定的索引开始。 |
System.out.println("18 " + string10.indexOf("nm", 3)); |
// lastIndexOf(int ch):返回指定字符在此字符串中后一次出现处的索引。 |
System.out.println("19 " + string10.lastIndexOf('n')); |
// lastIndexOf(int ch,int fromIndex):返回指定字符在此字符串中后一次出现处的索引,从指定 |
// 的索引处开始进行反向搜索。 |
System.out.println("20 " + string10.lastIndexOf('n', 7)); |
// lastIndexOf(String str):返回指定子字符串在此字符串中右边出现处的索引。 |
System.out.println("21 " + string10.lastIndexOf("nm")); |
// lastIndexOf(String str,int fromIndex):返回指定子字符串在此字符串中后一次出现处的索引,从 |
// 指定的索引开始反向搜索。 |
System.out.println("22 " + string10.lastIndexOf("nm", 7)); |
// substring(int start):从指定位置开始截取字符串,默认到末尾。 |
String string11 = string10.substring(5); |
System.out.println(string11); |
// substring(int start,int end):从指定位置开始到指定位置结束截取字符串。 |
String string12 = string10.substring(5, 10); |
System.out.println(string12); |
// 四、String的常用转换方法 |
String string13 = "你好,世界!"; |
byte[] bs; |
// getBytes():把字符串转换为字节数组。 |
try { |
bs = string13.getBytes("GBK"); |
System.out.println("GBK 编码"); |
for (byte b : bs) { |
System.out.print(b + ","); |
} |
System.out.println("\nutf-8 编码"); |
bs = string13.getBytes(); |
for (byte b : bs) { |
System.out.print(b + ","); |
} |
System.out.println(); |
String newStr = new String(bs); |
System.out.println("转为字符串:" + newStr); |
} catch (UnsupportedEncodingException e) { |
// TODO Auto-generated catch block |
e.printStackTrace(); |
} |
// toCharArray():把字符串转换为字符数组。 |
System.out.println("----toCharArray----"); |
char[] arr = string13.toCharArray(); |
for (int i = 0; i < arr.length; i++) { |
System.out.print(arr[i] + " "); |
} |
// String valueOf():把传入的变量转成字符串。 |
System.out.println(); |
System.out.println("----valueOf----"); |
String string14 = String.valueOf(arr); |
System.out.println(string14); |
System.out.println("----toLowerCase;toUpperCase----"); |
String string15 = "awefrgDFSAF"; |
// toLowerCase():把字符串转成小写。 |
System.out.println(string15.toLowerCase()); |
// toUpperCase():把字符串转成大写。 |
System.out.println(string15.toUpperCase()); |
// concat(String str):把字符串拼接。 |
System.out.println("----concat----"); |
String string16 = "abc"; |
String string17 = "sdf"; |
String string18 = string16.concat(string17); |
System.out.println(string18); |
// 五、String类其他常用方法 |
// replace(char old,char new):返回一个新的字符串,它是通过用newChar替换此 |
// 字符串中出现的所有oldChar得到的。 |
System.out.println("----replace----"); |
String string19 = "asdfghjkl"; |
String string20 = string19.replace('a', 'A'); |
System.out.println(string20); |
// replace(String old,String new):使用指定的字面值替换序列替换此字符串所有匹配字面值目标序列的子字符串。 |
String string21 = string19.replace("fg", "ooooo"); |
System.out.println(string21); |
// trim() 返回字符串的副本,忽略前导空白和尾部空白。 |
System.out.println("----trim----"); |
String string22 = " aeefsf "; |
String string23 = string22.trim(); |
System.out.println(string23); |
System.out.println("----compareTo;compareToIgnoreCase----"); |
String string24 = "aA"; |
String string25 = "aaa"; |
// compareTo(String str):按字典顺序比较两个字符串。 |
int inta = string24.compareTo(string25); |
System.out.println(inta); |
// compareToIgnoreCase(String str):按字典顺序比较两个字符串,不考虑大小写。 |
int intb = string24.compareToIgnoreCase(string25); |
System.out.println(intb); |
// split(String regex) 根据给定正则表达式的匹配拆分此字符串。 |
System.out.println("----split----"); |
String string26 = "asdgjkl;sadfg;fasdfg;rtscvb;rewe;d;;fg;hrew;5623;"; |
String[] strings = string26.split(";"); |
for (String string27 : strings) { |
System.out.print("\"" + string27 + "\","); |
} |
System.out.println(); |
//split(String regex,int limit) 根据匹配给定的正则表达式来拆分此字符串。 |
String[] strings2 = string26.split(";",5); |
for (String string27 : strings2) { |
System.out.print("\"" + string27 + "\","); |
} |
} |
} |
运行结果:
0 ABCDEFFHI |
1 �������� |
2 这是中文 |
3 这是中文 |
4 ABCDEF |
5 ABCDEF |
6 ABCDEF |
7 abc123 |
8 bc12 |
9 false |
10 true |
11 true |
12 true |
13 true |
14 false |
length:18 |
charAt:n |
15 5 |
16 7 |
17 5 |
18 5 |
19 7 |
20 7 |
21 5 |
22 5 |
nmn 123456789 |
nmn 1 |
GBK 编码 |
-60,-29,-70,-61,-93,-84,-54,-64,-67,-25,-93,-95, |
utf-8 编码 |
-28,-67,-96,-27,-91,-67,-17,-68,-116,-28,-72,-106,-25,-107,-116,-17,-68,-127, |
转为字符串:你好,世界! |
----toCharArray---- |
你 好 , 世 界 ! |
----valueOf---- |
你好,世界! |
----toLowerCase;toUpperCase---- |
awefrgdfsaf |
AWEFRGDFSAF |
----concat---- |
abcsdf |
----replace---- |
Asdfghjkl |
asdooooohjkl |
----trim---- |
aeefsf |
----compareTo;compareToIgnoreCase---- |
-32 |
-1 |
----split---- |
"asdgjkl","sadfg","fasdfg","rtscvb","rewe","d","","fg","hrew","5623", |
"asdgjkl","sadfg","fasdfg","rtscvb","rewe;d;;fg;hrew;5623;", |