Java basic notes-String class, java notes-string
String class (modified by final)
A string is a special object. Once the string is initialized, it cannot be changed (the content remains unchanged)
For example:
String s = "abc ";
String s1 = new String ("abc ");
S has an object in the memory. s represents a class type variable, and "abc" is an object.
S1 has two objects in the memory, new and abc ".
S = s1; the result is false. because s and s1 have different addresses, the address values stored in s and s1 are different. therefore, it is false.
S. equals (s1); the result is true. equals is to compare whether the content of the two objects is the same,
"=" And equals methods:
= The operator is used to compare whether the values of two variables are equal, that is, to compare whether the values stored in the memory corresponding to the variables are the same, to compare two basic types of data or whether two referenced variables are equal, you can only use the = operation.
Character.
If the data that a variable points to is of the object type, two pieces of memory are involved. The object occupies one piece of memory (heap memory) and the variable also occupies one piece of memory, in Objet obj = new Object (); the variable obj is a memory, and the new Object () is another memory. At this time, the value stored in the memory corresponding to the variable obj is the memory occupied by the object.First address.For a variable pointing to the object type, to compare whether two variables point to the same object, you must check whether the values in the memory corresponding to the two variables are equal, at this time, we need to use the = Operator for comparison.
The equals method is used for comparison.Two independent objectsWhether the content is the same. The two objects it compares are independent.
String a = new String ("abc ");
String B = new String ("abc ");
Two new statements create two objects, and use the variables a and B to point to one of them. These two objects have different first addresses, that is, the values (address values) stored in a and B are different. Therefore, expression a = B returns false, and the content of the two objects is the same, expression. equals (B) returns true.
The equals method in the Object class is rewritten in the String class to determine whether the strings are the same.
See Code: StringDemo. java
1 class StringDemo 2 {3 public static void main (String args []) 4 {5 String s = "abc"; // s is a class type variable. "abc" is an object 6 String s1 = new String ("abc"); // s1 contains two objects. 7 8 // s and s1: 9 // There is an object "abc" in s and a class type variable 10 // There are two objects in s1. they are new and "abc" 11 12 System. out. println (s = s1); // The output result is false13 System. out. println (s. equals (s1); // The output result is true.14 15} 16} 17 18
Common Operations in the String class.
1. Get operation.
1.1 obtain the number of characters in a string, that is, the length of the string.
Method return type: int length ()
Difference from the length in an array: In an array, the length attribute is. length.
In the string: the length () method is used.
1.2 obtain a character in the string based on its position.
The return type of the method is char CharAt (int index => badge). The parameter indicates the position of the character to be obtained.
1.3 obtain the position of the character in the string based on the character.
Method 1:
The return value type is int indexOf (int ch => the desired character.
Method 2:
Return Value Type: int indexOf (int ch, fromIndex) Obtain from fromIndex.
Obtain the position of the string in the string: return type: int indexOf (int ch)
Obtain the position of the string starting from fromIndex:
Return type: int method: indexOf (int ch, fromIndex)
Returns the position of a character in a string,
LastIndexOf (int ch, fromIndex)
2. Determine the operation.
2.1 whether the string contains a substring.
Return type: boolean method: contains (str );
IndexOf (str) can be used to index the location where the first index appears. If-1 is returned, it indicates that no index is found,
If (str. indexOf ("string ")! =-1) This method can be used to determine or obtain the location.
Whether there is content in the 2.2 characters.
Return type: boolean method: isEmpty (); judge whether the length is null.
2.3 whether the string starts with the specified content.
Return type: boolean method: startsWith (str );
2.4 whether the string ends with the specified content.
Return type: boolean method: endsWith (str );
2.5 determine whether the content of the string is the same
Return Value Type: boolean equals (str );
2.6 ignore the case and check whether the strings are the same.
EqualsIgnoreCase (str)
3. Conversion
3.1 convert the character array into a string.
Constructor: String (char [])
String (char [], offset, count): convert a part of the character array into a String.
Static Method:
Static String copyValueOf (char []);
Static String copyValueOf (char [] data, int offset, int count)
Static String valueOf (char []):
3.2 convert a string into a character array. (Important)
Return type: char [] method: toCharArray ():
3.3 convert the byte array into a string.
String (byte [])
String (byte [], offset, count): convert part of the byte array into a String.
3.4 convert a string into a byte array.
Return type: byte [] method: getBytes ():
3.5 convert the basic data type to a string
Static String valueOf (int)
Static String valueOf (double)
3 + ""; // String. valueOf (3); the results are the same, both of which are output String types. "3"
Special: encoding tables can be specified during conversion of strings and byte arrays.
4. Replace
String replace (oldchar, newchar );
5. Cutting
String [] split (regex );
6. substring to obtain part of the string.
String substring (begin );
String substring (begin, end );
7. Convert, remove spaces, and compare.
7.1 convert the string to uppercase or lowercase.
String toUpperCase ();
String toLowerCase ();
7.2 remove multiple spaces at both ends of the string.
String trim ();
7.3 compare the natural order of two strings.
Int compareTo (string );
Exercise instance code:
1 class StringFunctionDemo 2 {3 public static void main (String args []) 4 {5 // functionLength (); 6 // functionJudge (); 7 // functionTrans (); 8 // functionReplace (); 9 // functionSplit (); 10 functionSub (); 11 function_7 (); 12} 13 // remove spaces and convert them to uppercase or lowercase letters. output, compare the size of two strings. 14 public static void function_7 () 15 {16 String str = "Hello Java"; 17 printer (str. toLowerCase (); // convert all to lowercase letters and output 18 printer (str. toUpperCase (); // Convert all to uppercase letters and output 19 printer (str. trim (); // remove spaces on both sides of the string from the output string. 20 21 String str1 = "aba"; 22 String str2 = "aaa"; 23 24 printer (str1.compareTo (str2 )); // compare two strings in a natural order. A positive value indicates that the preceding string is large. the string following the negative number is large. 25} 26 // capture the substring in the string. 27 public static void functionSub () 28 {29 String str = "abcdef"; 30 31 printer (str. substring (2); // starts from the specified position to the end. If the badge does not exist, an exception occurs. 32 printer (str. substring (2, 4); // contains the header and does not contain the end str. substring (0, s. length (); 33} 34 35 36 // defines a method to convert a String to a String array 37 public static void functionSplit () 38 {39 String str = "zhagnsan, lisi, wangwu, qianliu "; 40 41 String [] arr = str. split (","); // converts a string to an array of character groups. 42 43 for (int x = 0; x <arr. length; x ++) 44 {45 printer ("String [] arr =" + arr [x]); 46} 47} 48 49 50 // defines the method for replacing String content 51 public static void functionReplace () 52 {53 String str = "hello java "; 54 55 // String str1 = s. replace ('Q', 'n'); // if the character to be replaced does not exist, the original string is returned. 56 57 String str1 = str. replace ("java", "world"); // replace "java" in the original string with "world ". 58 printer ("str =" + str); 59 printer ("str1 =" + str1 ); 60} 61 62 63 // defines the conversion method between a string and a character array 64 public static void functionTrans () 65 {66 char [] arr = new char [] {'A', 'B', 'C', 'D', 'E '}; 67 String str = new String (arr); 68 String str1 = new String (arr, 1, 3 ); // convert the last three elements starting with the element of the character starting from the 1-badge to the string 69 printer ("str =" + str); // convert the character array to the string. 70 printer ("str1 =" + str1); 71 72 String str2 = "abcdefg"; 73 char [] ch = str2.toCharArray (); // convert the string to the character array 74 for (int I = 0; I <ch. length; I ++) 75 {76 printer ("ch =" + ch [I]); // print out the character array. 77} 78} 79 // defines the method for determining the String. Operation 80 public static void functionJudge () 81 {82 String str = "HelloWorld. java "; 83 84 printer (str. contains ("java"); // determines whether to protect the string "java" 85 86 printer (str. isEmpty (); // determines whether the length is 0; 87 88 printer (str. startsWith ("Hello"); // determines whether the string starts with "Hello" and is 89 90 printer (str. endsWith (". java "); // determines if it is ". java files. 91} 92 93 // defines the method for obtaining the String position 94 public static void functionLength () 95 {96 97 String s = "abcdefgaqw"; 98 99 printer (s. length (); // The length of the output string. 100 101 printer (s. charAt (5); // when the input badge is out of the range, an exception occurs when the string badge is out of the range. 102 103 printer (s. indexOf ('A'); // obtain the position of the character 'a' in the string. this is the first location to appear. 104 105 printer (s. indexOf ('A', 2); // index a.106 107 printer (s. indexOf ('Z'); // if no value is found,-1 is returned. output result-1.108 109 printer (s. lastIndexOf ('C', 4); // The Position of the reverse index character in the string. 110 111} 112 113 114 115 public static void printer (Object obj) 116 {117 System. out. println (obj); 118} 119 120}