String(byte[] bytes)
通过使用平台的默认字符集解码指定的字节数组构造了一个新的 String。
public String(byte[] bytes,String charsetName)-使用参数指定的字符集来进行解码
String的常用方法:
1、public char charAt(int index)--返回指定索引的 char字符
例如:
String str1 = "hello";
char c = str1.charAt(0);
注意:
下标范围:0~长度-1
如果超出范围,则运行会抛出StringIndexOutOfBoundsException异常
public char charAt(int index) {
if ((index < 0) || (index >= value.length)) {
throw new StringIndexOutOfBoundsException(index);
}
return value[index];
}
练习:循环遍历字符串的每一个字符
for (int i = 0; i <=4 ; i++) {
System.out.println(str1.charAt(i));
}
2、public int length()-获取字符串的长度
练习:循环遍历字符串的每一个字符
for (int i = 0; i <=str1.length()-1 ; i++) {
System.out.println(str1.charAt(i));
}
3、public int compareTo(String anotherString)--比较两个字符串的大小
结果返回值int:
0 当前字符串内容和参数字符串内容相等
正数 当前字符串内容大于参数字符串内容
负数 当前字符串内容小于参数字符串内容
源码:
public int compareTo(String anotherString) {
int len1 = value.length;//len1=3
int len2 = anotherString.value.length;//len2=2
int lim = Math.min(len1, len2);//lim=2
char v1[] = value;//v1=abc
char v2[] = anotherString.value;//v2=ab
int k = 0;//下标
while (k < lim) {
char c1 = v1[k];//c1=a
char c2 = v2[k];//c2=a
if (c1 != c2) {
return c1 - c2;//'c'-'e'
}
k++;
}
return len1 - len2;//3-2=1
}
4、public int compareToIgnoreCase(String str)-忽略大小写
结果返回值int:
0 当前字符串内容和参数字符串内容相等
正数 当前字符串内容大于参数字符串内容
负数 当前字符串内容小于参数字符串内容
总结:
compareTo\compareToIgnoreCase方法通常用于字符串排序--有序集合
5、public boolean contains(CharSequence s)--判断当前字符串是否包含参数字符串
CharSequence 接口
String是CharSequence 接口的实现类
6、public static String copyValueOf(char[] data)--将字符数组转为字符串类型
public static String copyValueOf(char data[]) {
return new String(data);
}
7、public boolean endsWith(String suffix)-判断当前字符串是否以参数指定的内容结尾
8、public boolean startsWith(String prefix)-判断当前字符串是否以参数指定的内容开头
9、public boolean startsWith(String prefix, int toffset)-判断当前字符串从指定下标开始是否以参数指定的内容开头
10、public boolean equals(Object anObject)--比较两个字符串的内容是否相等
String重写了equals方法
源码:
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = value.length;
if (n == anotherString.value.length) {
char v1[] = value;
char v2[] = anotherString.value;
int i = 0;
while (n-- != 0) {
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
}
return false;
}
11、public boolean equalsIgnoreCase(String anotherString)--忽略大小写比较字符串内容是否相同
验证码校验案例
12、public byte[] getBytes()--使用默认字符集将字符串转为字节数组
idea:默认字符集 utf-8 一个中文汉字占用3个字节
eclipse:默认字符集 GBK 一个中文汉字占用2个字节
13、public byte[] getBytes(String charsetName)--使用指定的字符集将字符串转为字节数组
14、public void getChars(int srcBegin, int srcEnd,char[] dst, int dstBegin)
将字符串指定的内容复制到指定数组中
15、public int hashCode()-返回当前字符串对象的hash码值
String重写了Object的hashCode()
计算公式:s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
字符串内容相同 hash值相同
字符串内容不同 hash值不相同
查找第一次位置
16、public int indexOf(int ch)-查找参数指定字符在字符串中第一次出现的下标
17、public int indexOf(int ch,int fromIndex)-从指定位置开始查找参数指定字符在字符串中第一次出现的下标
如果查找不到 则返回值为-1
18、public int indexOf(String str)-查找参数指定字符串在当前字符串中第一次出现的下标
19、public int indexOf(String str, int fromIndex)--从指定位置开始查找参数指定字符串在当前字符串中第一次出现的下标
扩展:字符串查找算法
查找最后一次出现的位置
int lastIndexOf(int ch)
返回在指定字符的最后一个发生的字符串内的索引。
int lastIndexOf(int ch, int fromIndex)
返回在指定字符的最后一个发生的字符串内的索引,在指定的索引处搜索向后开始。
int lastIndexOf(String str)
返回指数在这个字符串的指定子字符串中最后出现。
int lastIndexOf(String str, int fromIndex)
返回此字符串的指定子字符串中最后出现在索引,搜索后从指定索引处开始。
20、public String intern()--返回当前字符串对象在字符串常量池中的地址
21、public boolean isEmpty()--判断字符串是否为空串
String str = "";
String str = null;
替换
不会直接对原有的字符串进行修改
替换的结果则是以返回值进行返回 底层实际是产生了新的字符串对象
22、public String replace(char oldChar, char newChar)--字符替换
23、public String replace(CharSequence target, CharSequence replacement)-字符串替换
截取
不会直接对原有的字符串进行修改
截取的结果则是以返回值进行返回 底层实际是产生了新的字符串对象
24、public String substring(int beginIndex)-从指定下标开始截取
25、public String substring(int beginIndex,int endIndex)-从指定下标开始截取 到指定下标结束
左闭右开
26、public char[] toCharArray()--字符串转为字符数组
底层返回了新的字符数组
字母大小写转换
不会直接对原有的字符串进行修改
转换的结果则是以返回值进行返回 底层实际是产生了新的字符串对象
27、public String toLowerCase()-小写
28、public String toUpperCase()-大写
29、public String trim()-去掉开头和结尾的空白字符
静态转换为字符串的方法-valueOf
public static String valueOf(char[] data)
new String(data)
static String valueOf(Object obj)
专题:
字符数组<->字符串:
字符数组->字符串:
new String(char[] cs);
String.copyValueOf(char[] cs);
字符串->字符数组:
toCharArray()
字节数组<->字符串:
字符串->字节数组:
getBytes()-默认字符集
getBytes(String charsetName)-参数指定字符集
字节数组->字符串:
new String(byte[] bs)--默认字符集
new String(byte[] bs,String charsetName)-指定字符集
专题:
字符串拼接
+:
基于StringBuilder实现的 append来实现拼接追加
效率要高于String concat方法
concat()方法
"hello".concat("a")
源码:
public String concat(String str) {
int otherLen = str.length();
if (otherLen == 0) {
return this;
}
int len = value.length;//5
char buf[] = Arrays.copyOf(value, len + otherLen);//buf={'h','e','l','l','o','\u0000'}
str.getChars(buf, len);//buf={'h','e','l','l','o','a'}
return new String(buf, true);
}
不建议concat方法进行大量字符串的拼接-- 内存利用率不高 效率不高
//循环拼接
String str = "start";//常量池 start字符串对象 1对象
for (int i = 1; i <=1000 ; i++) {
//"a" 字符串常量池也创建了一个对象
//new String("starta") new String在堆中创建了一个对象 ”starta“ 字符串常量池也创建了一个对象
//new String("startaa") new String在堆中创建了一个对象 ”startaa“ 字符串常量池也创建了一个对象
str = str.concat("a");
}
//共产生了2002个对象
System.out.println(str);
StringBuilder
一个可变的字符序列
类的属性;
char[] value;--存储字符串的内容 value是可变的
构造方法:
public StringBuilder()
public StringBuilder(String str)
方法:
append--追加
public StringBuilder append(String str)
直接修改了当前对象的value属性值 返回的还是当前对象
建议使用StringBuilder进行大量的字符串拼接-提高内存利用率 提高效率
StringBuilder builder = new StringBuilder("start");
//new StringBuilder在堆中创建一个对象 "start" 字符串常量池中创建了一个对象
for (int i = 1; i <=1000 ; i++) {
//"a" 字符串常量池也创建了一个对象
//"starta" 字符串常量池也创建了一个对象
//"startaa" 字符串常量池也创建了一个对象
builder.append("a");
}
String str = builder.toString();//new String(value)
//共产生了1004个对象
System.out.println(str);
StringBuffer
一个可变的字符序列
注意:
StringBuilder是线程非安全的
StringBuffer是线程安全
案例:
1、输出字符串中所有的数字字符
2、求字符串中所有数字的和
3、统计字符串中字母、数字、其他字符出现的次数
正则表达式
2021年7月20日
11:57
字符串的格式匹配
正则表达式-独立的技术
String类中提供了支持正则表达式的方法
正则表达式的语法规则