- /*
- 1.模拟一个trim方法,去除字符串两端的空格。
- 思路:
- 1,判断字符串第一个位置是否是空格,如果是继续向下
- 判断,知道不是空格为止,结尾处判断空格也是如此。
- 2,当开始和结尾都判断到不是空格时,就是要获取的字
- 符串。
- 2.将一个字符串进行反转,将字符串中指定部分进行反转,"abcdefg","abfedcg"
- 思路:
- 1,曾经学习过对西方的元素进行反转。
- 2,将字符串变成数组,对数组反转。
- 3,将反转后的数组变成字符串。
- 4,只要将要反转的部分的开始和结束为止作为参数传递即可。
- 3.获取一个字符串在另一个字符串中出现的次数。
- "abkkcdkkefkkskk"
- 思路:
- 1,定义一个计数器。
- 2,获取kk第一次出现的位置。
- 3,从第一次出现位置后剩余的字符串中继续获取kk 出现的位置。
- 每获取一次就计数一次。
- 4,当获取不到时,计数完成。
- 4.获取两个字符串中最大相同子串。第一个动作:将短的那个串进行
- 长度依次递减子串打印。
- "abcwerthelloyuiodef"
- "cvhellobnm"
- 思路:
- 1,将短的那个子串按照长度递减的方式获取到。
- 2,将每获取到的子串去长串中判断是否包含。
- 如果包含,已经找到。
- */
- class StringTest
- {
- public static void sop(String str)
- {
- System.out.println(str);
- }
- public static void main(String[] args)
- {
- String s1 = "awerthelloyuiodef";
- String s2 = "acvhellobnm";
- sop(getMaxSubString(s1,s2));
- // String str = "abkkcdkkefkkskk";
- // sop("count="+getSubCount2(str,"kk"));
- // String s = " bc affg ";
- // sop(s);
- // s = myTrim(s);
- // sop(s);
- // sop(reverseString(s));
- // sop(reverseString(s,4,8));
- }
- //练习四
- public static String getMaxSubString(String s1, String s2)
- {
- String max = "", min = "";
- max = (s1.length()>s1.length())?s1:s2;
- min = (max == s1)?s2:s1;
- for(int x=0; x<min.length(); x++)
- {
- for(int y=0, z=min.length()-x; z!=min.length()+1; y++,z++)
- {
- String temp = min.substring(y,z);
- // sop(temp);
- if(max.contains(temp))//if(s1.indexOf(temp)!=-1)
- return temp;
- }
- }
- return "";
- }
- //练习三,方式二
- public static int getSubCount2(String str, String key)
- {
- int count = 0;
- int index = 0;
- while((index=str.indexOf(key,index)) != -1)
- {
- sop("index="+index);
- index = index+key.length();
- count++;
- }
- return count;
- }
- //练习三
- public static int getSubCount(String str, String key)
- {
- int count = 0;
- int index = 0;
- while((index=str.indexOf(key)) != -1)
- {
- sop("str="+str);
- str = str.substring(index+key.length());
- count++;
- }
- return count;
- }
- //练习二:扩展--->
- public static String reverseString(String s, int start, int end)
- {
- //字符串变成数组
- char[] chs = s.toCharArray();
- //反转数组
- reverse(chs,start,end);
- //数组变成字符串
- return new String(chs);
- }
- private static void reverse(char[] arr, int x, int y)
- {
- for(int start=x, end=y-1 ; start<end; start++,end--)
- {
- swap(arr,start,end);
- }
- }
- //练习二:将字符串反转
- public static String reverseString(String s)
- {
- return reverseString(s,0,s.length());
- }
- private static void reverse(char[] arr)
- {
- for(int start=0,end=arr.length-1; start<end; start++,end--)
- {
- swap(arr,start,end);
- }
- }
- private static void swap(char[] arr, int x, int y)
- {
- char temp = arr[x];
- arr[x] = arr[y];
- arr[y] = temp;
- }
- //练习一:去除字符串两端空格
- public static String myTrim(String str)
- {
- int start=0, end=str.length()-1;
- while(start<=end && str.charAt(start) == ' ')
- start++;
- while(start<=end && str.charAt(end) == ' ')
- end--;
- return str.substring(start,end+1);
- }
- }