Forum Discussion

Mis3's avatar
Mis3
Frequent Contributor
3 years ago

Substring not working

I am trying to output the first X characters of a XML response and getting error. Line in the code below, MESSAGE2 can be very long and I like to output only the first 40 characters. Please help. ...
  • ChrisAdams's avatar
    ChrisAdams
    3 years ago

    Hi,

     

    I honestly cannot think of a way to use an 'if' against string length decision without calculating the length.

     

    Your approach looks good, but if its making it more concise that you're after, then try...

     

    if ( MESSAGE2.length() > 92 ) { MESSAGE2 = MESSAGE2.substring(0,91) }

     

    You might want to have a read about Ternary operators (https://groovy-programming.com/post/175536037619) which are very concise conditionals, but you'll still need to work out the length.