strings.chop strings.chomp

2,798 views
Skip to first unread message

Oleg Puchinin

unread,
Jan 11, 2010, 9:01:50 AM1/11/10
to golang-nuts
Probably it is necessary to add these two functions:
chop - removal of last symbol
chomp - removal of the end of a line

Devon H. O'Dell

unread,
Jan 11, 2010, 9:38:32 AM1/11/10
to Oleg Puchinin, golang-nuts
2010/1/11 Oleg Puchinin <graycard...@gmail.com>:

> Probably it is necessary to add these two functions:
> chop - removal of last symbol
> chomp - removal of the end of a line

These are most frequently used in Perl to remove trailing whitespace
from a line. Go already has functions to remove whitespace from both
the front and back of a string (this removes the necessity for chomp).
If you simply want to `strip' the last character from a string:

newString := string[:len(string)-1]

This effectively removes the necessity for chop.

--dho

Ramanan

unread,
Jan 11, 2010, 10:32:23 AM1/11/10
to golang-nuts
> newString := string[:len(string)-1]
> This effectively removes the necessity for chop.

This is true, though I think string.chop() is _far_ more readable.

Ramanan.

Devon H. O'Dell

unread,
Jan 11, 2010, 10:36:36 AM1/11/10
to Ramanan, golang-nuts
2010/1/11 Ramanan <rsiv...@gmail.com>:

>> newString := string[:len(string)-1]
>> This effectively removes the necessity for chop.
>
> This is true, though I think string.chop() is _far_ more readable.

Matter of taste.

func chop(s string) {
return s[:len(s)-1]
}

> Ramanan.
>

Devon H. O'Dell

unread,
Jan 11, 2010, 10:39:28 AM1/11/10
to Ramanan, golang-nuts
2010/1/11 Devon H. O'Dell <devon...@gmail.com>:

> 2010/1/11 Ramanan <rsiv...@gmail.com>:
>>> newString := string[:len(string)-1]
>>> This effectively removes the necessity for chop.
>>
>> This is true, though I think string.chop() is _far_ more readable.
>
> Matter of taste.

er,

func chop(s string) string {
return s[0:len(s)-1]
}

fixed.

Brian Stuart

unread,
Jan 11, 2010, 10:47:50 AM1/11/10
to Ramanan, golang-nuts
>> newString := string[:len(string)-1]
>> This effectively removes the necessity for chop.
>
> This is true, though I think string.chop() is _far_ more readable.

I guess it must be a matter of taste/preference/aesthetic/whatever.
For me the former is far more readable. Here's why. To know a
_language_ does not necessarily mean knowing every one of (in some
cases an enormous number) of libraries available for it, or even the
ones identified as standard. If I truly know the language and just
happen to have memorized the particulars of chop(), then both are
equally understandable. If I know the language, but haven't memorized
that part of the string library, I can understand the first, but the
second requires me to head to the man page. If I don't know the
language, then I shouldn't be using the library until I do. In other
words, for me readable implies that I can understand exactly what it's
doing without running off to some other documentation, and for me
that's much more likely to be true of the first form than the second.

BLS

Devon H. O'Dell

unread,
Jan 11, 2010, 11:11:48 AM1/11/10
to Brian Stuart, Ramanan, golang-nuts
2010/1/11 Brian Stuart <blst...@gmail.com>:

I quite like (and agree with) your explanation here. Thanks for
putting it so eloquently.

It's perhaps worth qualifying this, though: it only really holds valid
for short snippets such as this, though. Get much longer and you do
want a function: this is why they exist, and why we have packages in
the first place (this is obvious). At some level of complexity, you
have to refer to man pages or whatever the relevant documentation is.

I think there are two other things standing in the way of Chomp/Chop:

1) Nomenclature: both of these functions have very terse names and are
among the most frequently misused and misunderstood functions among
beginners in Perl. People chop when they mean chomp and vice versa. If
they were to make it into Go, I would suggest different names, and

2) Non-standard: they only really exist in Perl (and PHP, sort of, for
weak Perl-like ease-of-transition). In Python or Ruby, you would
likely be told to use right-hand trimming in place of chomp or a
similar slice-type syntax in place of chop.

--dho

> BLS
>

Brian Stuart

unread,
Jan 11, 2010, 11:40:16 AM1/11/10
to Devon H. O'Dell, golang-nuts
> It's perhaps worth qualifying this, though: it only really holds valid
> for short snippets such as this, though. Get much longer and you do
> want a function: this is why they exist, and why we have packages in
> the first place (this is obvious). At some level of complexity, you
> have to refer to man pages or whatever the relevant documentation is.

That's a good point. It would certainly be counterproductive to
eliminate libraries and functions altogether. And of course, once you
have libraries, man pages come along by necessity. What gets to me is
the propensity to give names to basic operations in a language under
the assumption that names are more readable than operators. It's
reminiscent of a comment like:

i++ // add 1 to i

If I'm being graded on the number of comments or I'm trying to write
example code to explain how ++ works, then maybe there's a reason for
a comment like this. Otherwise, it just clutters the code. Once
again the words of Grace Murray Hopper come to mind:

There are two kinds of people in the world. Those of us who are
basically mathematicians want to reduce all the words to symbols. The
others want to replace all the symbols with words. COBOL was created
for the second group.

BLS

Oleg Puchinin

unread,
Jan 12, 2010, 3:48:32 AM1/12/10
to golang-nuts

//
package op

func chop(s string) {
return s[:len(s)-1] // 5
}
//
./op_strings.go:5: syntax error near s

Peter Bourgon

unread,
Jan 12, 2010, 3:53:39 AM1/12/10
to golang-nuts
A function that returns a string must return a string:

func chop(s string) string {
return s[:len(s)-1]
}

And I think the new s[:n] syntax was introduced relatively recently,
make sure you're running the latest.

SnakE

unread,
Jan 12, 2010, 4:45:50 AM1/12/10
to peter....@gmail.com, golang-nuts
2010/1/12 Peter Bourgon <peterb...@gmail.com>

A function that returns a string must return a string:

func chop(s string) string {
   return s[:len(s)-1]
}

And I think the new s[:n] syntax was introduced relatively recently,
make sure you're running the latest.

The new slicing syntax allows to omit only the second index, not the first.  So the correct function would be

func chop(s string) string {
   return s[0:len(s)-1]
}

Peter Bourgon

unread,
Jan 12, 2010, 5:04:07 AM1/12/10
to golang-nuts
This is what I get for not compiling my example code :)

Oleg Puchinin

unread,
Jan 12, 2010, 5:35:54 AM1/12/10
to golang-nuts
func chop(s string) string {
return s[0:len(s)-1]
}

It's work. Thank you !

On 12 янв, 16:04, Peter Bourgon <pe...@bourgon.org> wrote:
> This is what I get for not compiling my example code :)
>

> On Tue, Jan 12, 2010 at 11:45 AM, SnakE <snake.sc...@gmail.com> wrote:
> > 2010/1/12 Peter Bourgon <peterbour...@gmail.com>

Devon H. O'Dell

unread,
Jan 12, 2010, 8:17:58 AM1/12/10
to Peter Bourgon, golang-nuts
2010/1/12 Peter Bourgon <pe...@bourgon.org>:

> This is what I get for not compiling my example code :)

I introduced (and, 3 minutes later, fixed) the broken code. This is
what we all get when people don't read email threads they're
participating in, I guess.

--dho

Michael Hoisie

unread,
Jan 12, 2010, 3:05:12 PM1/12/10
to golang-nuts
One thing that would be useful for me is a more general strings.Trim
method. It would be similar to python's Strip:
http://docs.python.org/library/stdtypes.html#str.strip

it would take a set of characters and remove them from both ends of
the string.

For instance, strings.Trim ( `" hello" `, `" `) -> "hello" ( removes
the quotes and the space )


On Jan 12, 5:17 am, "Devon H. O'Dell" <devon.od...@gmail.com> wrote:
> 2010/1/12 Peter Bourgon <pe...@bourgon.org>:
>
> > This is what I get for not compiling my example code :)
>
> I introduced (and, 3 minutes later, fixed) the broken code. This is
> what we all get when people don't read email threads they're
> participating in, I guess.
>
> --dho
>
>
>

> > On Tue, Jan 12, 2010 at 11:45 AM, SnakE <snake.sc...@gmail.com> wrote:
> >> 2010/1/12 Peter Bourgon <peterbour...@gmail.com>
>

Russ Cox

unread,
Jan 12, 2010, 4:55:09 PM1/12/10
to Michael Hoisie, golang-nuts
> For instance, strings.Trim ( `" hello" `, `" `) -> "hello" ( removes
> the quotes and the space )

In this specific instance you'd be better off
with a real unquoter like strconv.Unquote.

Russ

Reply all
Reply to author
Forward
0 new messages