0

I was just wondering it here is an API or some easy and quick way to split String at given index into String[] array but if there is a word at that index then put it to other String.

So lets say I have a string: "I often used to look out of the window, but I rarely do that anymore"

The length of that string is 68 and I have to cut it at 36, which is in this given sentence n, but now it should split the word at the so that the array would be ["I often used to look out of the", "window, but I rarely do that anymore"].

And if the new sentence is longer than 36 then it should be split aswell, so if I had a bit longer sentence: "I often used to look out of the window, but I rarely do that anymore, even though I liked it"
Would be ["I often used to look out of the", "window, but I rarely do that anymore", ",even though I liked it"]

1
  • @PM77-1 That question uses StringTokenizer. Now it can be done with String#split() Commented Sep 18, 2018 at 19:24

2 Answers 2

1

Here's an old-fashioned, non-stream, non-regex solution:

public static List<String> chunk(String s, int limit) { List<String> parts = new ArrayList<String>(); while(s.length() > limit) { int splitAt = limit-1; for(;splitAt>0 && !Character.isWhitespace(s.charAt(splitAt)); splitAt--); if(splitAt == 0) return parts; // can't be split parts.add(s.substring(0, splitAt)); s = s.substring(splitAt+1); } parts.add(s); return parts; } 

This doesn't trim additional spaces either side of the split point. Also, if a string cannot be split, because it doesn't contain any whitespace in the first limit characters, then it gives up and returns the partial result.

Test:

public static void main(String[] args) { String[] tests = { "This is a short string", "This sentence has a space at chr 36 so is a good test", "I often used to look out of the window, but I rarely do that anymore, even though I liked it", "I live in Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch", }; int limit = 36; for(String s : tests) { List<String> chunks = chunk(s, limit); for(String st : chunks) System.out.println("|" + st + "|"); System.out.println(); } } 

Output:

|This is a short string| |This sentence has a space at chr 36| |so is a good test| |I often used to look out of the| |window, but I rarely do that| |anymore, even though I liked it| |I live in| 
Sign up to request clarification or add additional context in comments.

Comments

1

This matches between 1 and 30 characters repetitively (greedy) and requires a whitespace behind each match.

public static List<String> chunk(String s, int size) { List<String> chunks = new ArrayList<>(s.length()/size+1); Pattern pattern = Pattern.compile(".{1," + size + "}(=?\\s|$)"); Matcher matcher = pattern.matcher(s); while (matcher.find()) { chunks.add(matcher.group()); } return chunks; } 

Note that it doesn't work if there's a long string (>size) whitout whitespace.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.