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