Java Learning String Practice

Source: Internet
Author: User

1, given a string array. Sort from small to large in dictionary order.

Ideas
* 1, sorted by array. You can use the selection, bubbling is OK.
* 2,for nesting and comparison and transposition.
* 3, Question: The front row is an integer, compared with the comparison operator, but is now a string object.
* How do string objects compare? Cool, the object provides functionality for string object comparisons.

1  Public Static voidsort (string[] strings) {2String temp =NULL;3          for(inti=0;i<strings.length;i++){4              for(intj = i+1;j<=strings.length-1;j++){5                 if(Strings[i].compareto (strings[j]) >0){6temp =Strings[i];7strings[i]=Strings[j];8strings[j]=temp;9                 }Ten             } One         } A}

2. The number of occurrences of a substring in the entire string

Ideas
* 1, the substring to be found is present if there is a location to get it to appear. This can be done using indexof.
* 2, if found, then record the occurrence of the position and continue to find the substring in the remaining string,
* While the starting bit of the remaining string is the length of the position + substring that appears.

* 3, and so on, through the loop to complete the lookup, if not found is-1, and each find with a counter record.

1  Public Static intsum (String string,string string1) {2         intLength =string1.length ();3         intCount = 0;4         intindex =String.IndexOf (string1);5          while(Index!=-1){6count++;7index = String.IndexOf (string1,index+length);8         }9         returncount;Ten}

3, two the largest of the same substring in a string

Ideas
* 1, since the maximum substring is obtained, first look at whether the short string is in the long string.
* If present, the short string is the maximum substring.
* 2, if not, then the short sub-string to the length of the way to the substring, to long string to determine whether there is.
* If it exists, it will not be found.

1  Public Staticstring getmaxsubstring (string s1, string s2) {2         3String max =NULL, Min =NULL;4Max = (S1.length () >s2.length ())?s1:s2;5         6min = max.equals (S1)?s2:s1;7         8System.out.println ("max=" +max);9System.out.println ("min=" +min);Ten          One          A          -          for(inti = 0; I < min.length (); i++) { -              the              for(intA = 0,b = Min.length ()-I; b! = Min.length () +1; a++,b++){ -                  -String Sub =min.substring (A, b); - //System.out.println (sub); +                 if(Max.contains (sub)) -                     returnSub; +             } A         } at          -         return NULL; -}

4, simulate a trim function consistent method. Remove whitespace at both ends of a string
Ideas
* 1, define two variables.
* A variable is used as a starting point to determine the angle of a string space. Constant + +.
* A variable as a corner mark that determines the string space from the end. Constantly--。
* 2, to determine that is not a space, the string between the kinsoku can be.
*/

1  Public Staticstring Mytrim (string s) {2 3         intStart = 0, end = S.length ()-1;4 5          while(Start <= end && S.charat (start) = = ") {6start++;7         }8          while(Start <= end && S.charat (end) = = ") {9end--;Ten         } One         returnS.substring (Start, end + 1); A}

Java Learning String Practice

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.