Advertisement
Upar

new paragraph (elsa speak)

May 25th, 2024 (edited)
539
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.02 KB | Source Code | 0 0
  1. fun createParagraphs(input: String): String {
  2.     val stringBuilder = StringBuilder()
  3.     var currentParagraph = StringBuilder()
  4.     var currentLength = 0
  5.  
  6.     val sentences = input.split(Regex("(?<=[.!?])\\s+"))
  7.  
  8.     for (sentence in sentences) {
  9.         if (currentLength + sentence.length + 1 <= 300) { // +1 for space
  10.             currentParagraph.append("$sentence ")
  11.             currentLength += sentence.length + 1
  12.         } else {
  13.             stringBuilder.append(currentParagraph.trimEnd())
  14.             stringBuilder.append("\n\n")
  15.             currentParagraph.clear()
  16.             currentParagraph.append("$sentence ")
  17.             currentLength = sentence.length + 1
  18.         }
  19.     }
  20.  
  21.     // Append any remaining text as the last paragraph
  22.     if (currentParagraph.isNotEmpty()) {
  23.         stringBuilder.append(currentParagraph.trimEnd())
  24.     }
  25.  
  26.     return stringBuilder.toString()
  27. }
  28.  
  29. fun main() {
  30.     val inputString = "your string"
  31.     val result = createParagraphs(inputString)
  32.     println(result)
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement