Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I format a string with string interpolation in Scala as a fixed width string?

I'm interfacing with a really old system and the file I need to generate needs a field that is a formed from a string but needs to be exactly 15 in width.

I want something like this:

val companyName = "FooBar, Inc" // 11 chars
f"$companyName%s"

To return:

"    FooBar, Inc"

Is there a slick way to do what I'm trying to do with the String interpolation?

like image 940
myyk Avatar asked Mar 13 '13 01:03

myyk


1 Answers

Use String.format with a format string. Surely something there will do what you want :-)

This code would do what you want:

scala> val companyName = "FooBar, Inc"
companyName: String = FooBar, Inc

scala> f"$companyName%15s"
res0: String = "    FooBar, Inc"
like image 140
Ron Dahlgren Avatar answered Oct 26 '22 18:10

Ron Dahlgren