Home >Backend Development >Golang >How to Remove Leading and Trailing Whitespace from Strings in Go?

How to Remove Leading and Trailing Whitespace from Strings in Go?

DDD
DDDOriginal
2024-12-07 09:56:16750browse

How to Remove Leading and Trailing Whitespace from Strings in Go?

Removing Leading and Trailing White Spaces from Strings in Go:

To effectively trim the leading and trailing white spaces of a string variable in Go, there is a built-in function called strings.TrimSpace(s). This function removes all leading and trailing whitespace from the input string s.

Example:

package main

import (
    "fmt"
    "strings"
)

func main() {
    s := "\t Hello, World\n "
    fmt.Printf("%d %q\n", len(s), s)
    t := strings.TrimSpace(s)
    fmt.Printf("%d %q\n", len(t), t)
}

Output:

16 "\t Hello, World\n "
12 "Hello, World"

The above is the detailed content of How to Remove Leading and Trailing Whitespace from Strings in Go?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn