Created
February 22, 2022 05:50
-
-
Save nidhi-canopas/17a68b3c36cc2f4c970f887773265d29 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import ( | |
| "fmt" | |
| "strings" | |
| "regexp" | |
| ) | |
| func main() { | |
| snakeCase := ConvertToSnakeCase("ILikeProgrammingINGo123") | |
| fmt.Println("String in snake case : ", snakeCase) | |
| } | |
| func ConvertToSnakeCase(input string) string { | |
| var matchChars = regexp.MustCompile("(.)([A-Z][a-z]+)") | |
| var matchAlpha = regexp.MustCompile("([a-z0-9])([A-Z])") | |
| snake := matchChars.ReplaceAllString(input, "${1}_${2}") | |
| snake = matchAlpha.ReplaceAllString(snake, "${1}_${2}") | |
| return strings.ToLower(snake) | |
| } | |
| output: | |
| String in snake case : i_like_programming_in_go123 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment