package cn.china; public class StringDemo7 { public static void main(String[] args) { String string = " dfg "; String s =mytrim(string); System.out.println(s); } public static String mytrim(String string) { int start=0; int end = string.length()-1; while(start<=end && string.charAt(start)==' '){ start++; } while(start<=end && string.charAt(end)==' '){ end--; } return string.substring(start,end+1); } }
package cn.china; public class StringDemo6 { public static void main(String[] args) { String s2 = "sdfgsdfghenbas"; String s1 = "asdfrethenba"; String s = maxsubstring(s1,s2); System.out.println("s="+s); } public static String maxsubstring(String s1, String s2) { String max=null,min=null; max=s1.length()>s2.length()?s1:s2; min = max.equals(s1)?s2:s1; for(int x=0;x<min.length();x++){ for (int i = 0, y =min.length()-x;y<min.length()+1 ; i++,y++) {/**因为是取子串所以要+1*/ String s=min.substring(i, y); System.out.println(s); if(max.contains(s)){ return s; } } } return null; } }
package cn.china; public class StringDemo5 { public static void main(String[] args) { // TODO Auto-generated method stub String s1 = "nbasdnbasdfnbasdfnbadsnba"; String s2 = "nba"; int count = nba_times(s1, s2); System.out.println("count="+count); } public static int nba_times(String s1, String s2) { int index = 0; int count = 0; while ((index = s1.indexOf(s2)) != -1) { count++; s1 = s1.substring(index + s2.length(), s1.length()); System.out.println("s1="+s1); } return count; } }
package cn.china; public class StringDemo4 { public static void main(String[] args) { // TODO Auto-generated method stub String s1 = "nbasdnbasdfnbasdfnbadsnba"; String s2 = "nba"; int count = nba_times(s1,s2); System.out.println("count="+count); } public static int nba_times(String s1, String s2) { // TODO Auto-generated method stub int count=0; int index = 0; while((index=s1.indexOf(s2,index))!=-1){ System.out.println(index); count++; index=index+s2.length(); } return count; } }
package cn.china; public class StringDemo3 { public static void main(String[] args) { // TODO Auto-generated method stub String[]arr = {"ab","cd","ae"}; printArray(arr); sortArray(arr); printArray(arr); } public static void printArray(String [] arr){ System.out.print("["); for(int x=0;x<arr.length;x++){ if(x!=arr.length-1){ System.out.print(arr[x]+","); }else{ System.out.println(arr[x]+"]"); } } } public static void sortArray(String[] arr){ for(int x = 0;x<arr.length-1;x++){ for(int y = 0;y<arr.length-1-x;y++){ if(arr[y].compareTo(arr[y+1])>0){ swap(arr,y,y+1); } } } } public static void swap(String[]arr,int a,int b){ String temp=arr[a]; arr[a]=arr[b]; arr[b]=temp; } }