How to delete words from string in Golang 1.20
In this video we are going to generate source code for the GoLang Program, How to delete words from string using chatGPT.
After generating we will verify and run the generated code.
IMPORTANT NOTE on SOURCE CODE: only source code that are acceptable by the youtube description box will be displayed.
##############SOURCE CODE#########################
package main
import (
"fmt"
"strings"
)
func main() {
// Define the input string
str := "The quick brown fox jumps over the lazy dog"
// Define the words to delete
wordsToDelete := []string{"quick", "lazy"}
// Loop through the words to delete
for _, word := range wordsToDelete {
// Replace the word with an empty string
str = strings.ReplaceAll(str, word, "")
}
// Print the modified string to the console
fmt.Println("String without specified words:", str)
}
Below is the explanation for the program:
In this program, we define the input string str as "The quick brown fox jumps over the lazy dog" and the words to delete as []string{"quick", "lazy"}.
We then use a for loop to loop through the words to delete, and for each word we use the strings.ReplaceAll function to replace all occurrences of the word with an empty string.
Finally, we use the fmt.Println function to print the modified string to the console.
#go #goprogramming #golang #golangtutorial #golanguage