java basics - strings

1. String

1. Final modification, constant, length cannot be changed. The content cannot be changed, but the pointer can be changed

String s1 = "abc";

String s1 = "bcd";

There is no error, just s1 points to another place, the string "abc" has not changed.

2. Memory

String s = "abc";//constant area

String s2 = new String("abc"); //constant area, memory area

3.== and equals()

== compares numbers and addresses

The equals() of the Object class compares the address by default, and the String class overrides the equals method to compare the content.

4. Methods involving subscripts, including headers and not tails

String s = "abc";

s.subString(start,end) :[  )

5. Method

(1) Cut split(str) according to regular expressions

 Regular expressions are a knowledge point

(2) String subString()

(3) Remove the spaces at both ends trim()

(4) Compare compareTo(str), the default is dictionary order

(5) Convert (list your most commonly used)

 a. String and character array String---char[]:

 String s = new String(char[]);

 char[] c = s.toCharArray();

b. String and byte array String---byte[]:

String s = new String(byte[]);

byte[] c = s.getBytes();

When converting a string to a byte array, you can specify the encoding: String s = new String(str.getBytes("GBK"),"GBK");

c. Strings and basic types

String s = 1+""; 

int a = Integer.parseInt("1");

. . . There are a lot more.

6. Encoding/Decoding

String s = "abc";

byte[] c = s.getBytes() //encoding

s = new String(c) //decode

 Make sure that the encoding and decoding use the same encoding format. GBK is used by default

String s = new String(str.getBytes("GBK"),"GBK");

二、StringBuffer


三、StringBuilder



Guess you like

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