簡體   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