String算法考察小练习(是我自己的思路,还不是最优解法)
1 package day2_9; 2 3 import java.util.Arrays; 4 5 /** 6 * @Author Tianhao 7 * @create 2021-02-09-15:05 8 */ 9 public class StringExer { 10 11 12 13 //实现和trim()一样的作用,去掉收尾空格 14 public String myTrim(String str) { 15 16 int start = 0; 17 int end = 0; 18 char[] chars = str.toCharArray(); 19 for (int i = 0; i < chars.length; i++) { 20 if (!String.valueOf(chars[i]).equals(" ")) { 21 start = i; 22 break; 23 } 24 } 25 for (int i = chars.length -1; i >= 0; i--) { 26 if (!String.valueOf(chars[i]).equals(" ")) { 27 end = i; 28 break; 29 } 30 } 31 return str.substring(start, end + 1); 32 } 33 34 35 //实现数组的反转,将字符串中指定部分进行反转 36 //比如“abcdef",将其中的cde反转后,输出为abedcf 37 public String myReverse(String src,String target) { 38 int start = src.indexOf(target); 39 int end = start+target.length(); 40 String substring = src.substring(start, end); 41 char[] chars = substring.toCharArray(); 42 int len = chars.length; 43 char[] newChars = new char[len]; 44 for (int i = 0,j=len-1; i < len; i++,j--) { 45 newChars[j] = chars[i]; 46 } 47 String newS = new String(newChars); 48 return src.substring(0,start)+ newS + src.substring(end); 49 } 50 51 52 //获取一个字符串在另一个字符串中出现的次数 53 public int myContainsCount(String src,String target) { 54 int count = 0; 55 for (; ; ) { 56 int index = src.indexOf(target); 57 if (index == -1) { 58 break; 59 } 60 count++; 61 src = src.substring(index+target.length()); 62 } 63 return count; 64 } 65 66 67 //获取两个字符串中最大相同子串 68 public String myLongSubstring(String one, String two) { 69 String max; 70 String min; 71 if (one.length() >= two.length()) { 72 max = one; 73 min = two; 74 } else { 75 max = two; 76 min = one; 77 } 78 String sub; 79 String targetSub = "没有相同的子串"; 80 int subLen = 0; 81 int index; 82 for (int i = 0; i < min.length(); i++) { 83 for (int j = min.length(); j > i; j--) { 84 sub = min.substring(i, j); 85 index = max.indexOf(sub); 86 if (index != -1) { 87 if (subLen < sub.length()) { 88 targetSub = sub; 89 subLen = sub.length(); 90 } 91 } 92 } 93 } 94 return targetSub; 95 } 96 97 //对字符串中字符进行自然顺序排序 98 public String mySort(String str) { 99 char[] chars = str.toCharArray(); 100 Arrays.sort(chars); 101 String newStr = new String(chars); 102 return newStr; 103 } 104 105 }
测试如下:
1 package day2_9; 2 3 import org.junit.Test; 4 import org.junit.runner.RunWith; 5 6 import java.util.Arrays; 7 8 import static org.junit.Assert.*; 9 10 /** 11 * @Author Tianhao 12 * @create 2021-02-09-15:12 13 */ 14 15 public class StringExerTest { 16 StringExer stringExer = new StringExer(); 17 18 @Test 19 public void myTrim() { 20 String s = stringExer.myTrim(" wang tian "); 21 System.out.println(s); 22 } 23 24 @Test 25 public void myReverse() { 26 String s = stringExer.myReverse("helloworld", "worl"); 27 System.out.println(s); 28 } 29 30 @Test 31 public void myContainsCount() { 32 int count = stringExer.myContainsCount("xiaoqiang and xiaohong", "ng"); 33 System.out.println(count); 34 35 } 36 37 38 @Test 39 public void myLongSubstring() { 40 String target = stringExer.myLongSubstring("xiaoqiang and xiaohong", "hong is female,qiang is male."); 41 System.out.println(target); 42 43 } 44 45 @Test 46 public void mySort() { 47 String str = stringExer.mySort("yienhaodlcjkf"); 48 System.out.println(str); 49 } 50 51 52 //补充知识点 53 @Test 54 public void test() { 55 String s = "wang tian"; 56 char[] chars = s.toCharArray(); 57 //数组工具类Arrays的复制方法copyOf(xx[],newLength) 58 char[] chars1 = Arrays.copyOf(chars, chars.length-3); 59 System.out.println(Arrays.toString(chars)); 60 System.out.println(Arrays.toString(chars1)); 61 62 63 //基本数据类型与String的转换 64 //第一种方式:String.valueOf(xxx) 65 int i = 1; 66 System.out.println(String.valueOf(i)); 67 char c = 'w'; 68 System.out.println(String.valueOf(c)); 69 70 //第二种方式:基本数据对应的包装类.toString() 71 //下面其实也是String.valueOf(int)的内部实现 72 System.out.println(Integer.toString(i)); 73 74 //对于char型,有第三种方式: 75 //下面是valueOf(char)的内部实现 76 char[] charArr = {c}; 77 String s1 = new String(charArr); 78 System.out.println(s1); 79 80 } 81 }