Common JavaSE-JDK classes - String Class

1.String class

1.1 String class

public final class String
implements java.io.Serializable, Comparable<String>, CharSequence {
/** The value is used for character storage. */
private final char value[];
/** Cache the hash code for the string */
private int hash; // Default to 0
  • String class: the representative of the string. All string literals (eg "abc") Java programs are implemented as an instance of this class.
  • String is a final class, the representative character sequences immutable.
  • Strings are constants, represented by double quotes. Their value can not be changed after creation.
  • Character String object content is stored in a character array value [] in.
String s1 = "abc";//字面量的定义方式
String s2 = "abc";
s1 = "hello";

Here Insert Picture Description

Creating 1.2 String object

String str = "hello";
//本质上this.value = new char[0];
String s1 = new String(); 
//this.value = original.value;
String s2 = new String(String original); 
//this.value = Arrays.copyOf(value, value.length);
String s3 = new String(char[] a); 
String s4 = new String(char[] a,int startIndex,int count);

Here Insert Picture Description
Here Insert Picture Description
Q: String str1 = "abc" ; distinction; and String str2 = new String ( "abc ")?
A: string constant pool is stored in a string constant, a shared object
strings is stored in an amount of heap objects.
Here Insert Picture Description

String s1 = "hello";
        String s2 = "world";
        String s3 = "hello" + "world";
        String s4 = s1 + "world";
        String s5 = s1 + s2;
        String s6 = (s1 + s2).intern();
        System.out.println(s3 == s4);//false
        System.out.println(s3 == s5);//false
        System.out.println(s4 == s5);//false
        System.out.println(s3 == s6);//true
  • Splicing results in constant and constant constant pool. And the constant pool constants of the same content will not exist.
  • As long as one of them is variable, result in a heap
  • If the result of splicing call intern () method returns the value in the constant pool
    use traps String:
  • String s1 = "a";
    Description: string constant pool created in a literal is "a" string.
  • s1 = s1 + "b";
    described: in fact the original "a" String object has been discarded, now produces a string s1 + "b" (i.e. "ab") in the heap space. If these changes strings content multiple times, will lead to a large number of copies remaining string object in memory, reducing efficiency. If such an operation into circulation, will greatly affect the performance of the program.
  • String s2 = "ab";
    Description: to create a literal "ab" string inside a string constant pool.
  • String s3 = "a" + " b";
    Description: s3 point "ab" string string constant pool has been created.
  • String s4 = s1.intern ();
    Description: s1 object heap space after calling intern (), will be constant pool already existing "ab" character string
    assigned to s4.
String s = "0";
for(int i=1 ;i<=5;i++){
	s +=i;
}
System.out.println(s);

Here Insert Picture Description

String s = new String("0");
for(int i=1 ;i<=5;i++){
	s +=i;
}
System.out.println(s);

Here Insert Picture Description

Class common method 2.String

int length():返回字符串的长度: return value.length
 char charAt(int index): 返回某索引处的字符return value[index] 
 boolean isEmpty():判断是否是空字符串:return value.length == 0 
  String toLowerCase():使用默认语言环境,将 String 中的所有字符转换为小写
 String toUpperCase():使用默认语言环境,将 String 中的所有字符转换为大写
 String trim():返回字符串的副本,忽略前导空白和尾部空白
 boolean equals(Object obj):比较字符串的内容是否相同
 boolean equalsIgnoreCase(String anotherString):与equals方法类似,忽略大小写
 String concat(String str):将指定字符串连接到此字符串的结尾。 等价于用“+int compareTo(String anotherString):比较两个字符串的大小
 String substring(int beginIndex):返回一个新的字符串,它是此字符串的从
beginIndex开始截取到最后的一个子字符串。 
 String substring(int beginIndex, int endIndex) :返回一个新字符串,它是此字符串从beginIndex开始截取到endIndex(不包含)的一个子字符串。
boolean endsWith(String suffix):测试此字符串是否以指定的后缀结束
boolean startsWith(String prefix):测试此字符串是否以指定的前缀开始
boolean startsWith(String prefix, int toffset):测试此字符串从指定索引开始的子字符串是否以指定前缀开始
boolean contains(CharSequence s):当且仅当此字符串包含指定的 char 值序列时,返回 true
 int indexOf(String str):返回指定子字符串在此字符串中第一次出现处的索引
 int indexOf(String str, int fromIndex):返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始
 int lastIndexOf(String str):返回指定子字符串在此字符串中最右边出现处的索引
 int lastIndexOf(String str, int fromIndex):返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索
注:indexOf和lastIndexOf方法如果未找到都是返回-1
String replace(char oldChar, char newChar):返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。 
String replace(CharSequence target, CharSequence replacement):使用指定的字面值替换序列替换此字符串所有匹配字面值目标序列的子字符串。 
String replaceAll(String regex, String replacement) : 使 用 给 定 的replacement 替换此字符串所有匹配给定的正则表达式的子字符串。 
String replaceFirst(String regex, String replacement) : 使 用 给 定 的replacement 替换此字符串匹配给定的正则表达式的第一个子字符串。 
boolean matches(String regex):告知此字符串是否匹配给定的正则表达式。 
String[] split(String regex):根据给定正则表达式的匹配拆分此字符串。 
String[] split(String regex, int limit):根据匹配给定的正则表达式来拆分此字符串,最多不超过limit个,如果超过了,剩下的全部都放到最后一个元素中。

3.String the basic data types, character array, the byte array into

3.1 String basic data types into

  • String -> basic data types, packaging
    public static Integer wrapper class int parseInt (String s): may be converted by the character string "digital" character is an integer.
    Similarly, using the java.lang package Byte, Short, Long, Float, Double tone corresponding class by class methods string "number" characters, converted to the corresponding basic data types.
  • Basic data types, Packaging -> String
    call String class public String valueOf (int n) can be converted to type int string
    corresponding valueOf (byte b), valueOf ( long l), valueOf (float f), valueOf (double d), corresponding to the character string type valueOf (boolean b) by conversion parameters

3.2 String array of characters and conversion

  • Character array -> String
    String class constructor: String (char []) and String (char [], int offset , int length) are all used to create a string object character and the character array portion of the character.
  • String -> character array
    public char [] toCharArray (): a method in a character array to store all the characters in the string.
    public void getChars (int srcBegin, int srcEnd, char [] dst, int dstBegin): provides a method for indexing a string in the specified range stored in the array.

3.2 String conversion and bytes

  • Byte array -> String
    String (byte []): default character set specified by the decoded byte array platform used to construct a new String.
    String (byte [], int offset , int length): with a portion of the specified byte array, i.e., length bytes starting at offset take objects from an array configuration of a string starting position.
  • String -> byte array
    public byte [] getBytes (): using the platform's default character set encoding is byte String this sequence, storing the result into a new byte array.
    public byte [] getBytes (String charsetName ): using the specified character coding to set this String byte sequence, and storing the result into a new byte array.

4.StringBuffer class

  • Java.lang.StringBuffer variable representing a sequence of characters, JDK1.0 declared, you can add or delete the contents of the string, then no new object.
  • Many ways the same as the String.
  • Passed as a parameter, the method can change the value inside.
  • Unlike StringBuffer class String, which is configured to generate the object must be used. There are three constructors:
    the StringBuffer (): an initial capacity of the string buffer 16
    StringBuffer (int size): Specifies the configuration string buffer capacity
    StringBuffer (String str): The contents of the initialization string specified content

4.1 StringBuffer class common method

StringBuffer append(xxx):提供了很多的append()方法,用于进行字符串拼接
StringBuffer delete(int start,int end):删除指定位置的内容
StringBuffer replace(int start, int end, String str):把[start,end)位置替换为str
StringBuffer insert(int offset, xxx):在指定位置插入xxx
StringBuffer reverse() :把当前字符序列逆转

When the append and insert, if the original length of the array is not enough value, expandable.
These methods described above chain operation supporting method.

public int indexOf(String str)
public String substring(int start,int end)
public int length()
public char charAt(int n )
public void setCharAt(int n ,char ch)

5.StringBuilder class

  • StringBuilder and StringBuffer very similar, represents a variable sequence of characters, but also provide related functions the same way
  • Interview questions: Comparative String, the StringBuffer, the StringBuilder
    String (JDK1.0): immutable sequence of characters
    StringBuffer (JDK1.0): a variable sequence of characters, inefficiency, security thread
    StringBuilder (JDK 5.0): a variable sequence of characters, high efficiency thread unsafe
    Note: passed as a parameter, then the method will not change its value inside String, StringBuffer and StringBuilder
    will change its value

5.1 StringBuilder class common method

Published 337 original articles · won praise 77 · views 570 000 +

Guess you like

Origin blog.csdn.net/feicongcong/article/details/104893813
Recommended