Personal Note Series JAVA Basics (String String)

java.lang -- String String:

Features : Once the string is initialized, it cannot be changed and is stored in the constant pool in the method area. Any data enclosed in double quotes is a string object.

------------------------------------------------------

String s1 = "abc"; // There is only one object abc in the memory pointed to by s1 .

String s2 = new String("abc"); // There are two objects abc and new in the content pointed to by s2 .

System.out.println(s1==s2);//false

System.out.println(s1.equals(s2));//true , equals in the string compares whether the contents of the strings are the same.

-------------------------------------------------------

String method:

1 : Construction method: Convert a byte array or character array into a string.

String s1 = new String();// Creates an empty string.

String s2 = null;//s2 does not point to any object and is a null constant value.

String s3 = "";//s3 points to a specific string object, but there is no content in this string.

// Generally, new is not used when defining strings .

String s4 = new String("abc");

String s5 = "abc"; generally use this writing

new String(char[]);// Convert the character array into a string.

new String(char[],offset,count);// Convert part of the character array into a string.

2 : General method:

According to object-oriented thinking:

2.1 Get:

2.1.2 : The character at the specified position. char charAt (int index);

2.1.3 : Get the position of the specified character. If it does not exist, return -1 , so you can judge whether a character does not exist by returning the value -1 .

int indexOf (int ch);// Return the first found character index

int indexOf(int ch,int fromIndex); // Returns the index found for the first time from the specified position

int indexOf(String str); // Return the first found string index

int indexOf(String str,int fromIndex);

 

int lastIndexOf (int ch);//  Returns the index of the last occurrence of the specified character in this string.

int lastIndexOf(int ch,int fromIndex);

int lastIndexOf(String str);

int lastIndexOf(String str,int fromIndex);

2.1.4 : Get the substring.

String substring (int start);// Start from the start bit and end at length ()-1 .

String substring(int start,int end);// From start to end . // Include the start bit, not the end bit.

substring(0,str.length());// Get the whole string

2.2 Judgment:

2.2.1 : Does the string contain the specified string?

boolean contains(String substring);

2.2.2 : Does the string start with the specified string?

boolean startsWith(string);

2.2.3 : Does the string end with the specified string?

boolean endsWith(string);

2.2.5 : Determine whether the contents of the strings are the same, ignoring case.

boolean equalsIgnoreCase(string) ;

 

2.3 Conversion:

2.3.1 : The character array or byte array can be converted into a string through the constructor.

2.3.2 : You can convert a character array into a string through the static method in the string.

static String copyValueOf(char[] );

static String copyValueOf(char[],int offset,int count);

static String valueOf(char[]);

static String valueOf(char[],int offset,int count);

2.3.3 : Convert basic data types or objects into strings.

static String valueOf(char);

static String valueOf(boolean);

static String valueOf(double);

static String valueOf(float);

static String valueOf(int);

static String valueOf(long);

static String valueOf(Object);

2.3.4 : Convert strings to uppercase and lowercase.

String toLowerCase();

String toUpperCase (); All characters are converted to uppercase.

2.3.5 : Convert the string to an array.

char[] toCharArray ();// Convert to character array.

byte[] getBytes ();// The encoding table can be added. Convert to byte array.

2.3.6 : Convert a string to an array of strings. cutting method.

String[] split ( split rule - string );

2.3.7 : Replace the content of the string. Note: After modification, it becomes a new string, not directly modifying the original string.

String replace(oldChar,newChar);

String replace(oldstring,newstring);

2.3.8 : String concat (string); // Append the string.

String trim ();// Remove spaces at both ends of the string

int compareTo ();// If the parameter string is equal to this string, it returns a value of 0 ; if this string is lexicographically less than the string parameter, it returns a value less than 0 ; if this string is lexicographically greater than the character String parameter, returns a value greater than 0 .

------------------------------------------------------------------------------------------------

--< java.lang >-- StringBuffer String buffer: ★★★☆

Constructs a string buffer with no characters in it, with an initial capacity of 16 characters.

Features:

1 : The content of the string can be modified.

2 : is a container.

3 : is variable length.

4 : Any type of data can be stored in the buffer.

5 : eventually needs to be turned into a string.

Containers usually have some fixed methods:

1 , add.

StringBuffer append (data): Append data to the buffer. Append to the end.

StringBuffer insert (index, data): Insert data at the specified position.

2 , delete.

StringBuffer delete (start,end); delete elements from start to end-1

StringBuffer deleteCharAt(index); delete the element at the specified position

//sb.delete(0,sb.length());//Empty the buffer.

3 , modify.

StringBuffer replace (start,end,string); replace start to end-1 with string

void setCharAt (index, char); replace the character at the specified position

void setLength (len); set the original string to a string of the specified length

4 , find. (can't find return -1 )

int indexOf (string); Returns the index of the first occurrence of the specified substring in this string.

int indexOf(string,int fromIndex); find the string starting from the specified position

int lastIndexOf (string); Returns the index of the rightmost occurrence of the specified substring in this string.

int lastIndexOf(string,int fromIndex); start the reverse search from the specified index

5 , get the substring.

string substring (start); returns the substring from start to end

string substring(start,end); returns the substring from start to end-1

6 , reverse.

StringBuffer reverse (); string reverse

--< java.lang >-- StringBuilder string buffer: ★★★☆

StringBuiler appears in JDK1.5 ; constructs a string generator without characters, with an initial capacity of 16 characters. This class is designed to be used as a drop-in replacement for StringBuffer when the string buffer is used by a single thread (which is common).

The method is the same as StringBuffer ;

The difference between StringBuffer and StringBuilder :

StringBuffer is thread safe.

StringBuilder is not thread safe.

Single- threaded operation, using StringBuilder is efficient.

Multithreaded operation, safe to use StringBuffer .

---------------------------------------------------------

StringBuilder sb = new StringBuilder("abcdefg");

sb.append("ak");  //abcdefgak

sb.insert(1,"et");//aetbcdefg

sb.deleteCharAt(2);//abdefg

sb.delete(2,4);//abefg

sb.setLength(4);//abcd

sb.setCharAt(0,'k');//kbcdefg

sb.replace(0,2,"hhhh");//hhhhcdefg

// To use the buffer, you must first create an object.

StringBuffer sb = new StringBuffer();

sb.append(12).append("haha");// Method call chain.

String s = "abc"+4+'q';

s = new StringBuffer().append("abc").append(4).append('q').toString();

 

String str=”sda”;

StringBuffer st=new StringBuffer(“sda”);

System.out.println(str.equals(st));

The print result is  : false   The reason is a different type

---------------------------------------------------------

class  Test{

public static void main(String[] args) {

String s1 = "java";

String s2 = "hello";

method_1(s1,s2);

System.out.println(s1+"...."+s2); //java....hello

StringBuilder s11 = new StringBuilder("java");

StringBuilder s22 = new StringBuilder("hello");

method_2(s11,s22);

System.out.println(s11+"-----"+s22); //javahello-----hello

}

public static void method_1(String s1,String s2){

s1.replace('a','k');

s1 = s2;

}

public static void method_2(StringBuilder s1,StringBuilder s2){

s1.append(s2);

s1 = s2;

}

}

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325389080&siteId=291194637