字符串"1\n2\\n3\n4\\n5",匹配\\n,以下两种方式等价,第二种不容易出错,不用考虑字符串带来的问题,下载jar包。
package com.test; import org.apache.commons.lang3.StringUtils; public class Test12 { public static void main(String[] args) { String ss1 = "1\n2\\n3\n4\\n5"; System.out.println("本身字符串:"); System.out.println(ss1); String[] array = ss1.split("\\\\n"); for (int i = 0; i < array.length; i++) { System.out.println("第" + (i + 1) + "个:" + array[i] + " "); } System.out.println(); String[] array1 = StringUtils.splitByWholeSeparator(ss1,"\\n"); for (int i = 0; i < array1.length; i++) { System.out.println("第" + (i + 1) + "个:" + array1[i] + " "); } } }
原文链接:https://www.f2er.com/regex/359061.html