How to remove duplicate spaces from a string in Go

1 Answer

0 votes
package main

import (
	"fmt"
	"regexp"
)

func main() {
	s := "golang      c# rust   java c    c++   python"

	whitespaceregx := regexp.MustCompile(`\s+`)
	s = whitespaceregx.ReplaceAllString(s, " ")

	fmt.Println(s)
}



/*
run:

golang c# rust java c c++ python

*/

 



answered Oct 14, 2024 by avibootz
edited Oct 14, 2024 by avibootz

Related questions

...