Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Groovy
Posted by: Jenya Mia
Added: Feb 19, 2020 7:16 PM
Views: 14
Tags: no tags
  1.  /**
  2.      * Split word in string to specific length separated by space
  3.      *
  4.      * @param stringToSplit
  5.      * @param length
  6.      * @return string fixed length string
  7.      */
  8.     static String splitStringByFixedLength(String stringToSplit, int length){
  9.         List<String> splitToLength = []
  10.         if(stringToSplit?.length() > length)
  11.         {
  12.             stringToSplit.split('\\s+').each {
  13.                 splitToLength.addAll(Splitter.fixedLength(length).splitToList(it.trim()))
  14.             }
  15.             stringToSplit = String.join(" ", splitToLength)
  16.         }
  17.  
  18.         return stringToSplit
  19.     }