String-type common method exercises, string-type exercises

Source: Internet
Author: User

String-type common method exercises, string-type exercises

StringClass represents a string. All character strings in Java (such"abc".

Strings are constants, and their values cannot be changed after creation. The string buffer supports variable strings.

StringClass can be used to check a single character, comparison string, search string, extract substring,

Create a string copy and convert all characters to uppercase or lowercase letters.

The String class is one of the most commonly used classes. Here are some exercises to familiarize yourself with the important methods provided in the String class.

String Exercise 1
Returns a string array in alphabetical order of Case sensitivity.

Ideas:
1. Sort the array by selection or bubble.
2. for loop nesting, comparison, and switch location.
3. The difference is that we used to compare numbers with comparison operators;
The comparison is a string object, and the compareTo method should be used.

1 public class StringTest_1 2 {3 // sort String arrays. 4 public static void stringSort (String [] arr) 5 {6 // sort 7 for (int I = 0; I <arr. length-1; I ++) 8 {9 for (int j = 0; j <arr. length-1-i; j ++) 10 {11 // use the compareTo Method for string comparison 12 if (arr [j]. compareTo (arr [j + 1])> 0) 13 {14 String temp = arr [j]; 15 arr [j] = arr [j + 1]; 16 arr [j + 1] = temp; 17} 18} 19} 20 showArray (arr); 21} 22 23 // define the method as [str1, str2, str3] format to print array 24 public static void s HowArray (String [] arr) 25 {26 System. out. print ("["); 27 for (int I = 0; I <arr. length; I ++) 28 {29 if (I! = Arr. length-1) 30 System. out. print (arr [I] + ","); 31 else32 {33 System. out. print (arr [I] + "] \ n"); 34} 35} 36} 37 public static void main (String [] args) 38 {39 String arr [] = {"nba", "abc", "CBA", "zz", "qq", "haha "}; 40 // print the array 41 showArray (arr); 42 // sort the array and output 43 stringSort (arr); 44} 45}

Run:

 

String Exercise 2
Number of times a substring appears in a string
Ideas:
1. Use the indexOf method to obtain the position index of the substring that appears for the first time in the string.
2. Use the indexOf method to obtain the position of the remaining substring starting with (index + substring length) until the substring is no longer included in the string.
It can be implemented through a while loop.
3. Use the counter to record each time it is found.

1 public class StringTest_2 2 {3 public static void main (String [] args) 4 {5 String str = "abcqwabcedcxabcuabcjkabcnmbabc"; 6 // String str = null; 7 try 8 {9 int count = countChildStr (str, "abc"); 10 System. out. println ("abc appears in" + str + ":" + count); 11} 12 catch (NullPointerException ne) 13 {14 System. out. println (ne); 15} 16 catch (RuntimeException re) 17 {18 System. out. println (re); 19} 20} 21 public static in T countChildStr (String str, String key) 22 {23 if (str = null | key = null) 24 {25 throw new NullPointerException ("null pointer exception, source string and substring cannot be NULL "); 26} 27 if (key =") 28 {throw new RuntimeException ("the call is invalid, the substring must contain content ");} 29 int count = 0, index = 0; 30 while (index = str. indexOf (key, index ))! =-1) 31 {32 count ++; 33 index + = key. length (); 34} 35 return count; 36} 37}

 

String Exercise 3
Find the maximum public substring of two strings
Ideas:
1. Determine whether a long string contains a short string. If so, the short string is the largest public substring.
2. If the string is not included, take the substring from the shorter string in descending length, and determine whether to include the substring from the longer string,
If the content is included, you can find it again.
3. Emphasis: Take the substring of a string in descending Length

1 public class StringTest_3 2 {3 public static void main (String [] args) 4 {5 // create two strings not empty 6 String str1 = "abxczwsxcvdfas "; 7 // String str1 = null; 8 String str2 = "ghwsxcvxcdbgthnnnrfqwe"; 9 10 try11 {12 String str = searchMaxCommonStr (str1, str2); 13 System. out. println ("the maximum public substring is:" + str); 14} 15 catch (NullPointerException ne) 16 {17 System. out. println (ne); 18} 19} 20 public static String searchMaxCommonStr (S Tring str1, String str2) 21 {22 if (str1 = null | str2 = null) 23 throw new NullPointerException ("Null pointer exception, parameter cannot be null "); 24 // determine the longer String and shorter String 25 String max = (str1.length ()> str2.length ())? Str1: str2; 26 String min = (str1.equals (max ))? Str2: str1; 27 28 // substring is obtained in descending order of length, from min. length ~~ 129 for (int I = min. length (); I> 0; I --) 30 {31 for (int x = 0, y = x + I; y <min. length (); x ++, y ++) 32 {33 String childStr = min. substring (x, y); 34 // if a long string contains this substring, 35 is found. // otherwise, 36 if (max. contains (childStr) 37 return childStr; 38} 39} 40 return null; 41} 42}

Run:

 

String Exercise 4

Write a method with the same functionality as trim
Ideas:
1. Define two variables to store two badge
2. traverse the strings from the beginning and end until the first character without spaces is found.
3. truncate a string

1 public class StringTest_4 2 {3 public static void main (String [] args) 4 {5 String str = "abc ws"; 6 str = myTrim (str); 7 System. out. println (str); 8 9} 10 public static String myTrim (String s) 11 {12 int begin = 0, end = s. length ()-1; 13 // traverse from the beginning 14 while (begin <= end & s. charAt (begin) = '') 15 {16 begin ++; 17} 18 // traverse 19 from the tail while (begin <= end & s. charAt (end) = '') 20 {21 end --; 22} 23 return s. substring (begin, end + 1); 24} 25}

Run:

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.