String的构造方法
1、public String()
无参构造方法,用来创建空字符串的String对象。
String str1 = new String();
2、public String(String value)
用已知的字符串value创建一个String对象。
String str2 = new String("asdf");
3、public String(char[] value)
用字符数组value创建一个String对象。
char[] value = {"a","b","c","d"};
String str4 = new String(value);//相当于String str4 = new String("abcd");
4、public String(char chars[], int startIndex, int numChars)
用字符数组chars的startIndex开始的numChars个字符创建一个String对象。
char[] value = {"a","b","c","d"};
String str5 = new String(value, 1, 2);//相当于String str5 = new String("bc");
5、public String(byte[] values)
用比特数组values创建一个String对象。
byte[] strb = new byte[]{65,66};
String str6 = new String(strb);//相当于String str6 = new String("AB");
字符串方法
字符串比较:
new String("Phil").compareTo("Liu")
该方法是对字符串内容,按字典顺序进行大小比较
比较是基于字符串中的每个字符的Unicode值
通过返回的整数值指明当前字符串与参数字符串的大小关系
当前对象比参数大:返回正整数,反之:返回负整数,相等:返回0
if(两个字符,长度相等){
if(两个字符,内容相等){
返回 0
}
else{
返回第一个不同字符之间的Unicode差值
}
}
else{
if(短的字符,是否是长字符前面的字串){
返回长度的差值
}
else{
返回第一个不同字符之间的Unicode差值
}
}
compareToIgnore(String anotherString)
与compareTo方法相似,但忽略大小写
equals(Object anotherObject)
比较当前字符串和参数字符串,在两个字符串相等的时候返回true,否则返回false
equalsIgnoreCase(String anotherString)
与equals方法相似,但忽略大小写
==:判断两个变量的内存地址,也就是说它们指向的对象是否一样。和基本类型的比较
equals:比较字符串中所包含的内容是否相同
字符串长度:
int strlength = new String("asdfzxc").length();
字符串某一位置字符:
char ch = new String("asdfzxc").charAt(4);
//返回字符串中指定位置的字符;注意字符串中第一个字符索引是0,最后一个是length()-1
提取子串:
public String substring(int beginIndex)
//该方法从beginIndex位置起,从当前字符串中取出剩余的字符作为一个新的字符串返回。
public String substring(int beginIndex, int endIndex)
//该方法从beginIndex位置起
从当前字符串中取出到endIndex-1位置的字符作为一个新的字符串返回。
字符串连接:
String str = "aa".concat("bb").concat("cc"); 相当于String str = "aa"+"bb"+"cc";
//将参数中的字符串str连接到当前字符串的后面,效果等价于"+"。
字符串中单个字符查找:
public int indexOf(int ch/String str)
用于查找当前字符串中字符或子串
返回字符或子串在当前字符串中从左边起首次出现的位置,若没有出现则返回-1
public int indexOf(int ch/String str, int fromIndex)
改方法与第一种类似,区别在于该方法从fromIndex位置向后查找
字符串中字符的大小写转换:
public String toLowerCase()
返回将当前字符串中所有字符转换成小写后的新串
public String toUpperCase()
返回将当前字符串中所有字符转换成大写后的新串
字符串中字符的替换:
public String replace(char oldChar, char newChar)
用字符newChar替换当前字符串中所有的oldChar字符,并返回一个新的字符串
public String replaceFirst(String regex, String replacement)
该方法用字符replacement的内容替换当前字符串中
遇到的第一个和字符串regex相匹配的子串,应将新的字符串返回
public String replaceAll(String regex, String replacement)
该方法用字符replacement的内容替换当前字符串中
遇到的所有和字符串regex相匹配的子串,应将新的字符串返回
截取空格:
String trim()
截取空格字符串两端的空格,但对于中间的空格不处理。
StringBuffer和StringBuilder
当对字符串进行大量、频繁修改的时候
需要使用 StringBuffer 和 StringBuilder 类
给String重新赋值的时候,是重新指向一个对象
但是StringBuilder,底层也是用一个数组来存储字符串的值
数组的默认长度为 16,即一个空的 StringBuffer 对象,数组长度为 16
实例化一个 StringBuffer 对象即创建了一个大小为 16 个字符的字符串缓冲区
但是当我们调用有参构造函数创建一个 StringBuffer 对象时,数组长度就不再是 16 了
而是根据当前对象的值来决定数组的长度,数组的长度为“当前对象的值的长度+16”
StringBuffer\StringBuilder和 String 的区别:
StringBuffer 和 StringBuilder 类的对象能够被多次的修改,并且不产生新的未使用对象
StringBuffer 线程安全,速度慢
StringBuilder 线程不安全,不能同步访问,速度块
常用方法:
public StringBuffer append(String s)
将指定的字符串追加到此字符序列
public StringBuffer reverse()
将此字符序列用其反转形式取代
public delete(int start, int end)
移除此序列的子字符串中的字符
public insert(int offset, int i)
将 int 参数的字符串表示形式插入此序列中
replace(int start, int end, String str)
使用给定 String 中的字符替换此序列的子字符串中的字符