Created
January 17, 2024 08:58
-
-
Save prashant-shahi/219ae1a8ae67ae5d8b9417de6cb0a147 to your computer and use it in GitHub Desktop.
kebab casing string for valid URL paths in Golang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"regexp" | |
"strings" | |
) | |
func toKebabCase(s string) string { | |
// Regular expression to match non-alphanumeric characters | |
reg := regexp.MustCompile("[^a-zA-Z0-9]+") | |
// Replace non-alphanumeric characters with hyphen | |
kebab := reg.ReplaceAllString(s, "-") | |
// Convert to lowercase | |
return strings.ToLower(kebab) | |
} | |
func main() { | |
testStrings := []string{"Hello/World", "hello world", "HeLLo%$./@@!^&*()WoRlD", "h3LL0^w0rLd"} | |
for _, s := range testStrings { | |
fmt.Println(toKebabCase(s)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment