赞
踩
-
- Pattern pattern = Pattern.compile("[,.?!;,。?!;]");//中括号需要;
- String[] split = pattern.split(content);
-
-
-
-
- String regex = "(\\。|\\.|\\?|\\?|\\;\\;\\!|\\!|\\\")";
- String[] split = contentSplit(content, regex);
-
-
-
- public String[] contentSplit(CharSequence input,String regex) {
- int index = 0;
- ArrayList<String> matchList = new ArrayList<>();
- String string = input.toString();
- Pattern pattern = Pattern.compile(regex);
- Matcher m = pattern.matcher(string);
-
- //这里是targetSdkVersion>28时的处理方法,其他情况查看Pattern源码
- while (m.find()) {
- //如果想去掉正则中的特殊字符,将“m.start() - 1”改成“m.start()”即可
- String match = input.subSequence(index, m.start() ).toString();
- matchList.add(match);
- index = m.end();
- }
-
- if (index == 0)
- return new String[]{input.toString()};
-
- matchList.add(input.subSequence(index, input.length()).toString());
-
- int resultSize = matchList.size();
- while (resultSize > 0 && matchList.get(resultSize - 1).equals(""))
- resultSize--;
- String[] result = new String[resultSize];
- return matchList.subList(0, resultSize).toArray(result);
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。