Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7854881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:13:16+00:00 2026-06-02T20:13:16+00:00

I have a String which contains some formatted content with LineFeed after each line.

  • 0

I have a String which contains some formatted content with LineFeed after each line. I want to format the content of that variable to restrict each line to have not more than 80 characters.

Can somebody help me with this in Groovy?

for testing purpose I copied the content in a file

String fileContents = new File('E://Projects//temp//license').text
println fileContents

fileContents content or Console Output

List of connectivities are:
    Valid [Metadata Exchange for Microsoft Visio]
   Valid [Metadata Exchange for Microstrategy]
   Valid [Metadata Exchange for Microsoft SQL Server Reporting Services and Analysis Services]
   Valid [Metadata Exchange for Netezza]
   Valid [Metadata Exchange for Oracle]
   Valid [Metadata Exchange for Oracle BI Enterprise Edition]
   Valid [Metadata Exchange for Oracle Designer]

Command ran successfully

Update

This is what I am using after tim_yates answer

def es=lic.entrySet()
xml.licInfo() {
    int i=0
    es.each{
        if(!it.key.contains("failed with error"))
        {
            String val=new String(it.value)
            license(name:it.key,value:trimOutput(val),assignedTo:resultRows[i++])

        }
    }       
}

def trimOutput(text)
{

    text=text.tokenize( '\n' )*.toList()*.collate(90)*.collect { it.join() }.flatten().join( '\n' )
    text
}

but is gives me the following exception

Exception in thread "main" groovy.lang.MissingMethodException: No signature of method: java.util.ArrayList.collate() is applicable for argument types: (java.lang.Integer) values: [90]
Possible solutions: clone(), collect(groovy.lang.Closure), collect(groovy.lang.Closure), clear(), clear(), clear()

More update (console output of println es)

[license_all =Edition:          BAAC Standard
Software Version:  6.5
Distributed by:    ABC
Issued on:         2012-Feb-06
Validity period:   Non-Expiry
Serial number:     210502
Deployment level:  Production

List of supported platforms are:
   [All operating systems] is authorized for [100] logical CPUs
Number of authorized repository instances: 100
Number of authorized CAL usage count: 100

List of connectivities are:

   Valid [Metadata Exchange for Microsoft SQL Server Reporting Services and Analysis Services]
   Valid [Metadata Exchange for Netezza]
   Valid [Metadata Exchange for Oracle]
   Valid [Metadata Exchange for Oracle BI Enterprise Edition]
   Valid [Metadata Exchange for Oracle Designer]
   Valid [Metadata Exchange for Oracle Warehouse Builder]
   Valid [Metadata Exchange for Popkin System Architect]
   Valid [Metadata Exchange for SAP R/3]
   Valid [Metadata Exchange for Select SE]
   Valid [Metadata Exchange for Silverrun - RDM]
   Valid [Metadata Exchange for SQL Server]
   Valid [Metadata Exchange for Sybase ASE]
   Valid [Metadata Exchange for Sybase PowerDesigner]
   Valid [Metadata Exchange for Teradata]

Command ran successfully.
]
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T20:13:17+00:00Added an answer on June 2, 2026 at 8:13 pm

    Here’s two different methods depending on what you want to do with lines over 80 chars in length

    def text = '''List of connectivities are:
                 |    Valid [Metadata Exchange for Microsoft Visio]
                 |   Valid [Metadata Exchange for Microstrategy]
                 |   Valid [Metadata Exchange for Microsoft SQL Server Reporting Services and Analysis Services]
                 |   Valid [Metadata Exchange for Netezza]
                 |   Valid [Metadata Exchange for Oracle]
                 |   Valid [Metadata Exchange for Oracle BI Enterprise Edition]
                 |   Valid [Metadata Exchange for Oracle Designer]
                 |
                 |Command ran successfully'''.stripMargin()
    
    // Strip everything after 80 chars
    println text.tokenize( '\n' )*.  // Split the lines based on newline character
                 take( 80 ).         // Only take upto the first 80 chars of each String
                 join( '\n' )        // And join them back together with '\n' between them
    
    // Add newline if line is over 80 chars
    println text.tokenize( '\n' )*.      // Split the lines based on newline character
                 toList()*.              // Convert each String to a list of chars
                 collate(80)*.           // Split these into multiple lists, 80 chars long
                 collect { it.join() }.  // Join all of the chars back into strings
                 flatten().              // Flatten the multiple lists of Strings into one
                 join( '\n' )            // And join these strings back together with '\n' between them
    

    Edit

    After the edit, does this work:

    String trimOutput( String input, int width=90 ) {
      input.tokenize( '\n' )*.
            toList()*.
            collate( width )*.
            collect { it.join() }.
            flatten().
            join( '\n' )
    }
    
    xml.licInfo {
      lic.eachWithIndex { key, value, idx ->
        // Shouldn't this be 'value', not 'key'?
        if( !key.contains( 'failed with error' ) ) {
          license( name: key, assignedTo: idx, trimOutput( value ) )
        }
      }
    }
    

    I think you need to change to checking for ‘failed with error’ in the value of the lic map, not the key as you have currently, but I can’t be sure)


    Edit2

    If you are stuck with groovy 1.8.1, there is no collate method, so you’ll have to roll your own:

    List.metaClass.collate = { size ->
      def rslt = delegate.inject( [ [] ] ) { ret, elem ->
        ( ret.last() << elem ).size() >= size ? ret << [] : ret
      }
      !rslt.last() ? rslt[ 0..-2 ] : rslt
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string which contains binary digits. How to separate string after each
If I have a string which contains proper xml format and I want to
I have a string variable tmpImgURLStr which contains URL like www.abc.com/img.png . i want
So I have an object that could hold a string which contains some data,
I have a string which contains a series of bits (like 01100011) and some
I have a list which contains some items of type string. List<string> lstOriginal; I
I have a string which contains only numbers. Now I want to remove all
I have a string column which contains some numeric fields, but a lot are
I have a char array which contains some value . I want to copy
I have a string which contains text and some <a> tags in it; I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.