1. Rewrite the Strim method to remove spaces at both ends of the string
Package Www.shangguigu.java.exer;import org.junit.test;/* Practice one: Rewrite the Strim method, enter a string, remove the spaces at both ends */public class Stringdemo {@ test//test 1:public void Test1 () {String str = "Wangqin wo aini! "; /string str = " ";//A string with only a space is theoretically not printed, but a null pointer exception appears, plus string newstr = Stringdemo.mystrim (str); System.out.println (NEWSTR);} Exercise one: public static string Mystrim (String str) {int start = 0;int end = Str.length () -1;while (start<=end && str.c Harat (start) = = ") {start++;} while (Start<=end &&str.charat (end) = = ") {end--;} String strimstr = str.substring (start,end+1); return strimstr;}
2. Invert a string, such as "ABCDEFG", and invert it to "ABFEDCG"
Package Www.shangguigu.java.exer;import org.junit.test;/* Exercise two: Invert a string. Reverses a part of a string, such as "ABCDEFG" to "ABFEDCG". Exercise three: */public class Stringdemo {@Testpublic void test2 () {String str = "ABCDEFG"; String revStr1 = stringdemo.reversestring1 (str, 2, 5); System.out.println (REVSTR1); String revStr2 = stringdemo.reversestring2 (str, 2, 5); System.out.println (REVSTR2);} Practice Two: Method One: Use the way to intercept a string public static string ReverseString1 (String str,int Start,int end) {String str1 = str.substring (0, Start); String str2 = str.substring (end+1), for (int i=end;i>=start;i--) {char c = str.charat (i); str1 + = C;} str1 + = Str2;return s TR1;} Exercise two: Method two: By forwarding the string into a character array, in the reverse operation, and finally in converting the character array into a string public static string ReverseString2 (string str,int start, int end) {Char [] Strarray = Str.tochararray (); return Reversechararray (Strarray,start,end);} public static String Reversechararray (char[] strarray,int Start,int end) {for (int i=start,j=end;i<end;i++,j--) {Char temp = Strarray[i];strarray[i] = strarray[j];strarray[j] = temp;} return new StriNg (Strarray);}
3. Gets the number of occurrences of a string in another string. For example:
: Gets the number of "AB" occurrences in "Abkkcadkabkebfkabkskab"
Package Www.shangguigu.java.exer;import org.junit.test;/* Exercise three: Gets the number of occurrences of a string in another string. For example: Get the number of "AB" occurrences in "Abkkcadkabkebfkabkskab" */public class Stringdemo {@Testpublic void Test3 () {String str1 = " Abcab3fabkkkabddcadl "; String str2 = "AB"; int count = Stringdemo.gettime (str1, str2); System.out.println (count);} Exercise 3: Get the number of occurrences of a string in another string. Gets the number of times that STR2 appears in str1 public static int getTime (String str1,string str2) {int count = 0;int len;while ((Len=str1.indexof (STR2) !=-1) {//str1.indexof (str2) determines where str2 first appears in str1 count++;str1 = str1.substring (len + str2.length ());} return count;}
Still Silicon Valley JavaSEday18 string class exercises