One, String class
1. String is a class that is located in the Java.lang package
2.2 ways to create a string object:
String Variable name = "Value";
String Object name =new string ("value");
3. Common methods of string
3.1 String length--length ()
3.2 Determine if the values are equal equals ()
3.3 Determine if the string object address is the same = =
3.4 Ignore case judgment equalsignorecase ()
3.5 Case Conversion
toLowerCase ()--lowercase
toUpperCase ()--uppercase
public static void main (String [] args) {Scanner input =new
Scanner (system.in); String uname,pwd; System.out.print ( Please enter user name:
=input.next (); System.out.print ( "Please enter password:" =input.next (); if (Pwd.length () >=6 "registered successfully! "); } else {System.out.println ( "Password length is not can be less than 6 bits! "); } }
judging length
public static void main (String [] args) {Scanner input =new
Scanner (system.in); String uname,pwd; System.out.print ( Please enter user name:
=input.next (); System.out.print ( "Please enter password:" =input.next (); if (Uname.equals ("Tom") && pwd.equals ("1234567" ) {System.out.println (" Login succeeded!) "); else {System.out.println ( "Password or account number Error! "); } }
v to determine if strings are equal
Public Static voidmain (String [] args) {Scanner input=NewScanner (system.in); String STR1=NewString ("I Love my Motherland"); String STR2=NewString ("I Love my Motherland"); if(Str1.equals (STR2)) {System.out.println ("Two strings equal"); }Else{System.out.println ("Two strings Not Equal"); } if(str1==Str2) {System.out.println ("Two strings equal"); }Else{System.out.println ("Two strings Not Equal"); } }determine if the string address is the same
3.6 Ways to concatenate strings
Method 1: Use the + sign
Method 2: String variable. concat (The string to concatenate);
3.7 Common methods for string extraction
The same method, with multiple representations-method overloading
3.7.1 indexOf (character or string), where the first occurrence of a character or string is found;
Returns a number, 1 means no, returns a number that is >=0, indicating the presence
3.7.2 lastIndexOf (character or string) to find the last occurrence of a character or string;
Returns a number, 1 means no, returns a number that is >=0, indicating the presence
3.7.3 substring () method
L substring (extract position): Indicates the end of extraction from the beginning of the fetch position to the end of the string
L substring (start position, end position): a substring that extracts from the start position to the end position-1 [the end position is greater than the length of the entire string, an exception occurs]
3.8 Trim ()--remove whitespace from the left and right sides of the string
Public Static voidmain (String [] args) {Scanner input=NewScanner (system.in); System.out.print ("Please enter the user name:"); String name=Input.next (); System.out.print ("Please enter the user password:"); String pwd=Input.next (); if(Name.tolowercase (). Equals (("TOM"). toLowerCase ()) &&pwd.touppercase (). Equals (("1234567"). toUpperCase ())) {System.out.println ("Successful Landing!" "); }Else{System.out.println ("User name or password does not match, login failed." "); } }
Second, StringBuffer class
1. What is the difference between StringBuffer and string?
String space or length is fixed, stringbuffer space variable
String efficiency less than StringBuffer class
2. What are the steps of using the StringBuffer class?
Step 1: Create the object first
StringBuffer Object name =new stringbuffer ();
StringBuffer Object name =new stringbuffer (default value);
Step 2: Append or Insert values to the object
StringBuffer object name. Append (appended value);//can append any value (Char,int, etc.)
StringBuffer the object name. Insert (the position to insert, the value inserted);
Public Static void main (string [] args) { string words= "changting outside the ancient road side of the grass green Breeze Breeze Liudisheng Sound Remnant Sunset Mountain Mountain"; string [] A=new string [[]; SYSTEM.OUT.PRINTLN ("* * * Original lyrics ***\n" +words); A=words.split (""); for (int i=0;i<a.length;i++) { System.out.println (a[i]); // print output. } }
Java Basics (9)