Is There Any Special Character For String Termination

Hello Reader,
I am working on function to remove the whitespace from begginning and ending of string

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

package Madhur

// This package will alter the user name if there are white spaces in name of string

// this will remove the white spaces (if any) from string

func Remove_Whitespc(word string) string {

// str is converted to byte a string are immutable
str := []byte(word)
if str[0] == ' '{
	for i := 0; i < len(str) - 1; i++ {
		str[i] = str[i+1]
	}
}

return string(str)

}

1 Like

Why not use the Trim function from the ā€œstringsā€ package in the standard library?

In your case you could do:

`strings.Trim(word, " ")`

Example from the documentation:

package main

import (
	"fmt"
	"strings"
)

func main() {
	fmt.Print(strings.Trim("Ā”Ā”Ā”Hello, Gophers!!!", "!Ā”"))
}
4 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.