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

前端 未结 1 1054
萌比男神i
萌比男神i 2021-01-04 11:24

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 so

相关标签:
1条回答
  • 2021-01-04 12:05

    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"
    
    0 讨论(0)
提交回复
热议问题