一、將字串反轉
package string;public class Reverse { public static void main(String[] args) { String s1 = "abc defg wf"; //用字串轉字元數組實現反轉 String s2 = ""; char[] cs = s1.toCharArray(); for (int i = cs.length - 1; i >= 0; i--) { s2 = s2 + cs[i]; } System.out.println("方法一反轉操作後為:" + s2); //利用StringBuffer的reverse()方法實現反轉 StringBuffer sb = new StringBuffer(s1); StringBuffer sb2 = sb.reverse(); System.out.println("方法二反轉操作後為:" + sb2); //利用倒序儲存實現反轉 String s3=myReverse(s1); System.out.println("方法三反轉操作後為:" + s3); } public static String myReverse(String str) { char[] arr = new char[str.length()]; int pos = str.length(); for (int x=0; x<str.length(); x++) { arr[x] = str.charAt(--pos); } str = String.copyValueOf(arr); return str; } }
輸出:
方法一反轉操作後為:fw gfed cba方法二反轉操作後為:fw gfed cba方法三反轉操作後為:fw gfed cba
二、實現去字串兩端空格功能
package string;public class Trime { public static void main(String[] args) { String str1 = " dwwg dwg wfg "; //利用String類自己的方法去除兩端空格 String str2 = str1.trim(); System.err.println("方法一"+str2); //自訂方法去除兩端空格 String str3=myTrim(str1); System.err.println("方法二"+str3); } 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); }}
輸出:
方法一dwwg dwg wfg方法二dwwg dwg wfg
三、擷取一個字串在另一個字串中出現的次數,並返回出現起始索引
package string;public class GetCount { public static void main(String[] args) { String str = "sshelloworldhellojavahellojsp"; System.out.println("count=" + getSubCount(str, "hello")); } public static int getSubCount(String str, String key) { int count = 0; int index = 0; while ((index = str.indexOf(key, index)) != -1) { System.out.println("index=" + index); index = index + key.length(); count++; } return count; }}
index=2index=12index=21count=3
indexOf返回的是長字串中所求子字串起始處索引值,沒有則返回-1.
四、將一段英文句子倒序輸出
package string;import java.util.Scanner;public class Reverse02 { public static String reverseWords(String sentence) { StringBuilder sb = new StringBuilder(sentence.length() + 1); String[] words = sentence.split(" "); for (int i = words.length - 1; i >= 0; i--) { sb.append(words[i]).append(' '); } sb.setLength(sb.length() - 1); return sb.toString(); } public static void main(String[] args) { Scanner in = new Scanner(System.in); // 存放輸入的字串 System.out.println("輸入測試案例數目n:"); String[] input = new String[in.nextInt()]; in.nextLine(); for (int i = 0; i < input.length; i++) { input[i] = in.nextLine(); } // 輸出倒序後的英文字串 System.out.println("輸出為:"); for (String s : input) { System.out.println(reverseWords(s)); } }}
輸入測試案例數目n:2hello world!i love you?輸出為:world! helloyou? love i
四、刪除一個字串中出現最多的字元
package string;import java.util.HashMap;import java.util.Map;import java.util.Set;public class DelMost { public static void main(String[] args) { String str = "aassdfscfw"; Map<Character, Integer> map = new HashMap<Character, Integer>(); char[] box = str.toCharArray(); for (char ch : box) { Integer i = map.get(ch); if (i == null) { map.put(ch, 1); } else { map.put(ch, i + 1); } } // 建立map索引值對 Integer max = 0; Set<Character> set = map.keySet(); for (Character key : set) { Integer j = map.get(key); if (max < j) { max = j; } } // 將最大次數傳給max for (Character key : set) { if (map.get(key) == max) { str = str.replaceAll(key.toString(), ""); } } System.out.println(str); }}
aadfcfw
五、輸入一行字元分別統計英文字母、數字、空格個數。
package string;import java.util.Scanner;public class Tongji { public static void main(String[] args) { int eng=0,num=0,kong=0,elsel=0; Scanner sc=new Scanner(System.in); String str=sc.nextLine(); char[] ch=null; ch=str.toCharArray(); for(char c:ch){ if(c>='0'&&c<='9'){ num++; }else if((c>='a'&&c<='z')||(c>='A'&&c<='Z')){ eng++; }else if(c==' '){ kong++; }else{ elsel++; } } System.out.println("字母有"+eng); System.out.println("數字有"+num); System.out.println("空格有"+kong); System.out.println("其他"+elsel); }}
hello world 378nihao字母有15數字有3空格有2其他0