- class Demo2
- {
- public static void sop(String str)
- {
- System.out.println(str);
- }
- public static void main(String[] args)
- {
- //System.out.println("Hello World!");
- //String s="abkkcdkkefkkskkhkktkk";
- //System.out.println("出现次数为:"+getcount("kk",s));
- String s1="abchellodvert";
- String s2="abhellod";
- sop(getMaxSubString(s1,s2));
- //sop(reverseString(s));
- //System.out.println(s);
- //myTrim(s);
- }
- //练习一,去除字符串两端空格
- public static void myTrim(String str)
- {
- int start=0,end=str.length()-1;
- while(start<=end && str.charAt(start)==' ')
- start++;
- while(start<=end && str.charAt(end)==' ')
- end--;
- String s=str.substring(start,(end+1));
- sop(s);
- }
- //练习二,将字符串反转
- public static String reverseString(String s)
- {
- char[] arr=s.toCharArray();
- char[] arr2=new char[arr.length];
- int x=0;
- for(int i=arr.length-1;i>=0;i--)
- {
- arr2[x++]=arr[i];
- }
- String str=new String(arr2);
- return str;
- }
- //获取一个字符串在另一个字符串中出现的次数
- public static int getcount(String s1,String s2)
- {
- int count=0,index=0;
- boolean res=s2.contains(s1);
- while(res && index!=(s1.length()-1))
- {
- index=s2.indexOf(s1,index);
- count=count+1;
- index=index+s1.length();
- }
- return count-1;
- }
- //练习四
- public static String getMaxSubString(String s1,String s2)
- {
- for(int x=0;x<s2.length();x++)
- {
- for(int y=0,z=s2.length()-x;z!=s2.length()+1;y++,z++)
- {
- String temp=s2.substring(y,z);
- //sop(temp);
- if(s1.contains(temp))
- return temp;
- }
- }
- return "";
- }
- }
复制代码
|
|