String padding functions

3,967 views
Skip to first unread message

Piotr Zurek

unread,
Dec 22, 2013, 2:34:22 AM12/22/13
to golan...@googlegroups.com
Hello,

I think I may have found something that should make a nice simple way to contribute something small but possibly to this brilliant language.
Go's standard library seems to be missing functions allowing to pad a string with a another string up to a certain number of characters.

I have implemented those for my own purposes but maybe the general public will find those useful too?
I have found those especially useful when implementing sequential numbering that needs to return a certain number of characters, for file names like file_001.txt or dates 2013-02-08.

My implementation looks like that:
func PadLeft(s, p string, l int) string
func PadRight(s, p string, l int) string
and I was also thinking about writing :
func Pad(s, p string, l int) string
The way they work, they take a string 's' and pad it with 'p' up to the total number of 'l'. PadLeft adds characters on the left, PadRight obviously on the right and Pad could put 's' in the middle and pad it on both sides.

Do you guys think it would be valuable additions to the 'strings' package?

Cheers,
Piotr

Egon Elbre

unread,
Dec 22, 2013, 3:54:23 AM12/22/13
to golan...@googlegroups.com
fmt already can do most of that... http://play.golang.org/p/enMD71zFg4

+ egon

Oleku Konko

unread,
Dec 22, 2013, 9:47:29 AM12/22/13
to golan...@googlegroups.com
I might be wrong but i don't think `fmt` has proper support or simplicity when it comes to string padding  because its only easy padding with `space` and `0` anything else introduces complexity 


If i understand Zurek he is trying to implement something like :

package main
import "fmt"
func PadRight(str, pad string, lenght int) string {
    for {
        str += pad
        if len(str) > lenght {
            return str[0:lenght]
        }
    }
}
func PadLeft(str, pad string, lenght int) string {
    for {
        str = pad + str
        if len(str) > lenght {
            return str[0:lenght]
        }
    }
}
func main() {
    str := "abc"
    fmt.Println(PadRight(str, "x", 5))   // expects abcxx
    fmt.Println(PadLeft(str, "x", 5))    // expects xxabc
    fmt.Println(PadRight(str, "xyz", 5)) // expects abcxy
    fmt.Println(PadLeft(str, "xyz", 5))  // expects xyzab
}

Egon Elbre

unread,
Dec 22, 2013, 10:29:37 AM12/22/13
to golan...@googlegroups.com
Yes I'm aware of that... (hence the word "most of that")... But, I rarely have seen something that requires padding numbers with anything else and similarly for strings. If I have seen something like that, then it's more complex... e.g. "(000)_000-0000;0;*". Those PadRight/PadLeft are for special uses and not generally useful.

+ egon

Lucy hi

unread,
Dec 22, 2013, 11:32:22 AM12/22/13
to golan...@googlegroups.com
On Sunday, 22 December 2013 09:54:23 UTC+1, Egon Elbre wrote:
fmt already can do most of that... http://play.golang.org/p/enMD71zFg4

+ egon
 
Dynamic padding is rather painful if you try using fmt for it as well.

Brad Fitzpatrick

unread,
Dec 22, 2013, 11:49:09 AM12/22/13
to Piotr Zurek, golang-dev
I don't think these come up often enough to warrant inclusion in the standard library.

For example. I can't think of anything in the standard library that would use these.




--
 
---
You received this message because you are subscribed to the Google Groups "golang-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-dev+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Piotr Zurek

unread,
Dec 22, 2013, 11:52:44 AM12/22/13
to golan...@googlegroups.com, Piotr Zurek
On Monday, December 23, 2013 5:49:09 AM UTC+13, Brad Fitzpatrick wrote:
I don't think these come up often enough to warrant inclusion in the standard library.

For example. I can't think of anything in the standard library that would use these.

Fair enough. I'll have to come up with a better idea for my first contribution.

Robert Griesemer

unread,
Dec 22, 2013, 3:39:14 PM12/22/13
to Piotr Zurek, golang-dev
You may also want to look at text/tabwriter which provides some mechanism to write columns in tabular form, with left or right alignment, and with different filler characters.
- gri


--
Reply all
Reply to author
Forward
0 new messages