Reputation: 139
Using Scala, I am grabbing a json response object from a web API and storing the response as a string s
. This string is at least several kilobytes. Because sometimes this response can provide some funky stuff hinting at errors or issues with the API I want to print out a preview of the response to our logs. That way I can see the log and tell that the job is either running successfully or has failed. Is there an efficient and safe way to grab the first 100 or so characters from a string? The string may occasionally be very small so grabbing via a slice I think will cause an index out of range issue.
val n = 100
val myString: String = getResponseAsString()//returns small or very large string
logger.warn(s"Data: $myString") //how to print only first 'n' chars?
Upvotes: 1
Views: 2092
Reputation: 51271
val n :Int = ...
val myString :String = ...
logger.warn(s"Data: %.${n}s" format myString)
Upvotes: 1