繁体   English   中英

如何根据限制在java中拆分字符串

[英]How to Split a string in java based on limit

我有跟随字符串,我想将此字符串拆分为多个子字符串(通过将','作为一个分隔符),当它的长度达到36时。它不完全分裂在第36位

      String message = "This is some(sampletext), and has to be splited properly";

我想获得输出,如下面的两个子串:
1. '这是一些(样本文本)'
2.'并且必须妥善分裂'

提前致谢。

我能想到的最好的解决方案是创建一个迭代字符串的函数。 在函数中,您可以跟踪空白字符,并且对于每个第16个位置,您可以根据最后遇到的空格的位置将子字符串添加到列表中。 找到子字符串后,从最后遇到的空格重新开始。 然后,您只需返回子字符串列表。

这应该适用于所有输入,除非存在没有大于16的空格的字符序列。它还通过索引到原始字符串来创建最少量的额外字符串。

  public static void main(String[] args) throws IOException
  {
    String message = "This is some sample text and has to be splited properly";
    List<String> result = new ArrayList<String>();
    int start = 0;
    while (start + 16 < message.length())
    {
      int end = start + 16;
      while (!Character.isWhitespace(message.charAt(end--)));
      result.add(message.substring(start, end + 1));
      start = end + 2;
    }
    result.add(message.substring(start));
    System.out.println(result);
  }

这是一个整洁的答案:

String message = "This is some sample text and has to be splited properly";

String[] temp = message.split("(?<=^.{1,16}) ");
String part1 = message.substring(0, message.length() - temp[temp.length - 1].length() - 1);
String part2 = message.substring(message.length() - temp[temp.length - 1].length());

基于正则表达式的解决方案:

    String s = "This is some sample text and has to be splited properly";
    Pattern splitPattern = Pattern.compile(".{1,15}\\b");
    Matcher m = splitPattern.matcher(s);
    List<String> stringList = new ArrayList<String>();
    while (m.find()) {
        stringList.add(m.group(0).trim());
    }

更新:trim()可以通过将模式更改为在空格或字符串结尾处结束来进行调整:

    String s = "This is some sample text and has to be splited properly";
    Pattern splitPattern = Pattern.compile("(.{1,15})\\b( |$)");
    Matcher m = splitPattern.matcher(s);
    List<String> stringList = new ArrayList<String>();
    while (m.find()) {
        stringList.add(m.group(1));
    }

group(1)表示我只需要模式的第一部分(。{1,15})作为输出。

。{1,15} - 任何字符(“。”)的序列,长度在1到15之间({1,15})

\\ b - 单词分隔(任何单词之前的非字符)

(| $) - 空格或字符串结尾

另外我添加了()周围。{1,15}所以我可以将它作为整个组使用(m.group(1))。 根据所需的结果,可以调整此表达式。

更新 :如果您希望仅在逗号长度超过36的情况下按逗号分割消息,请尝试以下表达式:

Pattern splitPattern = Pattern.compile("(.{1,36})\\b(,|$)");

如果您有一个简单的文本,就像上面显示的那样(用空格分隔的单词),您总能想到StringTokenizer 这是一些适用于您的案例的简单代码:

public static void main(String[] args) {

        String message = "This is some sample text and has to be splited properly";
        while (message.length() > 0) {
            String token = "";
            StringTokenizer st = new StringTokenizer(message);
            while (st.hasMoreTokens()) {
                String nt = st.nextToken();
                String foo = "";
                if (token.length()==0) {
                    foo = nt;
                }
                else {
                    foo = token + " " + nt;
                }
                if (foo.length() < 16)
                    token = foo;
                else {
                    System.out.print("'" + token + "' ");
                    message = message.substring(token.length() + 1, message.length());
                    break;
                }
                if (!st.hasMoreTokens()) {
                    System.out.print("'" + token + "' ");
                    message = message.substring(token.length(), message.length());
                }
            }
        }

    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM