You can use the String class and the StringBuffer class.
The string class provides a number of methods for manipulating strings, which are commonly used as follows:
Gets the length of the string: Length ().
comparison string: Equals ( ).
link string: concat ( ).
Extract string: substring ( ).
Search string: IndexOf ().
Split string: Spilt ().
The common StringBuffer class provides the following methods:
Convert to String type: ToString () .
Link string: Append ().
Insert string: Intsert ().
Some cases:
Public classDemo_1 { Public Static voidMain (string[] args) {//TODO auto-generated Method StubString pwd; @SuppressWarnings ("Resource") Scanner Input=NewScanner (System.inch); Do{System. out. println ("Please enter user name:"); String name=Input.next (); System. out. println ("Please enter your password:"); PWD=Input.next (); if(Pwd.length () <6) {System. out. println ("password length not enough"); }Else{System. out. println ("Enter the correct"); System. out. println ("you are welcome! "+name); Break; } } while(Pwd.length ()! =6); }
Boolean flag; Public voidPanduan (String sfz, String sjh,string zj) {flag=false; intIndex=sjh.lastindexof ("-"); if(! (sfz.length () = = -|| Sfz.length () = = -) ) {System. out. println ("the ID number must be 16-bit or 18-bit!"); } Else if(Sjh.length () < One) {System. out. println ("mobile phone number must be 11 bit!"); }Else if(index!=-1&&index!=0&&index!=4&&zj.length ()! = -) {System. out. println ("The landline number must be 4-bit and the phone number must be 7!"); }Else{System. out. println ("Registration Successful"); Flag=true; }} Call: Public Static voidMain (string[] args) {@SuppressWarnings ("Resource") Scanner Input=NewScanner (System.inch); Practice2 PD=NewPractice2 (); Do{System. out. println ("Please enter your ID:"); String SFZ=Input.next (); System. out. println ("Please enter your mobile phone number:"); String SJH=Input.next (); System. out. println ("Please enter your landline number:"); String ZJ=Input.next (); Pd.panduan (SFZ, SJH, ZJ); } while(pd.flag==false); }
Small white Java Learning path "string"