How to split string in java based on constraint

I have the following line and I want to split this line into the number of substrings (taking "," as a delimeter) when its length reaches 36. It does not completely split at 36th position

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

I want to get the result in the form of two substrings:
1. 'This is some (sample text)'
2. 'and should be split correctly

Thanks in advance.

+3
source share
5 answers

Regular Expression Solution:

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

Update: trim () can be removed by changing the template to the end in space or at the end of the line:

    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) means that I need only the first part of the template (. {1,15}).

. {1,15} - ( "." ) 1 15 ({1,15})

\ b - ( )

(| $) -

, () . {1,15}, (m.group(1)). .

: , 36, :

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

, , , . , 16- , . , , . .

+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);
  }
+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

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

    }
+2
source

All Articles