分享知識-快樂自己:關於 String 小案例
阿新 • • 發佈:2018-12-31
|| demo 重復 任意進制轉換 括號 保存 bst reverse tostring
單個字符出現的次數:
/*** * 驗證是否符合拆分條件 * * @param text * 原字符串 * @param sub * 判斷條件 * @return */ public static int count(String text, String sub) { int count = 0, start = 0; while ((start = text.indexOf(sub, start)) >= 0) { start+= sub.length(); count++; } return count; }
一刀切上式(事先補齊):
@Test public void demo1() { String a = "ab bc,cd,ef gh"; String result = ""; //保存結果 a += ","; //事先補齊 ab bc,cd,ef gh, for (int i = 0; i < a.length(); i++) { char c = a.charAt(i); //獲取每一個字符 if (c == ‘ ‘ || c == ‘,‘) { System.out.print(result); result = ""; } else { result += c; } } }
一刀切下式(事後修正):
@Test public void demo2() { String b = "abcdefg"; //結果a,b,c,d,e,f,g String result = ""; for (inti = 0; i < b.length(); i++) { result += "," + b.charAt(i);//獲取每一個字符 //,a,b,c,d,e,f,g } result = result.substring(1); System.out.println(result); }
字符串的常用API (假設修正法)
@Test public void demo3() { String b = "abcdefgabcd"; //求字符串中是否有重復的字符 boolean flag = false; // 假設不重復 for (int i = 0; i < b.length(); i++) { char c = b.charAt(i);//獲取每一個字符 if (b.lastIndexOf(c) != i) { flag = true; //重復 break; } } }
字符串的常用API (正則表達式)split
@Test public void demo4() { String a="a b c d e f g"; String [] str=a.split(" +"); //a.split(" {1,}"); for(String s:str){ System.out.print(s); } }
字符串替換
@Test public void demoText1() { String str = "a,A,b,c,d,G,h,-Z"; char[] chars = str.toCharArray(); for (int i = 0; i < str.length(); i++) { int count = (int) chars[i]; if ((count >= 65 && count <= 90) || (count >= 97 && count <= 127)) { System.out.print(chars[i]); } } }
字符串中單個字符出現的次數
@Test public void demoText2() { String text = "wefawefaafickdbbjoasdfcweccwerqeasdaajfnblsdbboioe"; StringBuffer sb = new StringBuffer(); char[] chars = text.toCharArray(); Map<String, Integer> map = new HashMap<String, Integer>(); for (int i = 0; i < chars.length; i++) { Pattern pattern = Pattern.compile("(" + chars[i] + ")");//正則表達式 匹配 aa或bb或bb Matcher matcher = pattern.matcher(text); String t = ""; while (matcher.find()) { t += matcher.group(); } map.put(t.substring(0, 1), Integer.valueOf(t.length())); } Set<Map.Entry<String, Integer>> entrys = map.entrySet(); for (Map.Entry<String, Integer> entry : entrys) { String key = entry.getKey(); Integer value = entry.getValue(); System.out.println(key + "----" + value); } System.out.println("-----------------------------------------"); String a = "ab bc,cd,ef gh"; String result = ""; //保存結果 a += ","; //事先補齊 ab bc,cd,ef gh, for (int i = 0; i < a.length(); i++) { char c = a.charAt(i); if ((c == ‘ ‘) || (c == ‘,‘)) { System.out.print(result); result = ""; } else { result += c; } } }
只輸出英文字符
@Test public void demoText3() { String str1 = "ab *s-fh*-d-+bc,cd,ef gh"; char[] chars = str1.toCharArray(); for (int i = 0; i < str1.length(); i++) { int count = (int) chars[i]; if ((count >= 65 && count >= 90) || (count >= 97 && count >= 127)) { System.out.print(chars[i]); } } }
任意進制轉換首先將3進制轉換為10進制,然後再把10進制轉換5進制
@Test public void demoText4() { String num = "200110102"; // 3進制的數字轉換成5進制的 int result = 0; for (int i = 0; i < num.length(); i++) { char c = num.charAt(i); result = result * 3 + (c - ‘0‘); } System.out.println(result); //十進制 13457 String str = ""; while (true) { if (result == 0) break; str += result % 5; result = result / 5; } System.out.println(str); }
字符串和數字的轉換
@Test public void demoText5() { String str = "6789"; //字符串 需要轉換成數字 6789 int num = str.charAt(0) - ‘0‘; // 6 num = num * 10 + str.charAt(1) - ‘0‘; num = num * 10 + str.charAt(2) - ‘0‘; num = num * 10 + str.charAt(3) - ‘0‘; System.out.println(num); System.out.println("------------------------------"); String str1 = "6789"; //字符串 需要轉換成數字 6789 int result = 0; for (int i = 0; i < str.length(); i++) { result = result * 10 + str.charAt(i) - ‘0‘; } System.out.println(result); }
枚舉與剪枝 求位數需求:數字自身平方的尾數=自身 (100W以內)
@Test public void demoText6() { for (int a = 0; a <= 1000000; a++) { //求a的平方 int num = a * a; if (num % 10 == a || num % 100 == a || num % 1000 == a || num % 10000 == a || num % 100000 == a || num % 1000000 == a) { System.out.println("數字本身是=》" + a + "數字的平方是=》" + num); } } }
雞兔同籠
/** * 雞兔同籠 * 雞兔共50頭,腳共120只! * x+y=50; * 2x+4y=120; */ @Test public void demoText7() { for (int x = 0; x <= 50; x++) { int y = 50 - x; //得到兔子的數量 if (2 * x + 4 * y == 120) { System.out.println(x); System.out.print(y); } } }
判斷某個字符串是否滿足我們定義的正則表達式
@Test public void demoText8() { String a = "ABC01234"; boolean matches = a.matches("[A-Z]{1,3}[0-9]{3,5}");//返回boolean System.out.println(matches); }
字符串的常用API (正則表達式)replaceAll()在小括號裏面的內容我們稱之為子組,用$獲取子組內容
@Test public void demoText9() { String a = "asasasa 2018-07-16 sasa sasas sasa"; //實現的效果是asasasa 07/16/2018年 sasa sasas sasa a = a.replaceAll("([0-9]{4})-([0-9]{2})-([0-9]{2})", "$2/$3/$1 年"); System.out.print(a); }
文字正序輸出,並顯示字母出現次數
public static void main(String[] args) { String str = "服微端雲入加迎歡|welcome join us !"; int flag=0; String str1=str.substring(0, 8); String str2=str.substring(9); StringBuffer sb1=new StringBuffer(str1); sb1.reverse(); String newStr=sb1.append(str2).toString(); System.out.println(newStr); for (int i = 0; i < str2.length(); i++) { char letter=str2.charAt(i); if (String.valueOf(letter).matches("^[a-zA-Z]$")) { flag++; } } System.out.println("英文字符有:"+flag+"個"); }
分享知識-快樂自己:關於 String 小案例