标签:asd att fga 自身 array com als dem pat
/*** * 验证是否符合拆分条件 * * @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); }
@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; } } }
@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]); } } }
@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); }
@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); }
@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+"个"); }
标签:asd att fga 自身 array com als dem pat
原文地址:https://www.cnblogs.com/mlq2017/p/10200919.html