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
Release: 2024-12-07 09:56:16
Original
702 people have browsed it

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)
}
Copy after login

Output:

16 "\t Hello, World\n "
12 "Hello, World"
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template