Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- fun createParagraphs(input: String): String {
- val stringBuilder = StringBuilder()
- var currentParagraph = StringBuilder()
- var currentLength = 0
- val sentences = input.split(Regex("(?<=[.!?])\\s+"))
- for (sentence in sentences) {
- if (currentLength + sentence.length + 1 <= 300) { // +1 for space
- currentParagraph.append("$sentence ")
- currentLength += sentence.length + 1
- } else {
- stringBuilder.append(currentParagraph.trimEnd())
- stringBuilder.append("\n\n")
- currentParagraph.clear()
- currentParagraph.append("$sentence ")
- currentLength = sentence.length + 1
- }
- }
- // Append any remaining text as the last paragraph
- if (currentParagraph.isNotEmpty()) {
- stringBuilder.append(currentParagraph.trimEnd())
- }
- return stringBuilder.toString()
- }
- fun main() {
- val inputString = "your string"
- val result = createParagraphs(inputString)
- println(result)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement