情绪上的紧张,让人真的很累。java
package com.test.regex; import java.util.Arrays; public class Demo06 { public static void main(String[] args) { /* 1. 切割字符串,获得字符串数组 * 2. 遍历字符串数组,将其转换为int类型,并装入int[]数组 * 3. 对int[]数组进行排序 * 4. 拼接int[]数组 * 5. 输出 */ String s = "91 27 46 38 50"; String[] sArr = s.split(" "); int[] arr = new int[5]; for(int i = 0; i < sArr.length; i++){ arr[i] =Integer.parseInt(sArr[i]); } Arrays.sort(arr); //这种方法不推荐,让内存中产生了太多垃圾 String str = new String(); for (int i = 0; i < arr.length; i++) { if(i!= arr.length - 1){ str = str + arr[i] + " "; } else { str = str + arr[i]; } } System.out.println(str); //这种方式推荐【还有更好的,用方法链】 StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.length; i++) { if(i!=arr.length-1){ sb.append(arr[i]+" "); } else { sb.append(arr[i]); } } System.out.println(sb); } }
public String replaceAll(String regex,String replacement)正则表达式
package com.test.regex; public class Demo07 { public static void main(String[] args) { String s = "wo111ai2222shi3333jie"; String regex = "\\d"; String s2 = s.replaceAll(regex,"-"); System.out.println(s2); } }
叠词“快快乐乐”与“搞起搞起”的正则表达式编程
package com.test.regex; public class Demo08 { public static void main(String[] args) { //叠词“快快乐乐”“高高兴兴” String s1 = "快快乐乐"; String s2 = "高高兴兴"; String regex = "(.)\\1(.)\\2"; // \\1表明让第一组再出现一次,\\2表明让第二组再出现一次 boolean a1 = s1.matches(regex); boolean a2 = s2.matches(regex); System.out.println(a1); System.out.println(a2); //叠词“搞起搞起” String s3 = "搞起搞起"; String regex2 = "(..)\\1"; boolean a3 = s3.matches(regex2); System.out.println(a3); } }
按照叠词切割“sdqqfgkkkhjppppkl”数组
package com.test.regex; public class Demo09 { public static void main(String[] args) { String s = "sdqqfgkkkhjppppkl"; String regex = "(.)\\1+"; // + 表明出现一次到屡次【我的认为,可想象(.)与+之间是相乘关系】 String[] sArr = s.split(regex); for(int i = 0 ; i < sArr.length; i++){ System.out.println(sArr[i]); } } }
将“我我....我...我.要...要要...要学....学学..学.编..编编.编.程.程.程..程”转换为“我要学编程”app
package com.test.regex; public class Demo10 { public static void main(String[] args) { String s = "我我....我...我.要...要要...要学....学学..学.编..编编.编.程.程.程..程"; String s2 = s.replaceAll("\\.", ""); String s3 = s2.replaceAll("(.)\\1+", "$1"); //$1表明第一组中的内容 System.out.println(s3); String a = "快快乐乐"; String regex = "(.)\\1"; String a2 = a.replaceAll(regex, "$1"); System.out.println(a2); } }
package com.test.regex; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo11 { public static void main(String[] args) { //模式和匹配器的典型调用顺序 Pattern p = Pattern.compile("a*b"); Matcher m = p.matcher("aaaaab"); boolean b = m.matches(); System.out.println(b); //等同于 System.out.println("aaaaab".matches("a*b")); } }
区分find()方法与matches()方法。dom
package com.test.regex; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo12 { public static void main(String[] args) { String s = "个人号码:18500238888,你的号码:13488885555,他的号码:18752222222"; Pattern p = Pattern.compile("1[387]\\d{9}"); Matcher m = p.matcher(s); while(m.find()){ //必须find()找一次,用group()才能打印 System.out.println(m.group()); } } }
用于生成伪随机数ui
Random():空参构造生成伪随机数,是利用当前纳秒值。code
package com.test.regex; import java.util.Random; public class Demo13 { public static void main(String[] args) { Random r = new Random(); for(int i = 0; i < 10; i++){ int x = r.nextInt(); System.out.println(x); } } }
Random(long a):提供一个种子生成伪随机数【每一次运行结果相同】排序
package com.test.regex; import java.util.Random; public class Demo13 { public static void main(String[] args) { Random r = new Random(1000); int a = r.nextInt(); int b = r.nextInt(); System.out.println(a); System.out.println(b); } }
nextInt():生成Int取值范围内的伪随机数内存
package com.test.regex; import java.util.Random; public class Demo13 { public static void main(String[] args) { Random r = new Random(); for(int i = 0; i < 10; i++){ int a = r.nextInt(); System.out.println(a); } } }
nextInt(100):生成0-99的伪随机数【要变成1-100,加1便可】
package com.test.regex; import java.util.Random; public class Demo13 { public static void main(String[] args) { Random r = new Random(); for(int i = 0; i < 100; i++){ int a = r.nextInt(100); System.out.println(a); } } }