Home > Backend Development > Golang > How to Remove Duplicated Spaces and Whitespace from Strings in Golang?

How to Remove Duplicated Spaces and Whitespace from Strings in Golang?

Barbara Streisand
Release: 2024-11-15 06:01:02
Original
733 people have browsed it

How to Remove Duplicated Spaces and Whitespace from Strings in Golang?

Removing Duplicated Spaces and Whitespace from Strings in Golang

To remove both leading/trailing whitespace and redundant spaces from a string in Golang, you can utilize the strings package.

  1. Removing Leading/Trailing Whitespace:

The strings.TrimSpace() function removes leading and trailing whitespace, including new-line characters and null characters.

trimmedString := strings.TrimSpace(originalString)
Copy after login
  1. Removing Redundant Spaces:

To remove redundant spaces, you can use strings.Fields(). This function splits a string on whitespace characters, resulting in a slice of substrings.

formattedString := strings.Join(strings.Fields(originalString), " ")
Copy after login

Handling International Space Characters:

To handle international space characters, you can use unicode support. The following code uses the unicode.IsSpace() function to check for various whitespace characters:

func standardizeSpacesUnicode(s string) string {
    var buffer bytes.Buffer
    for _, r := range s {
        if unicode.IsSpace(r) {
            if buffer.Len() == 0 || buffer.Bytes()[buffer.Len()-1] != ' ' {
                buffer.WriteRune(r)
            }
        } else {
            buffer.WriteRune(r)
        }
    }
    return buffer.String()
}
Copy after login

Example Usage:

package main

import (
    "fmt"
    "strings"
)

func main() {
    tests := []string{
        " Hello,   World  ! ",
        " Hello,\tWorld ! ",
        " \t\n\t Hello,\tWorld\n!\n\t",
        "你好,世界!", // Unicode test
    }

    for _, test := range tests {
        trimmed := strings.TrimSpace(test)
        formatted := strings.Join(strings.Fields(test), " ")
        standardizedUnicode := standardizeSpacesUnicode(test)

        fmt.Println("Original:", test)
        fmt.Println("Trimmed:", trimmed)
        fmt.Println("Formatted:", formatted)
        fmt.Println("Standardized Unicode:", standardizedUnicode)
        fmt.Println()
    }

}
Copy after login

Output:

Original:  Hello,   World  !
Trimmed:  Hello, World !
Formatted:  Hello World !
Standardized Unicode: Hello World !

Original:  Hello,\tWorld !
Trimmed:  Hello, World !
Formatted:  Hello World !
Standardized Unicode: Hello World !

Original:   \t\n\t Hello,\tWorld\n!\n\t
Trimmed:  Hello, World!
Formatted:  Hello World!
Standardized Unicode: Hello World!

Original:  你好,世界!
Trimmed:  你好,世界!
Formatted:  你好 世界!
Standardized Unicode: 你好 世界!
Copy after login

The above is the detailed content of How to Remove Duplicated Spaces and Whitespace from Strings in Golang?. 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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template