分享知识-快乐自己:关于 String 小案例
单个字符出现的次数:
/*** * 验证是否符合拆分条件 * * @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 (int i = 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+"个"); }