Split a paragraph into a list of strings, each not exceeding a given size and avoiding splitting words in...
up vote
2
down vote
favorite
Question
How can the following be done in an idiomatic way:
Split a large String
into a list of String
s, each not exceeding the given size, and avoiding splitting words in half.
Closest solution with String.chunked()
(Splits words)
The closest solution to this is using the String class's chunked()
method. However, the problem with this is that
it splits words in the given String
.
Code example of use of String.chunked()
val longString = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod " +
"tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, " +
"quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo " +
"consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse " +
"cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non " +
"proident, sunt in culpa qui officia deserunt mollit anim id est laborum. "
// Split [longString] into list
var listOfStrings = longString.chunked(40)
listOfStrings.forEach {
println(it)
}
Example output of closest example with String.chunked()
Below is the output received by running the example code provided. As can be seen, the words are split at the end of the lines.
Lorem ipsum dolor sit amet, consectetur
adipisicing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna ali
qua. Ut enim ad minim veniam, quis nostr
ud exercitation ullamco laboris nisi ut
aliquip ex ea commodo consequat. Duis au
te irure dolor in reprehenderit in volup
tate velit esse cillum dolore eu fugiat
nulla pariatur. Excepteur sint occaecat
cupidatat non proident, sunt in culpa qu
i officia deserunt mollit anim id est la
borum.
string kotlin
add a comment |
up vote
2
down vote
favorite
Question
How can the following be done in an idiomatic way:
Split a large String
into a list of String
s, each not exceeding the given size, and avoiding splitting words in half.
Closest solution with String.chunked()
(Splits words)
The closest solution to this is using the String class's chunked()
method. However, the problem with this is that
it splits words in the given String
.
Code example of use of String.chunked()
val longString = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod " +
"tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, " +
"quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo " +
"consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse " +
"cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non " +
"proident, sunt in culpa qui officia deserunt mollit anim id est laborum. "
// Split [longString] into list
var listOfStrings = longString.chunked(40)
listOfStrings.forEach {
println(it)
}
Example output of closest example with String.chunked()
Below is the output received by running the example code provided. As can be seen, the words are split at the end of the lines.
Lorem ipsum dolor sit amet, consectetur
adipisicing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna ali
qua. Ut enim ad minim veniam, quis nostr
ud exercitation ullamco laboris nisi ut
aliquip ex ea commodo consequat. Duis au
te irure dolor in reprehenderit in volup
tate velit esse cillum dolore eu fugiat
nulla pariatur. Excepteur sint occaecat
cupidatat non proident, sunt in culpa qu
i officia deserunt mollit anim id est la
borum.
string kotlin
1
Dunno about kotlin, but I would first split into single words, then reassemble words into strings that do not exceed limit
– ravenspoint
Nov 22 at 15:36
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
Question
How can the following be done in an idiomatic way:
Split a large String
into a list of String
s, each not exceeding the given size, and avoiding splitting words in half.
Closest solution with String.chunked()
(Splits words)
The closest solution to this is using the String class's chunked()
method. However, the problem with this is that
it splits words in the given String
.
Code example of use of String.chunked()
val longString = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod " +
"tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, " +
"quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo " +
"consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse " +
"cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non " +
"proident, sunt in culpa qui officia deserunt mollit anim id est laborum. "
// Split [longString] into list
var listOfStrings = longString.chunked(40)
listOfStrings.forEach {
println(it)
}
Example output of closest example with String.chunked()
Below is the output received by running the example code provided. As can be seen, the words are split at the end of the lines.
Lorem ipsum dolor sit amet, consectetur
adipisicing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna ali
qua. Ut enim ad minim veniam, quis nostr
ud exercitation ullamco laboris nisi ut
aliquip ex ea commodo consequat. Duis au
te irure dolor in reprehenderit in volup
tate velit esse cillum dolore eu fugiat
nulla pariatur. Excepteur sint occaecat
cupidatat non proident, sunt in culpa qu
i officia deserunt mollit anim id est la
borum.
string kotlin
Question
How can the following be done in an idiomatic way:
Split a large String
into a list of String
s, each not exceeding the given size, and avoiding splitting words in half.
Closest solution with String.chunked()
(Splits words)
The closest solution to this is using the String class's chunked()
method. However, the problem with this is that
it splits words in the given String
.
Code example of use of String.chunked()
val longString = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod " +
"tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, " +
"quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo " +
"consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse " +
"cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non " +
"proident, sunt in culpa qui officia deserunt mollit anim id est laborum. "
// Split [longString] into list
var listOfStrings = longString.chunked(40)
listOfStrings.forEach {
println(it)
}
Example output of closest example with String.chunked()
Below is the output received by running the example code provided. As can be seen, the words are split at the end of the lines.
Lorem ipsum dolor sit amet, consectetur
adipisicing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna ali
qua. Ut enim ad minim veniam, quis nostr
ud exercitation ullamco laboris nisi ut
aliquip ex ea commodo consequat. Duis au
te irure dolor in reprehenderit in volup
tate velit esse cillum dolore eu fugiat
nulla pariatur. Excepteur sint occaecat
cupidatat non proident, sunt in culpa qu
i officia deserunt mollit anim id est la
borum.
string kotlin
string kotlin
edited Nov 22 at 16:07
Poul Bak
5,42831132
5,42831132
asked Nov 22 at 15:32
AliAvci
692713
692713
1
Dunno about kotlin, but I would first split into single words, then reassemble words into strings that do not exceed limit
– ravenspoint
Nov 22 at 15:36
add a comment |
1
Dunno about kotlin, but I would first split into single words, then reassemble words into strings that do not exceed limit
– ravenspoint
Nov 22 at 15:36
1
1
Dunno about kotlin, but I would first split into single words, then reassemble words into strings that do not exceed limit
– ravenspoint
Nov 22 at 15:36
Dunno about kotlin, but I would first split into single words, then reassemble words into strings that do not exceed limit
– ravenspoint
Nov 22 at 15:36
add a comment |
2 Answers
2
active
oldest
votes
up vote
3
down vote
accepted
Not really the most idiomatic way I found, but maybe it suffices your needs:
fun String.chunkedWords(limitChars: Int,
delimiter: Char = ' ',
joinCharacter: Char = 'n') =
splitToSequence(delimiter)
.reduce { cumulatedString, word ->
val exceedsSize = cumulatedString.length - cumulatedString.indexOfLast { it == joinCharacter } + "$delimiter$word".length > limitChars
cumulatedString + if (exceedsSize) {
joinCharacter
} else {
delimiter
} + word
}
You can then use it as follows:
longText.chunkedWords(40).run(::println)
which for your given string would then print:
Lorem ipsum dolor sit amet, consectetur
adipisicing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna
aliqua. Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris
nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit
in voluptate velit esse cillum dolore eu
fugiat nulla pariatur. Excepteur sint
occaecat cupidatat non proident, sunt in
culpa qui officia deserunt mollit anim
id est laborum.
You could also split it to lines from there, e.g. longText.chunkedWords(40).splitAsSequence("n")
. Note that it also splits nicely if there are already new-line characters in the string, i.e. if you have a String
like "Testing shorter lines.nAnd now there comes a very long line"
a call of .chunkedWords(17)
will produce the following output:
Testing shorter
lines.
And now there // this tries to use the whole 17 characters again
comes a very
long line
This is really nice
– AliAvci
Nov 22 at 17:39
add a comment |
up vote
1
down vote
You could use this simple helper function:
fun splitIntoChunks(max: Int, string: String): List<String> = ArrayList<String>(string.length / max + 1).also {
var firstWord = true
val builder = StringBuilder()
// split string by whitespace
for (word in string.split(Regex("( |n|r|nr)+"))) {
// if the current string exceeds the max size
if (builder.length + word.length > max) {
// then we add the string to the list and clear the builder
it.add(builder.toString())
builder.setLength(0)
firstWord = true
}
// append a space at the beginning of each word, except the first one
if (firstWord) firstWord = false else builder.append(' ')
builder.append(word)
}
// add the last collected part if there was any
if(builder.isNotEmpty()){
it.add(builder.toString())
}
}
Which then can be called simply like this:
val chunks: List<String> = splitIntoChunks(20, longString)
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
Not really the most idiomatic way I found, but maybe it suffices your needs:
fun String.chunkedWords(limitChars: Int,
delimiter: Char = ' ',
joinCharacter: Char = 'n') =
splitToSequence(delimiter)
.reduce { cumulatedString, word ->
val exceedsSize = cumulatedString.length - cumulatedString.indexOfLast { it == joinCharacter } + "$delimiter$word".length > limitChars
cumulatedString + if (exceedsSize) {
joinCharacter
} else {
delimiter
} + word
}
You can then use it as follows:
longText.chunkedWords(40).run(::println)
which for your given string would then print:
Lorem ipsum dolor sit amet, consectetur
adipisicing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna
aliqua. Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris
nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit
in voluptate velit esse cillum dolore eu
fugiat nulla pariatur. Excepteur sint
occaecat cupidatat non proident, sunt in
culpa qui officia deserunt mollit anim
id est laborum.
You could also split it to lines from there, e.g. longText.chunkedWords(40).splitAsSequence("n")
. Note that it also splits nicely if there are already new-line characters in the string, i.e. if you have a String
like "Testing shorter lines.nAnd now there comes a very long line"
a call of .chunkedWords(17)
will produce the following output:
Testing shorter
lines.
And now there // this tries to use the whole 17 characters again
comes a very
long line
This is really nice
– AliAvci
Nov 22 at 17:39
add a comment |
up vote
3
down vote
accepted
Not really the most idiomatic way I found, but maybe it suffices your needs:
fun String.chunkedWords(limitChars: Int,
delimiter: Char = ' ',
joinCharacter: Char = 'n') =
splitToSequence(delimiter)
.reduce { cumulatedString, word ->
val exceedsSize = cumulatedString.length - cumulatedString.indexOfLast { it == joinCharacter } + "$delimiter$word".length > limitChars
cumulatedString + if (exceedsSize) {
joinCharacter
} else {
delimiter
} + word
}
You can then use it as follows:
longText.chunkedWords(40).run(::println)
which for your given string would then print:
Lorem ipsum dolor sit amet, consectetur
adipisicing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna
aliqua. Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris
nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit
in voluptate velit esse cillum dolore eu
fugiat nulla pariatur. Excepteur sint
occaecat cupidatat non proident, sunt in
culpa qui officia deserunt mollit anim
id est laborum.
You could also split it to lines from there, e.g. longText.chunkedWords(40).splitAsSequence("n")
. Note that it also splits nicely if there are already new-line characters in the string, i.e. if you have a String
like "Testing shorter lines.nAnd now there comes a very long line"
a call of .chunkedWords(17)
will produce the following output:
Testing shorter
lines.
And now there // this tries to use the whole 17 characters again
comes a very
long line
This is really nice
– AliAvci
Nov 22 at 17:39
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
Not really the most idiomatic way I found, but maybe it suffices your needs:
fun String.chunkedWords(limitChars: Int,
delimiter: Char = ' ',
joinCharacter: Char = 'n') =
splitToSequence(delimiter)
.reduce { cumulatedString, word ->
val exceedsSize = cumulatedString.length - cumulatedString.indexOfLast { it == joinCharacter } + "$delimiter$word".length > limitChars
cumulatedString + if (exceedsSize) {
joinCharacter
} else {
delimiter
} + word
}
You can then use it as follows:
longText.chunkedWords(40).run(::println)
which for your given string would then print:
Lorem ipsum dolor sit amet, consectetur
adipisicing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna
aliqua. Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris
nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit
in voluptate velit esse cillum dolore eu
fugiat nulla pariatur. Excepteur sint
occaecat cupidatat non proident, sunt in
culpa qui officia deserunt mollit anim
id est laborum.
You could also split it to lines from there, e.g. longText.chunkedWords(40).splitAsSequence("n")
. Note that it also splits nicely if there are already new-line characters in the string, i.e. if you have a String
like "Testing shorter lines.nAnd now there comes a very long line"
a call of .chunkedWords(17)
will produce the following output:
Testing shorter
lines.
And now there // this tries to use the whole 17 characters again
comes a very
long line
Not really the most idiomatic way I found, but maybe it suffices your needs:
fun String.chunkedWords(limitChars: Int,
delimiter: Char = ' ',
joinCharacter: Char = 'n') =
splitToSequence(delimiter)
.reduce { cumulatedString, word ->
val exceedsSize = cumulatedString.length - cumulatedString.indexOfLast { it == joinCharacter } + "$delimiter$word".length > limitChars
cumulatedString + if (exceedsSize) {
joinCharacter
} else {
delimiter
} + word
}
You can then use it as follows:
longText.chunkedWords(40).run(::println)
which for your given string would then print:
Lorem ipsum dolor sit amet, consectetur
adipisicing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna
aliqua. Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris
nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit
in voluptate velit esse cillum dolore eu
fugiat nulla pariatur. Excepteur sint
occaecat cupidatat non proident, sunt in
culpa qui officia deserunt mollit anim
id est laborum.
You could also split it to lines from there, e.g. longText.chunkedWords(40).splitAsSequence("n")
. Note that it also splits nicely if there are already new-line characters in the string, i.e. if you have a String
like "Testing shorter lines.nAnd now there comes a very long line"
a call of .chunkedWords(17)
will produce the following output:
Testing shorter
lines.
And now there // this tries to use the whole 17 characters again
comes a very
long line
edited Nov 22 at 17:13
answered Nov 22 at 16:27
Roland
9,16711141
9,16711141
This is really nice
– AliAvci
Nov 22 at 17:39
add a comment |
This is really nice
– AliAvci
Nov 22 at 17:39
This is really nice
– AliAvci
Nov 22 at 17:39
This is really nice
– AliAvci
Nov 22 at 17:39
add a comment |
up vote
1
down vote
You could use this simple helper function:
fun splitIntoChunks(max: Int, string: String): List<String> = ArrayList<String>(string.length / max + 1).also {
var firstWord = true
val builder = StringBuilder()
// split string by whitespace
for (word in string.split(Regex("( |n|r|nr)+"))) {
// if the current string exceeds the max size
if (builder.length + word.length > max) {
// then we add the string to the list and clear the builder
it.add(builder.toString())
builder.setLength(0)
firstWord = true
}
// append a space at the beginning of each word, except the first one
if (firstWord) firstWord = false else builder.append(' ')
builder.append(word)
}
// add the last collected part if there was any
if(builder.isNotEmpty()){
it.add(builder.toString())
}
}
Which then can be called simply like this:
val chunks: List<String> = splitIntoChunks(20, longString)
add a comment |
up vote
1
down vote
You could use this simple helper function:
fun splitIntoChunks(max: Int, string: String): List<String> = ArrayList<String>(string.length / max + 1).also {
var firstWord = true
val builder = StringBuilder()
// split string by whitespace
for (word in string.split(Regex("( |n|r|nr)+"))) {
// if the current string exceeds the max size
if (builder.length + word.length > max) {
// then we add the string to the list and clear the builder
it.add(builder.toString())
builder.setLength(0)
firstWord = true
}
// append a space at the beginning of each word, except the first one
if (firstWord) firstWord = false else builder.append(' ')
builder.append(word)
}
// add the last collected part if there was any
if(builder.isNotEmpty()){
it.add(builder.toString())
}
}
Which then can be called simply like this:
val chunks: List<String> = splitIntoChunks(20, longString)
add a comment |
up vote
1
down vote
up vote
1
down vote
You could use this simple helper function:
fun splitIntoChunks(max: Int, string: String): List<String> = ArrayList<String>(string.length / max + 1).also {
var firstWord = true
val builder = StringBuilder()
// split string by whitespace
for (word in string.split(Regex("( |n|r|nr)+"))) {
// if the current string exceeds the max size
if (builder.length + word.length > max) {
// then we add the string to the list and clear the builder
it.add(builder.toString())
builder.setLength(0)
firstWord = true
}
// append a space at the beginning of each word, except the first one
if (firstWord) firstWord = false else builder.append(' ')
builder.append(word)
}
// add the last collected part if there was any
if(builder.isNotEmpty()){
it.add(builder.toString())
}
}
Which then can be called simply like this:
val chunks: List<String> = splitIntoChunks(20, longString)
You could use this simple helper function:
fun splitIntoChunks(max: Int, string: String): List<String> = ArrayList<String>(string.length / max + 1).also {
var firstWord = true
val builder = StringBuilder()
// split string by whitespace
for (word in string.split(Regex("( |n|r|nr)+"))) {
// if the current string exceeds the max size
if (builder.length + word.length > max) {
// then we add the string to the list and clear the builder
it.add(builder.toString())
builder.setLength(0)
firstWord = true
}
// append a space at the beginning of each word, except the first one
if (firstWord) firstWord = false else builder.append(' ')
builder.append(word)
}
// add the last collected part if there was any
if(builder.isNotEmpty()){
it.add(builder.toString())
}
}
Which then can be called simply like this:
val chunks: List<String> = splitIntoChunks(20, longString)
edited Nov 23 at 8:51
Roland
9,16711141
9,16711141
answered Nov 22 at 16:01
Lino
6,98221936
6,98221936
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53434185%2fsplit-a-paragraph-into-a-list-of-strings-each-not-exceeding-a-given-size-and-av%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Dunno about kotlin, but I would first split into single words, then reassemble words into strings that do not exceed limit
– ravenspoint
Nov 22 at 15:36