I have following String and i want to split this string into number of sub strings(by taking ‘,’ as a delimeter) when its length reaches 36. Its not exactly splitting on 36’th position
String message = "This is some(sampletext), and has to be splited properly";
I want to get the output as two substrings follows:
1. ‘This is some (sampletext)’
2. ‘and has to be splited properly’
Thanks in advance.
A solution based on regex:
Update:
trim() can be droped by changing the pattern to end in space or end of string:
group(1) means that I only need the first part of the pattern (.{1,15}) as output.
.{1,15} – a sequence of any characters (“.”) with any length between 1 and 15 ({1,15})
\b – a word break (a non-character before of after any word)
( |$) – space or end of string
In addition I’ve added () surrounding .{1,15} so I can use it as a whole group (m.group(1)).
Depending on the desired result, this expression can be tweaked.
Update:
If you want to split message by comma only if it’s length would be over 36, try the following expression: