Java---String class

Source: Internet
Author: User

 Packagesad;//common methods of the String classImportJava.util.Scanner; Public classers { Public Static voidMain (string[] args) {factory.choose (); }}classfactory{Static voidChoose () {intChoose=0; Scanner SC=NewScanner (system.in);  while(true) {System.out.println ("Please enter the action you want:"); System.out.print ("1: Extract a character from a string \n2: strings and Arrays convert \n3: Determine if a string is included (lookup) \n4: string substitution \n5: Intercept string \n6: Split string \ n"); Choose=Sc.nextint (); Switch(Choose) { Case1:            NewStringcharat ();  Break;  Case2:            NewStringarray ();  Break;  Case3:            NewStringsearch ();  Break;  Case4:            NewStringReplace ();  Break;  Case5:            NewSubString ();  Break;  Case6:            NewStringsplit ();  Break; default:                 Break; }        }    }}classstringcharat{ PublicStringcharat () {Scanner SC=NewScanner (system.in); System.out.println ("Please enter a string:"); String Str=Sc.next (); System.out.println ("The 0th character is:" +str.charat (0));//remove a character from a stringSystem.out.println ("The second character is:" +str.charat (2)); }}classstringarray{ PublicStringarray () {Scanner SC=NewScanner (system.in); System.out.println ("Please enter a string:"); String Str=Sc.next (); CharData[] = Str.tochararray ();//array of string turnsSystem.out.println ("After the string is converted to an array:");  for(inti=0;i<data.length;i++) {System.out.println (Data[i]+" "); } System.out.println ("After the array is converted to a string:"); System.out.println (NewString (data));//Array to stringSystem.out.println ("begins to intercept a string of a certain length from a character in the array:"); System.out.println ("Please enter the starting position:"); intStart =Sc.nextint (); System.out.println ("Please enter length:"); intLength =Sc.nextint (); System.out.println (NewString (data,5,4));//starting point, lengthSystem.out.println ("____________________"); }}classstringsearch{ PublicStringsearch () {//TODO auto-generated Constructor stubString str = "***hello**word"; if(Str.contains ("Hello"))//determine if a string is included{System.out.println ("contains the specified string"); }        if(Str.indexof ("E")!=-1)//determines whether the specified character is present from the beginning, and the return value is the position{System.out.println ("String Position:" +str.indexof (' E ')); }        if(Str.indexof ("E", 6) ==-1)//To find out if a character is present at the beginning of the specified position{System.out.println ("Does not exist"); }        if(Str.lastindexof ("E", 12) ==-1)//find out if a character is present from the beginning of the specified position{System.out.println ("Does not exist"); }        if(Str.startswith ("*"))//determine whether to start with a character{System.out.println ("True"); }        if(Str.startswith ("*", 2))//determines whether a specified position is a character or a segment of characters{System.out.println ("True"); }        if(Str.endswith ("D"))//determines whether a character ends{System.out.println ("True"); } System.out.println ("____________________"); }}classstringreplace{StringReplace () {String str= "Helloword"; System.out.println (Str.replaceall ("O", "* * *"));//Replace all occurrences of a character withSystem.out.println (Str.replacefirst ("L", "_"));//replaces the first occurrence of a specified characterSystem.out.println ("____________________"); }}classsubstring{ PublicSubString () {String str= "Hellojava"; System.out.println (Str.substring (5));//intercepts a string from the specified position to the endSystem.out.println (str.substring (0,5));//intercepts a string at a specified positionSystem.out.println ("____________________"); }}classstringsplit{ PublicStringsplit () {String str= "Hello World Hello java"; string[] Data= Str.split ("");  for(inti=0;i<data.length;i++) {System.out.println (data[i]); } System.out.println ("____________________"); String data2[]= Str.split ("", 3);  for(inti=0;i<data2.length;i++) {System.out.println (data2[i]); }        }}

Java---String class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.