當前位置:
首頁 >
Java算法--串的简单处理
發布時間:2025/7/14
33
豆豆
生活随笔
收集整理的這篇文章主要介紹了
Java算法--串的简单处理
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目例如以下:
串的處理
在實際的開發工作中。對字符串的處理是最常見的編程任務。
本題目即是要求程序對用戶輸入的串進行處理。詳細規則例如以下:
1. 把每個單詞的首字母變為大寫。
2. 把數字與字母之間用下劃線字符(_)分開,使得更清晰
3. 把單詞中間有多個空格的調整為1個空格。
比如:
用戶輸入:
you and me what cpp2005program
則程序輸出:
You And Me What Cpp_2005_program
用戶輸入:
this is a 99cat
則程序輸出:
This Is A 99_cat
我們如果:用戶輸入的串中僅僅有小寫字母,空格和數字。不含其他的字母或符號。
每個單詞間由1個或多個空格分隔。
如果用戶輸入的串長度不超過200個字符。
方法一:
public class 串的簡單處理 {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);String string = scanner.nextLine();Vector<Character> vector = new Vector<Character>();for (int i = 0; i < string.length(); i++) {vector.add(string.charAt(i));}try {int index = 0;while (index < vector.size()) {//推斷第一個是否為小寫的英文字符,是的話進行操作if (index == 0 && vector.elementAt(index) >= 'a'&& vector.elementAt(index) <= 'z') {//Replaces the element at the specified position in this Vector with the specified elementvector.set(index,(char) (vector.elementAt(index) - ('a' - 'A')));} else if (vector.elementAt(index - 1) == ' '&& vector.elementAt(index) == ' ') {//處理有多個空格的可能vector.remove(index);index--;} else if (vector.elementAt(index - 1) == ' '&& (vector.elementAt(index) >= 'a' && vector.elementAt(index) <= 'z')) {//推斷是空格后邊的字符vector.set(index,(char) (vector.elementAt(index) - ('a' - 'A')));} else if ((vector.elementAt(index) >= 'a' && vector.elementAt(index) <= 'z')&& (vector.elementAt(index - 1) >= '0' && vector.elementAt(index - 1) <= '9')) {vector.add(index, '_');index++;} else if ((vector.elementAt(index - 1) >= 'a' && vector.elementAt(index - 1) <= 'z')&& (vector.elementAt(index) >= '0' && vector.elementAt(index) <= '9')) {//推斷的是數字vector.add(index, '_');index++;}index++;}for (int i = 0; i < vector.size(); i++) {System.out.print(vector.elementAt(i));}System.out.println();} catch (ArrayIndexOutOfBoundsException e) {}} }方法二:主要用到正則表達式對字符串進行截取,然后對每個字符數組的元素進行正則匹配,含有數字的單獨進行處理
public class SimpleString {// 打印字符串的函數public static void print(String[] s) {for (int i = 0; i < s.length - 1; i++) {System.out.print(s[i] + " ");}System.out.println(s[s.length - 1]);}public static void main(String[] args) {Scanner scan = new Scanner(System.in);String s = scan.nextLine();String[] ss = s.split("[\\s]+"); // 依據正則表達式,刪除一個或多個空格,將字符串保存為字符數組for (int i = 0; i < ss.length; i++) {// 將每個字符數組的首字母改為大寫String up = ("" + ss[i].charAt(0)).toUpperCase(); // 大寫StringBuffer sb = new StringBuffer(ss[i]);ss[i] = sb.replace(0, 1, up).toString();// 上邊已經把字符串數組的首字母該為大寫。然后對更改后的字符數組推斷是否有數字Matcher m = Pattern.compile("\\d+").matcher(ss[i]);// 0-9出現一次或多次while (m.find()) {// m.group():Returns the input subsequence matched by the previous matchString num = new String(m.group());String num2 = num;num2 = "_" + num + "_"; // 數字前后都加入"_"ss[i] = ss[i].replace(num, num2);if (ss[i].startsWith("_")) { // 去頭"_"ss[i] = ss[i].substring(1);}if (ss[i].endsWith("_")) { // 去尾"_"ss[i] = ss[i].substring(0, ss[i].length() - 1);}}}print(ss);} }總結
以上是生活随笔為你收集整理的Java算法--串的简单处理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux 内核调试方法
- 下一篇: java enum(枚举)的使用