Java中的String
平时的编程中,我们能清晰的体会到 ,字符串操作在编程中随处可见 。
不可变的String
首先,我们要明确的问题就是Java里面的String是不可变的。String类中的每一个看起来修改String值的方法,实际上都是创建了一个全新的String对象引用,以包含修改后的字符串的内容。
<span style="white-space:pre"> </span>public static void main(String[] args) {
String hello = "Hello";
System.out.println(hello);
System.out.println(hello.hashCode());
hello += " Java String";
System.out.println(hello);
System.out.println(hello.hashCode());
hello = hello.toUpperCase();
System.out.println(hello);
System.out.println(hello.hashCode());
}
/**
* 输出
* Hello 69609650
* Hello Java String -1069039263
* HELLO JAVA STRING 919675233
*/
上面的代码输出说明,每次对String对象的值进行改变的时候,都创建了一个新的String对象,而不是修改原来的String对象的值,回到了 我们的第一个要点
String是不可变的。
另外还需要注意的地方是 Java的 字符串是放在常量池里面的,当你第已经创建了一个字符串常量的时候 ,如果再创建和原来字符串值相同的String对象,其实只是对原String对象的一个引用。
看下面的这段代码:
<span style="white-space:pre"> </span>String helloAgain = "Hello Again";
System.out.println(helloAgain.hashCode());
String helloAgain1 = "Hello Again";
System.out.println(helloAgain1.hashCode());
String helloAgain2 = helloAgain1;
System.out.println(helloAgain2.hashCode());
输出
-883117486
-883117486
-883117486
String 上的一些操作常用方法
Method 方法 | Arguments, Overloading 参数 , 重载版本 | Use 应用 |
Constructor | Overloaded: default, String, StringBuilder, StringBuffer, char arrays, byte arrays. | Creating String objects. |
length( ) | Number of characters in the String. String中字符的个数 | |
charAt( ) | int Index Int索引 | The char at a location in the String. 取得该位置上的char |
getChars( ), getBytes( ) | The beginning and end from which to copy, the array to copy into, an index into the destination array. 要复制部分的起点和终 点的索引, 复制的目标数组,目标数组的起始索引 | Copy chars or bytes into an external array. 复制char, byte到一个目标数组中 |
toCharArray( ) | Produces a char[] containing the characters in the String. 生成一个char[],包含所有的字符 | |
equals( ), equalsIgnoreCase( ) | A String to compare with. | An equality check on the contents of the two Strings. |
compareTo( ) | A String to compare with. | Result is negative, zero, or positive depending on the lexicographical ordering of the String and the argument. Uppercase and lowercase are not equal! 按照词典顺序比较String的内容,比较结果为负数,正数,0.注意,大小写并不等价。 |
contains( ) | A CharSequence to search for. 要搜索的CharSequence | Result is true if the argument is contained in the String. 如果String对象包含参数内容,返回true |
contentEquals( ) | A CharSequence or StringBuffer to compare to. | Result is true if there’s an exact match with the argument. |
equalsIgnoreCase( ) | A String to compare with. | Result is true if the contents are equal, ignoring case. |
regionMatches( ) | Offset into this String, the other String and its offset and length to compare. Overload adds "ignore case." | boolean result indicates whether the region matches. |
startsWith( ) | String that it might start with. Overload adds offset into argument. | boolean result indicates whether the Stringstarts with the argument. |
endsWith( ) | String that might be a suffix of this String. | boolean result indicates whether the argument is a suffix. |
indexOf( ), lastIndexOf( ) | Overloaded: char, char and starting index, String, | Returns -1 if the argument is not found within this String; otherwise, returns |
substring( ) (also subSequence( )) | Overloaded: starting index; starting index + ending index. | Returns a new String object containing the specified character set. |
concat( ) | The String to concatenate. | Returns a new String object containing the original String’s characters followed by the characters in the argument. |
replace() | The old character to search for, the new character to replace it with. Can also replace a CharSequence with a CharSequence. | Returns a new String object with the replacements made. Uses the old String if no match is found. |
toLowerCase( ) toUpperCase( ) | Returns a new String object with the case of all letters changed. Uses the old String if no changes need to be made. | |
trim( ) | Returns a new String object with the whitespace removed from each end. Uses the old String if no changes need to be made. | |
valueOf( ) | Overloaded: Object, char[], char[] and offset and count, boolean, char, int, long, float, double. | Returns a String containing a character representation of the argument. |
intern( ) | Produces one and only one String reference per unique character sequence. |
StringBuilder 类
通过上面的内容我们似乎也看出来了String的弊端,如果直接使用String来进行涉及到字符串值改变的操作会产生大量的中间过程,从而产生性能上的问题。
虽然 StringBuilder 对象是动态对象,允许扩充它所封装的字符串中字符的数量,但是您可以为它可容纳的最大字符数指定一个值,此值称为该对象的容量。
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
Random random = new Random(47);
for (int i = 0; i < 25; i++) {
builder.append(random.nextInt(100));
builder.append(",");
}
return builder.toString();
}
public static void main(String[] args) {
UsingStringBulider usb = new UsingStringBulider();
System.out.println(usb);
}
// 输出 58,55,93,61,61,29,68,0,22,7,88,28,51,89,9,78,98,61,20,58,16,40,11,22,4,
未完待续