Demo entry 6714103

Programs

   

Submitted by anonymous on Feb 11, 2018 at 16:29
Language: Go. Code size: 609 Bytes.

package main

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

var matchFirstCap = regexp.MustCompile("(.)([A-Z][a-z]+)")
var matchAllCap = regexp.MustCompile("([a-z0-9])([A-Z])")

func ToSnakeCase(str string) string {
	snake := matchFirstCap.ReplaceAllString(str, "${1}_${2}")
	snake = matchAllCap.ReplaceAllString(snake, "${1}_${2}")
	return strings.ToLower(snake)
}

func main() {
	fmt.Println(ToSnakeCase("JapanCanadaAustralia"))
	fmt.Println(ToSnakeCase("JapanCanadaAUSTRALIA"))
	fmt.Println(ToSnakeCase("JAPANCanadaAUSTRALIA"))
	fmt.Println(ToSnakeCase("Japan125Canada130Australia150"))
}

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).