I would like to create a String where the values have a fixed offset where to add values to a string. Example
ID(0) Name(10) Lastname(20) City(30)
example
1 Chris Smith Paris
I have found StringBuffer.putAt(IntRange range, Object value) or similiar, but I don't want to have a range, but an index where to start.
StringBufferWriter.write(String text, int offset, int length)
I have found [StrindBufferWriter][1], but not sure if the package codehause is an offical package I can use.
Any suggestions what to use here?
CodePudding user response:
You can use String.padRight
to achieve this effect:
def users = [
[id: 1, name: 'Chris', lastname: 'Smith', city:'Paris'],
[id: 2, name: 'Tim', lastname: 'Yates', city:'Manchester'],
]
users.each { user ->
println "${user.id.toString().padRight(10)}${user.name.padRight(10)}${user.lastname.padRight(20)}$user.city"
}
Which prints:
1 Chris Smith Paris
2 Tim Yates Manchester