3

I used strings.Trim() in Golang to trim the first five characters.
However, the last "a" is always gone.
Why is that so?

Example:

sentence := "Kab. Kolaka Utara"
result := strings.Trim(sentence,sentence[:4])
fmt.Println(result)

Result: Kolaka Utar

I expected: Kolaka Utara

https://play.golang.org/p/R6UoKWNaVv

3
  • 1
    What is your expected output, for example: "Kolaka Utara" ? Commented Aug 5, 2016 at 3:10
  • @Amd Yes, I expected Kolaka Utara Commented Aug 5, 2016 at 3:32
  • For googlers who just want to trim spaces from a string (the standard definition of "trim"), strings.Trim(s, " ") for spaces only or strings.TrimSpace(s) for all whitespace. Commented Jan 24, 2023 at 17:50

5 Answers 5

6

Trim returns a slice of the string s with all leading and trailing Unicode code points contained in cutset removed.

sentence[:4] is "Kab." Trim will remove all leading and trailing "k", "a", "b", ".".

https://golang.org/pkg/strings/#Trim

Sign up to request clarification or add additional context in comments.

Comments

4

If you want to trim the first 5 bytes, then use:

result := sentence[5:]

playground example

Comments

2

Since strings are UTF-8 encoded in Golang for single byte Unicode code points you may use result := sentence[5:]
like this working sample code:

package main

import "fmt"

func main() {
    sentence := "Kab. Kolaka Utara"
    result := sentence[5:]
    fmt.Println(result)
}

output:

Kolaka Utara

and for multibyte Unicode code points like "µµµµ Kolaka Utara" you may use string([]rune(sentence)[5:]), like this working sample code:

package main

import "fmt"

func main() {
    sentence := "µµµµ Kolaka Utara"
    result := string([]rune(sentence)[5:])
    fmt.Println(result)
}

output:

Kolaka Utara

and see Docs:

func Trim(s string, cutset string) string:

Trim returns a slice of the string s with all leading and trailing Unicode code points contained in cutset removed.

and see: Extracting substrings in Go

Comments

1

You should know the precise meaning of "trim" in programming: removing some characters both from the beginning and the end.

And in Golang's strings.Trim, The second argument is a set of characters.

Trim will remove all leading and trailing characters contained in the set.

In your example:

The set is

{"K", "a", ".", "b"};

For "Kab. Kolaka Utara", Trim will remove "Kab." from the beginning and "a" from the end.

So, the actual string you get is " Kolaka Utar" instead of "Kolaka Utar" which has no leading space.

If you just want to "trim" the first five characters, you should use this statement:

sentence = sentence[5:].

Comments

0

If you are sure about how many characters to be trimmed. Use this method.

func main() {
    sentence := "Kab. Kolaka Utara"
    fmt.Println(sentence)
    fmt.Println(sentence[5:len(sentence)])

}

Output :

 Value :  Kab. Kolaka Utara
 Trimed : Kolaka Utara

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.