How to remove duplicate whitespace from a string in Go

1 Answer

0 votes
package main

import (
	"fmt"
	"regexp"
	"strings"
)

func main() {
	s := " \ngo    c++\n\t     php   "

	re := regexp.MustCompile(`\s+`)
	
    s = re.ReplaceAllString(s, " ")
    s = strings.TrimSpace(s)

	fmt.Println(s)
}
 

 
 
/*
run:
 
go c++ php
 
*/

 



answered Aug 25, 2020 by avibootz
...