字符串和String类
java字符串是Unicode字符的有序集合,而且,在java中,字符串是作为对象的。
java语言中使用java.lang包中的String、StringBuilder、StringBuffer来构造自定义字符串,执行许多基本字符串操作,
String对象
String类是不可变得,只要一旦创建了该对象,就不能修改该对象的值。有些字符串操作看来似乎修改了String对象,实际上是返回一个包含修改内容的新String对象。
-
字符串的定义和赋值
字符串声明:String stringName;
字符串创建:stringName = new String(字符串常量);或stringName = 字符串常量;
String str1 = “hello”;
String str2 = new String(“hello”);
String str3 = “he”+“llo”;//“hello”
String str4 = new String(“he”)+new String(“llo”);
-
字符串的常用方法
设的常量声明和初始化
String s1 = " ABcdABcd123 ";
String s2 = “ABcdEFg”;
String s3,s4,s5,s6;
int i1, i2, i3, i4;
boolean b1, b2, b3, b4;
char char1, char2, char3, char4; -
字符串长度和空判断
int length(); //返回此字符串的长度
boolean isEmpty(); // 判断字符串是否为空,如果字符串的length()为0,则返回true。 -
获取字符串/截取子字符串
char chatAt(int index); //返回指定索引处的char值
String substring(int beginIndex); //截取子字符串:从索引beginIndex到结束。
String substring(int beginIndex, int endIndex ); //截取子字符串:从索引beginIndex到endIndex.
索引编号从0开始,0<=索引<length() - 1; -
大小写转换
String toLowerCase(); //字符串转换为小写
String toUpperCase(); //字符串转换为大写 -
连接字符转
String concat(String str); // 将指定字符串连接到此字符串的结尾
也可使用运算符“+”实现字符串连接
s4 = s1 + “XYZ”
*取出字符串前后的空白
String concat(String str); ///删除字符串前后的所有空格 -
比较字符串的大小
boolean equals(Object anObject); //正常方式的比较
Boolean equalsIgnoreCase(String anotherString); //比较大小,不考虑大小写
int compareTo(String anotherString); //按字典顺序比较大小
int compareToIgnoreCase(String str); // 按字典顺序比较大小,不考虑大小写 -
查找字符/字符串
int indexOf(int ch); //查找指定字符在字符串中的第一个匹配项的索引位置
int indexOf(String str); //查找指定字符串在字符串中的第一个匹配项的索引位置
int indexOf(int ch, int formIndex ); //从指定索引开始查找指定字符
int indexOf(String str, int formIndex ); //从指定索引开始查找指定字符串 -
替换字符/字符串
String replace(char oldChar, char newchar); //将字符串中的指定字符oldChar替换为 newchar; -
拷贝整个数组给dst数组
void getChars(char dst[], int dstBegin) {
System.arraycopy(value, 0, dst, dstBegin, value.length);} -
格式化字符串
static String format(String format , Object…args); //使用指定的格式字符串和参数格式化字符串 -
字符串和字符数组之间的转换
char[] toCharArray(); //将字符串转化为一个新的字符数组
byte[] getBytes(); //将此字符串转化为byte数组
-
StringBuilder类和StringBuffer类
- 当对字符串进行修改的时候,需要使用 StringBuffer 和 StringBuilder 类。
和 String 类不同的是,StringBuffer 和 StringBuilder 类的对象能够被多次的修改,并且不产生新的未使用对象。
StringBuilder 的方法不是线程安全的(不能同步访问)。在应用程序要求线程安全的情况下,则必须使用 StringBuffer 类。
建议在涉及大量字符串操作时使用StringBuffer。
public class StringBuffer {
public static void main(String[] args) {
String str1 = "abcde";
String str2 = "abcde";
System.out.println(Integer.toHexString(str1.hashCode()));//打印地址
str1 = str1 + "fg";
System.out.println(Integer.toHexString(str1.hashCode()));
System.out.println("-----------------");
java.lang.StringBuffer stringBuffer = new java.lang.StringBuffer("hello");
System.out.println(stringBuffer);
System.out.println(Integer.toHexString(stringBuffer.hashCode()));
stringBuffer.append("world");//追加
System.out.println(stringBuffer);
System.out.println(Integer.toHexString(stringBuffer.hashCode()));
}
}
运行结果:
584f463
b8197464
————————————
hello
154617c
helloworld
154617c
StringBuffer的常用方法
a、append方法
public StringBuffer append(boolean b)
该方法的作用是追加内容到当前StringBuffer对象的末尾,类似于字符串的连接。调用该方法以后,StringBuffer对象的内容也发生改变,例如:
StringBuffer sb = new StringBuffer(“abc”);
sb.append(true);
则对象sb的值将变成”abctrue”。
使用该方法进行字符串的连接,将比String更加节约内容。
b、deleteCharAt方法
public StringBuffer deleteCharAt(int index)
该方法的作用是删除指定位置的字符,然后将剩余的内容形成新的字符串。
StringBuffer sb = new StringBuffer(“Test”);
sb. deleteCharAt(1);
public StringBuffer delete(int start,int end)
该方法的作用是删除指定区间以内的所有字符,包含start,不包含end索引值的区间。
StringBuffer sb = new StringBuffer(“TestString”);
sb. delete (1,4);
该代码的作用是删除索引值1(包括)到索引值4(不包括)之间的所有字符,剩余的字符形成新的字符串。则对象sb的值是”TString”
c、insert方法
public StringBuffer insert(int offset, boolean b)
该方法的作用是在StringBuffer对象中插入内容,然后形成新的字符串。
tringBuffer sb = new StringBuffer(“TestString”);
sb.insert(4,true);
该示例代码的作用是在对象sb的索引值4的位置插入true值,形成新的字符串,则执行以后对象sb的值是”TestyrueString”。
d、reverse方法
public StringBuffer reverse()
该方法的作用是将StringBuffer对象中的内容反转,然后形成新的字符串。
sb.reverse();
转以后对象sb中的内容将变为”cba” .
e、setCharAt方法
public void setCharAt(int index, char ch)
该方法的作用是修改对象中索引值为index位置的字符为新的字符ch
StringBuffer sb = new StringBuffer(“abc”);
sb.setCharAt(1,’D’);
对象sb的值将变成”aDc”。
StringBuilder类和StringBuffer类功能基本相似,方法也差不多,主要区别在于StringBuffer类的方法是多线程安全的,而StringBuilder是StringBuffer的一个简易替换,用在字符串缓冲区被单个线程使用的时候,不是线程安全的,相比Buffer而言,StringBuilder具有更高的性能,会略微快一点。
总结
线程安全:
StringBuffer:线程安全
StringBuilder:线程不安全
使用环境:
操作少量的数据使用 String;
单线程操作大量数据使用 StringBuilder;
多线程操作大量数据使用 StringBuffer。
例子1:
public static void main1(String[] args) {
String str1 = "hello";
String str2 = new String("hello");
String str3 = "he"+"llo";//"hello"
String str4 = new String("he")+new String("llo");
System.out.println(str1 == str2);
System.out.println(str3 == str1);
System.out.println(str3 == str2);
System.out.println(str4 == str3);
}
}
例子2:
public static void main(String[] args) {
String str1 = "hello";
String str2 = "world";
String str3 = "helloworld";
System.out.println(str1+str2);
System.out.println(str3);
System.out.println(str3 == (str1+str2));
System.out.println(str3 == ("hello"+"world"));
}
例子3、
public static void main(String[] args) {
String str1 = new String("hello");
String str2 = "hello";
System.out.println(str1 == str2);//false
String str3 = "he"+new String("llo");
System.out.println(str1 == str3);
System.out.println(str2 == str3);
String str4 = "he"+"llo";
System.out.println(str4 == str2);
char[] array = {'h','e','l','l','o'};
String str5 = new String(array);
System.out.println("=============");
System.out.println(str1 == str5);
System.out.println(str2 == str5);
System.out.println(str3 == str5);
System.out.println(str4 == str5);
}
例子4:
public static void main(String[] args) {
String str1 = "abcde";
String str2 = "abcde";
System.out.println(Integer.toHexString(str1.hashCode()));
str1 = str1+"fg";
System.out.println(Integer.toHexString(str1.hashCode()));
System.out.println("===========================");
StringBuffer stringBuffer = new StringBuffer("hello");
System.out.println(stringBuffer);
System.out.println(Integer.toHexString(stringBuffer.hashCode()));
stringBuffer.append("world");//追加
System.out.println(stringBuffer);
System.out.println(Integer.toHexString(stringBuffer.hashCode()));
System.out.println("=============stringBuilder==============");
StringBuilder stringBuilder = new StringBuilder(xigong");/xigongworld"
System.out.println(stringBuilder);
System.out.println(Integer.toHexString(stringBuilder.hashCode()));
stringBuilder.append("world");//追加
System.out.println(stringBuilder);
System.out.println(Integer.toHexString(stringBuilder.hashCode()));
}
#### 例子5、
public static void main(String[] args) {
String str1 = "abcde";
String str2 = "hello";
String str3 = str1+str2+"world";
System.out.println(str3);
}
intern()方法
intern() 方法返回字符串对象的规范化表示形式。
它遵循以下规则:对于任意两个字符串 s 和 t,当且仅当 s.equals(t) 为 true 时,s.intern() == t.intern() 才为 true。
语法
public String intern()
参数:无
返回值:一个字符串,内容与此字符串相同,但一定取自具有唯一字符串的池。
public static void main(String[] args) {
String str1 = new String("ab")+new String("cdef");
str1.intern();
String str2 = "abcdef";
str1.intern();
System.out.println(str1 == str2);
}
当调用 str1.intern()时,在常量池中没有"abcdef"这个常量,所以,此时就会在常量池中生成堆中对象的引用,所以,常量池的引用和str1的引用的地址一样。
拷贝:
public String substring(int beginIndex, int endIndex)
public static void main(String[] args) {
char[] array = {'a','b','c','d','e'};
String str1 = new String(array,2,3);
System.out.println(str1);//cde
String str2 = "abcdef";
String str3 = str2.substring(0,3);//不包含3号下标
System.out.println(str3);