JAVA learning record (2) Basic syntax of String

First, the String class

The String class is in the java.lang package. Java uses the String class to create a string variable, which belongs to the object. Java final class declared by String class can not have class. String objects cannot be modified after they are created, and consist of 0 or more characters, enclosed in a pair of double quotes.

Second, the creation of String objects

String declaration: String stringName;
String creation: stringName = new String (string constant); or stringName = string constant;

Three, String class construction method

1. The public String ()
parameterless construction method is used to create a String object with an empty string.

1 String str1 = new String():

2. public String (String value)
creates a String object with a known string value.

1 String str2 = new String("asdf"); 2 String str3 = new String(str2); 

3. public String (char [] value)
creates a String object with the character array value.

1 char[] value = {"a","b","c","d"};
2 String str4 = new String(value);//相当于String str4 = new String("abcd");

**
4, public String (char chars [], int startIndex, int numChars) **
Create a String object with numChars characters starting from the startIndex of the character array chars.

1 char[] value = {"a","b","c","d"};
2 String str5 = new String(value, 1, 2);//相当于String str5 = new String("bc");

5. Public String (byte [] values)
creates a String object with the bit array values.

1 byte[] strb = new byte[]{65,66};
2 String str6 = new String(strb);//相当于String str6 = new String("AB");

Four, String class commonly used methods

1. Introduction to the string
1) The string definition only defines that no memory space is allocated, and no operation is performed; the
two methods of string initialization are directly equal sign assignment, initialized with new, direct equal sign assignment is placed into the memory pool, other variables can Reference; new initializes the allocated memory space, which cannot be referenced; the
string assignment is initialized with null, and there is a reference, but it does not point to any memory space

2. Find the length of the string
public int length () // return the length of the string

1 String str = new String("asdfzxc");
2 int strlength = str.length();//strlength = 7

3. Find the character at a certain position in the string
public char charAt (int index) // Return the character at the specified position in the string; note that the first character index in the string is 0, and the last one is length ()-1

1 String str = new String("asdfzxc");
2 char ch = str.charAt(4);//ch = z

4. String comparison
1) public int compareTo (String anotherString) // This method is to compare the size of the string content in lexicographic order, and indicate the size relationship between the current string and the parameter string through the returned integer value. If the current object is larger than the parameter, it returns a positive integer, otherwise it returns a negative integer, and equals 0.
2) public int compareToIgnore (String anotherString) // Similar to compareTo method, but ignore case.
3) public boolean equals (Object anotherObject) // Compare the current string and the parameter string, return true when the two strings are equal, otherwise return false.
4) public boolean equalsIgnoreCase (String anotherString) // Similar to the equals method, but ignore case.

1 String str1 = new String("abc");
2 String str2 = new String("ABC");
3 int a = str1.compareTo(str2);//a>0
4 int b = str1.compareTo(str2);//b=0
5 boolean c = str1.equals(str2);//c=false
6 boolean d = str1.equalsIgnoreCase(str2);//d=true

5) The equals method is inherited by the String class from its superclass Object, and is used to detect whether two objects are equal, that is, whether the contents of the two objects are equal, case-sensitive.

For string variables, when using the "==" and "equals ()" methods to compare strings, the comparison method is different. "==" compares the values ​​of the two variables themselves, ie the first addresses of the two objects in memory. "Equals ()" compares whether the content contained in the string is the same. E.g:

s1 = new String("abc");
s2 = new String("abc");
s1.equals(s2) 是 true

3. For non-string variables, the function of "==" and "equals" are the same. They are used to compare the first address of the object in the heap memory, that is, to compare whether two reference variables point to the same Object.

class A
{
      A obj1   =   new  A();
      A obj2   =   new  A();
}
    obj1.equals(obj2)是false

5. String connection
public String concat (String str) // Connect the string str in the parameter to the back of the current string, the effect is equivalent to "+".

1 String str = "aa".concat("bb").concat("cc");
2 相当于String str = "aa"+"bb"+"cc";
Published 12 original articles · Liked3 · Visits 407

Guess you like

Origin blog.csdn.net/Hobo_hua/article/details/82891036