Padding a string left and right in Scala

10,176 views
Skip to first unread message

Edmondo Porcu

unread,
Jul 18, 2012, 11:24:12 AM7/18/12
to scala...@googlegroups.com

Dear all,

I would need for making my logs more readable to pad a string left and right symmetrically, is there anything like that pre-built in the Standard library?

 

Best regards

 

 

Edmondo Porcu

 

 

√iktor Ҡlang

unread,
Jul 18, 2012, 11:28:38 AM7/18/12
to Edmondo Porcu, scala...@googlegroups.com
Any decent logging lib will support that in the log pattern definition.
--
Viktor Klang

Akka Tech Lead
Typesafe - The software stack for applications that scale

Twitter: @viktorklang

Dennis Haupt

unread,
Jul 18, 2012, 11:30:55 AM7/18/12
to "√iktor Ҡlang", Edmond...@gottexbrokers.com, scala...@googlegroups.com
i suspect logging = println(...) ;)

-------- Original-Nachricht --------
> Datum: Wed, 18 Jul 2012 17:28:38 +0200
> Von: "√iktor Ҡlang" <viktor...@gmail.com>
> An: Edmondo Porcu <Edmond...@gottexbrokers.com>
> CC: "scala...@googlegroups.com" <scala...@googlegroups.com>
> Betreff: Re: [scala-user] Padding a string left and right in Scala

> Any decent logging lib will support that in the log pattern definition.
>
> On Wed, Jul 18, 2012 at 5:24 PM, Edmondo Porcu <
> Edmond...@gottexbrokers.com> wrote:
>
> > Dear all,****
> >
> > I would need for making my logs more readable to pad a string left and
> > right symmetrically, is there anything like that pre-built in the
> Standard
> > library?****
> >
> > ** **
> >
> > Best regards****
> >
> > ** **
> >
> > ** **
> >
> > *Edmondo Porcu*
> >
> > ** **
> >
> > ** **
> >
>
>
>
> --
> Viktor Klang
>
> Akka Tech Lead
> Typesafe <http://www.typesafe.com/> - The software stack for applications
> that scale
>
> Twitter: @viktorklang

√iktor Ҡlang

unread,
Jul 18, 2012, 11:34:42 AM7/18/12
to Dennis Haupt, Edmond...@gottexbrokers.com, scala...@googlegroups.com
On Wed, Jul 18, 2012 at 5:30 PM, Dennis Haupt <h-s...@gmx.de> wrote:
i suspect logging = println(...) ;)


synchronous blocking IO for logging :(



--
Typesafe - The software stack for applications that scale

Twitter: @viktorklang

Edmondo Porcu

unread,
Jul 19, 2012, 2:50:12 AM7/19/12
to scala...@googlegroups.com

This pads only to the right, how can one also make it to left symmetrically?

 

PS: viktor, it is only for few messages so I am not sure I would like to change the layout for log4j for only a few messages at startup of my app

 

From: Naftoli Gugenheim [mailto:nafto...@gmail.com]
Sent: jeudi 19 juillet 2012 06:06
To: Edmondo Porcu
Subject: Re: [scala-user] Padding a string left and right in Scala

 

scala> "hello".padTo(25,"-").mkString

res5: String = hello--------------------


______________________________________________________________________
This email has been scanned by the Symantec Email Security.cloud service.
For more information please visit http://www.symanteccloud.com
______________________________________________________________________

Dennis Haupt

unread,
Jul 19, 2012, 3:36:38 AM7/19/12
to Edmondo Porcu, scala...@googlegroups.com
if it is only a special case, go all dirty:
str.reverse.pad.reverse

-------- Original-Nachricht --------
> Datum: Thu, 19 Jul 2012 06:50:12 +0000
> Von: Edmondo Porcu <Edmond...@gottexbrokers.com>
> An: "scala...@googlegroups.com" <scala...@googlegroups.com>
> Betreff: RE: [scala-user] Padding a string left and right in Scala

pagoda_5b

unread,
Jul 19, 2012, 9:32:30 AM7/19/12
to scala...@googlegroups.com
"-------%s -------".format("My log message")

Ivano

Sylvain HENRY

unread,
Jul 19, 2012, 3:39:42 PM7/19/12
to scala...@googlegroups.com
Hi,

I don't think there is a dedicated function. Try something like this:

def pad(s:String, width:Int, ch:String) = {
    val l = s.length
    val left = (width - l) / 2
    val right = width - left - l
    ch*left + s + ch*right
}

scala> pad("hello you", 50, "-")
res4: java.lang.String = --------------------hello you---------------------

Cheers
Sylvain
Reply all
Reply to author
Forward
0 new messages