One, String class
A string is a sequence of characters.
1. Build a String
// Directly the string literal is treated as a string object. String message = "Weclome to JAVA"; // using character arrays Char [] Chararray = {' G ', ' o ', ' o ', ' d ', ' ', ' d ', ' a ', ' Y '}; New String (Chararray);
A string variable stores a reference to a string object that is stored in a string object.
2. Immutable strings and qualifying strings
The string object is immutable, and its contents cannot be changed.
String s = "Java"= "HTML"; // after the assignment, the "Java" string still exists and does not become "HTML", but is inaccessible because the s variable points to the new object.
Qualification (interned): Use the same instance for a string literal with the same string sequence.
New String (' Weclome to java '= ' weclome to java '= ' weclome to Java '; System.out.println ("S1 = = S2 is" + (S1 = = s2)); System.out.println ("S2 = = S3 is" + (S2 = = s3));
Results:
Falsetrue
= =: Detects whether two string variables point to the same object. Compares the addresses of two objects
3. Comparison of strings
equals (s1:string): Boolean |
If this string equals the string S1 returns True |
equalsignorecase (s1:string): Boolean |
If the case is not case-insensitive, this String equals string S1 returns True |
compareTo (s1:string): int |
returns an integer greater than 0, equal to 0, or less than 0 to indicate that the string is greater than, equal to or less than S1 |
comparetoignorecase (s1:string): int |
except case-insensitive, the others are the same as CompareTo |
regionmatched (Index:int, s1:string, S1index:int, Len:int): Boolean |
If this string specifies a subdomain that exactly matches the string s1 The specified subdomain returns true |
regionmatches (Index:int, s1:string, S1index:int, Len:int): Boolean |
except Not case sensitive, others are the same as regionmatched |
startwith (prefix:string): Boolean |
returns t if the string starts with the specified prefix Rue |
endwith (suffix:string): boolean |
Returns True if the string starts with the specified suffix |
Note: the = = operator can only detect whether two strings point to the same object and cannot determine whether the contents of the two strings are equal. The equals method should be used to determine whether the contents of two strings are equal.
string S1 = new String ("Weclome to Java" ); String s2 = "Weclome to Java" ; String s3 = "Weclome to Java" ; String S4 = "Weclome to C + +" ; System.out.println ( "S1 = = S2 is" + (S1 = = s2)); // false System.out.println ("S2 = = S3 is" + (S2 = = s3)); // true; System.out.println (" S1 equals S2 "+ s1.equals (S2)); // true System.out.println ("S2 equals S4" + s2.equals (S4)); // false
CompareTo method: S1.compareto (S2); If S1 is equal to S2, return 0, if the dictionary order (that is, the uniform encoding Order) S1 less than S2, the return value is less than 0, if the dictionary order S1 greater than S2, the return value is greater than 0.
1 New String ("abc"); 2 String s2 = "ABG"; 3 4 System.out.println (S1.compareto (S2)); //C-ratio g Small 4, so return-4
4. String lengths, characters, and combined strings
Length (): INT--The number of characters that return a string; Massage.length ()
CharAt (index:int): CHAR--Returns the character of the subscript specified in this string Massage.charat (0)
Concat (s1:string): string-Returns a new string consisting of the string and the string S1. S3 = S1.concat (s2); equivalent to: s3 = s1 + s2;
Length is a method of the string class, but it is a property of an array object. Gets the number of characters in a string object with S.length (); Gets the number of elements of array A with a.length.
SUBSTRING (beginindex:int): string-Returns the string that starts at the specified beginindex in this character and continues to the end of the string.
SubString (Beginindex:int, Endindex:int): String-Returns the string that begins with the specified Beginindex and continues to the string endeIndex-1 . If Beginindex equals Endindex, then return An empty string of length 0, if Beginindex is greater than Endindex, runs the error.
5. Conversion, substitution, and separation of strings
toLowerCase (): string-Returns the new string after converting all characters into lowercase characters
toUpperCase (): string-Returns the new string after converting all characters to uppercase
Trim (): string-Returns a new string after a blank character is removed from both ends.
Replac (Oldchar:char, Newchar:char): string--Returns a new character replacing all of the new strings in the string with the characters it matches
Replacefirst (oldstring:string, newstring): string--Returns a new string in the replacement string after the first of the strings that match it
ReplaceAll (oldstring:string, newstring): string--Returns a new string replacing all the strings in the string that match it
Split (delimiter:string): string[]--Returns an array of strings consisting of a delimited string of delimiters
6. Find a character in a string or a substring
IndexOf (CH:CHAR): INT--Returns the subscript of CH in the first occurrence of a string. Returns 1 if it does not match.
IndexOf (Ch:char, fromindex:int): INT--Returns the subscript of CH for the first time after fromIndex in the string. Returns 1 if it does not match.
IndexOf (s:string): INT--Returns the subscript for the first occurrence of the string s in the string. If no match is returned-1.
IndexOf (s:string, fromindex:int): INT--Returns the subscript of the string s for the first time after the string fromIndex. If no match is returned-1.
LastIndexOf (CH:CHAR): INT--Returns the subscript for the last occurrence of CH in the string. Returns 1 if it does not match.
LastIndexOf (Ch:char, fromindex:int): INT--Returns the subscript for the last occurrence of CH before fromIndex in the string. Returns 1 if it does not match.
LastIndexOf (s:string): INT--Returns the subscript for the last occurrence of the string s in the string. If no match is returned-1.
LastIndexOf (s:string, fromindex:int): INT--Returns the subscript of the string s for the last occurrence of the string fromIndex. If no match is returned-1.
7. Conversion between string and array, numeric value
ToCharArray--Converting a string into a character array
Char[] chars = "java". ToCharArray ();
To convert a character array to a string, you need to call the constructor method string (char[]) or method valueof (char[]):
String str = new string (new char[]{' J ', ' a ', ' V ', ' a '});
String str = string.valueof (new char[]{' J ', ' a ', ' V ', ' a '});
ValueOf (C:char): string--Returns a string containing the character C
ValueOf (data:char[]): string--Returns a string containing the characters in the array
ValueOf (d:double): string--Returns a string containing a double value
ValueOf (f:float): string--Returns a string containing the float value
ValueOf (i:int): string--Returns a string containing the int value
ValueOf (L:long): string--Returns a string containing a long value
ValueOf (B:boolean): string--Returns a string containing a Boolean value
You can use Double.parsedouble (str), Integer.parseint (str) to convert a string to a double or int value.
Second, Stringbuilder/stringbuffer class
You can add, insert, or append new content to a StringBuilder or stringbuffer, but once the string object is created, its value is determined.
StringBuffer is similar to the StringBuilder class except that the modify buffer in StringBuffer is synchronous. If it is multi-tasking concurrent access, use StringBuffer, and if it is a single-tasking access, it will be more efficient to use StringBuilder. ( do not know multitasking...... Mark)
How to construct the Java.lang.StringBuilder class:
StringBuilder ()--Build an empty string generator with a capacity of 16
StringBuilder (Capacity:int)--Build a string generator with a specified capacity
StringBuilder (s:string)--Build a string generator with the specified string
1. Modifying a string in StringBuilder
Append (data:char[]): StringBuilder-Append a char array to this string
Append (data:char[], Offsset:int, len:int): StringBuilder-Append the sub-array in data to this generator
Append (v:aperimitivetype): StringBuilder--a primitive type value appended to this generator as a string
Append (s:string): StringBuilder-Appends a string to the string generator
Delete (Startindex:int, endindex:int): StringBuilder--Deletes the character of the specified interval: startIndex ~ endIndex-1
Deletecharat (index:int): StringBuilder--delete the character of the specified subscript
Insert (Index:int, data:cahr[], Offset:int, len:int): StringBuilder--Inserts a string at the location offset of the generator
Insert (Offset:int, s:string): StringBuilder-Inserts the data sub-array in the array at the specified subscript of the generator
Revers (): StringBuilder--reverses the characters in the generator
Setcharat (Index:int, Ch:char): void--Sets a new character at the subscript of the generator
Replac (Startindex:int, Endindex:int, s:string): StringBuilder-replaces the characters in the specified interval in the generator with the specified string: StartIndex ~ endIndex-1
ToString (): String--Returns a String object from the string generator
Capacity (): INT--Returns the capacity of this string generator
CharAt (index:int): CHAR--Returns the character at the specified subscript
Length (): INT--Returns the number of characters in the generator
SetLength (Newlength:int): void--Sets the new length in the generator
SubString (startindex:int): string--Returns a string starting from StartIndex
SubString (Startindex:int, Endindex:int): string--Returns a string from StartIndex to EndIndex-1
TrimToSize (): Void--reduces the storage size used by the generator.
Packagedemo;ImportJava.util.Scanner;/*** Write a small program, enter a string containing numbers, letters and special symbols, * ignore the special characters in the string to determine whether it is a palindrome * Created by Luts on 2015/12/1.*/ Public classTest { Public Static voidMain (string[] args) {Scanner input=NewScanner (system.in); String src=NewString (); System.out.println ("Enter an arbitrary string"); SRC=Input.next (); System.out.println ("Ignores special characters in the input string, which is a palindrome string?" " +ispalindrome (SRC)); } Public Static BooleanIspalindrome (String s) {string tempstring=filter (s); String tempString2=reverse (tempstring); returntempstring.equals (TEMPSTRING2); //Determine if the contents are not equal } Filter out non-alphabetic, non-numeric characters Public Staticstring Filter (String src) {StringBuilder StringBuilder=NewStringBuilder (); for(inti = 0; I < src.length (); i++){ if(Character.isletterordigit (Src.charat (i))) Stringbuilder.append (Src.charat (i)); } returnstringbuilder.tostring (); } Public Staticstring Reverse (string src) {StringBuilder StringBuilder=NewStringBuilder (SRC); Stringbuilder.reverse (); returnstringbuilder.tostring (); }}
enter an arbitrary string 123*&%^$321true
Sting String Class