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

时间:2012-05-24 10:25:29

标签: java string string-split

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

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

我想得到两个子串的结果:
1. '这是一些(sampletext)' 2.'并且必须妥善分割'

提前致谢。

5 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:3)

这应该适用于所有输入,除非存在不超过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);
  }

答案 2 :(得分:3)

这是一个整洁的答案:

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());

答案 3 :(得分:3)

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

    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(,|$)");

答案 4 :(得分:2)

如果你有一个简单的文字就像你上面显示的那样(用空格分隔的单词),你总能想到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());
                }
            }
        }

    }
相关问题