Complete the task by querying the string class in the Java API

Source: Internet
Author: User

Package com. String;public class a {    public static void main (String[]  args)  {        String str =  "This is  a test of java ";         t1 (str);//  The static method can call         t2 (str) directly;       &NBSP;&NBSP;T3 (str);         t4 (str);      &NBSP;&NBSP;&NBSP;T5 (str); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;T6 (str);         t8 ("123321");     }    // 1. Counts the number of occurrences of the letter S in the string     public static void t1 (STRING&NBSP;STR)  {         int num = 0;//  Define a counter          for  (INT&NBSp;i = 0; i < str.length ();  i++)  {             char ch = str.charat (i);//  get the character of the current position              if  (ch ==  ' s ')  {                 num++;//  If there is a s, counter +1             }         }        system.out.println ("string has:"  + num +   "S");     }    // 2. Remove substring "test"      Public static void t2 (STRING&NBSP;STR)  {         Int beginindex = str.indexof ("test") the position of the;//  substring first appears          int endindex = beginindex + 4;        string s  = str.substring (Beginindex, endindex);         System.out.println (s);     }    // 3. Copy this string to a character array in a variety of ways char[] &NBSP;&NBSP;&NBSP;&NBSP;PUBLIC&NBSP;STATIC&NBSP;VOID&NBSP;T3 (STRING&NBSP;STR)  {         //  method One: Char[] ch=str.tochararray ();         //  Method Two:        char[] ch = new  Char[str.length ()];//  defines an array         for  (int i =  0; i < str.length ();  i++)  {             ch[i] = str.charat (i);//  remove characters from a specific position to the array          }         system.out.println (ch.length +  ","  + str.length ());//  The array length is consistent with the string length, which indicates that replication succeeded     }    // 4. capitalizes the first letter of each word in a string,  Output to the console. &NBSP;&NBSP;&NBSP;&NBSP;PUBLIC&NBSP;STATIC&NBSP;VOID&NBSP;T4 (STRING&NBSP;STR)  {         string[] ss = str.split (" ");//  the word with a space and gets an array of strings          String result =  "";         for  (Int i = 0; i < str.length ();  i++)  {             char c = ss[i].charat (0);//   Remove the first letter of the word             string s =  string.valueof (c);//  convert character to string   or a-97,a-65             String S1 = s.touppercase ();//  into uppercase              system.out.println (S1);             string  s2 = s1 + ss[i].substring (1);// ss[i].substring (1): intercepts the remaining strings in each word except for the first letter              result = result + s2  +  " ";        }       &NBSP;&NBSP;SYSTEM.OUT.PRINTLN (result);     }    // 5. To implement the reverse output of the string &NBSP;&NBSP;&NBSP;&NBSP;PUBLIC&NBSP;STATIC&NBSP;VOID&NBSP;T5 (STRING&NBSP;STR)  {         string s = new stringbuffer (str). reverse (). ToString ();//  After the anonymous object call reverse method reverses, it becomes a string         system.out.println (s);     }   &Nbsp;// 6. Converting this string to an array of strings requires that each array element be a meaningful English word and output to the console     public static  VOID&NBSP;T6 (STRING&NBSP;STR)  {        string[] ss =  str.split (" ");//  separated by spaces         for  (int i  = 0; i < str.length ();  i++)  {             //  judge current Ss[i] is not a valid word:             boolean b = true;//  Define flag amount B, default B valid              for  (Int j = 0; j < str.length ();  j++)  {                 if  (!) ( (Ss[i].charat (j)  >=  ' A '  && ss[i].charat (j)  <=  ' z ')  | |   (SS[I]&NBSP;&Nbsp;                       .charat (j)  >=  ' A '  && ss[i].charat (j)  <=   ' Z '))  {                     b = false;// ss[i] is not a word, as long as one does not jump out of the loop                      break;                 }             }             if  (b)  {                 system.out.println (Ss[i]);             }  &nbsP;     }        system.out.println ("--------- -");     }    // 8. Determine if the input string is a palindrome (such as  12344321, ABCCBA is a palindrome   112233 not)--the same     public static void t8 after folding (STRING&NBSP;STR)  {         //method Three: Reverse the string, and the same as the original string, then a palindrome          //method Two: The first half and the last half of the string to make a fuss         String s1 =  Str.substring (0, str.length ()  / 2);//  first half string          string s2 = str.substring (Str.length ()  / 2 +  (Str.length ()  % &NBSP;2),                 Str.length ());//  the latter half of the string         string s3 = new  stringbuffer (S2). Reverse (). toString ();         if  (S1.equals (S3))  {             system.out.println ("true");         }    }}      // 7. Implements the word flashback output of the string. (i.e. output Siht si a tset fo avaj).


Complete the task by querying the string class in the Java API

Related Article

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.