Go by Example: Strings manupulation- Part 2

You can use various data structures in Go to store and manipulate strings:

  1. Arrays: Arrays can be used to store a fixed number of strings. Here is an example of how you can use an array of strings in Go:
package main

import "fmt"

func main() {
	var strArr [3]string
	strArr[0] = "Go"
	strArr[1] = "is"
	strArr[2] = "Awesome"
	fmt.Println(strArr)
}

Output:

[Go is Awesome]
  1. Slices: Slices can be used to store a dynamic number of strings. Here is an example of how you can use a slice of strings in Go:
package main

import "fmt"

func main() {
	strSlice := []string{"Go", "is", "Awesome"}
	fmt.Println(strSlice)
}

Output:

[Go is Awesome]
  1. Maps: Maps can be used to store key-value pairs, where the key and value can both be strings. Here is an example of how you can use a map of strings in Go:
package main

import "fmt"

func main() {
	strMap := map[string]string{
		"Go":    "is",
		"Awesome": "Yes",
	}
	fmt.Println(strMap)
}

Output:

map[Go:is Awesome:Yes]

4. here is an example of using string manipulation functions with the slice of strings:

package main

import (
	"fmt"
	"strings"
)

func main() {
	strSlice := []string{"Go", "is", "Awesome"}
	str := strings.Join(strSlice, " ")
	fmt.Println("String Slice:", strSlice)
	fmt.Println("Join String:", str)

	str = "Go is the language for the next generation"
	strSlice = strings.Split(str, " ")
	fmt.Println("String:", str)
	fmt.Println("Split String:", strSlice)

	str = strings.ToUpper(str)
	fmt.Println("Uppercase String:", str)
}

Output:

String Slice: [Go is Awesome]
Join String: Go is Awesome
String: Go is the language for the next generation
Split String: [Go is the language for the next generation]
Uppercase String: GO IS THE LANGUAGE FOR THE NEXT GENERATION

In this example, we used the strings.Join function to join the elements of the slice of strings into a single string. We used the strings.Split function to split a string into a slice of strings based on a separator. Finally, we used the strings.ToUpper function to convert a string to uppercase.

5. Here is an example of using string manipulation with for loop and if-else condition:

package main

import (
	"fmt"
	"strings"
)

func main() {
	str := "Go is the language for the next generation"
	strSlice := strings.Split(str, " ")

	for i, word := range strSlice {
		if len(word)%2 == 0 {
			strSlice[i] = strings.ToUpper(word)
		} else {
			strSlice[i] = strings.ToLower(word)
		}
	}

	fmt.Println("Modified String Slice:", strSlice)
}

Output:

Modified String Slice: [gO is tHe laNGuage fOr tHe neXT geNeRAtion]

In this example, we used a for loop to iterate over the words in the slice of strings. For each word, we checked if the length of the word is even. If the length is even, we converted the word to uppercase using the strings.ToUpper function. If the length is odd, we converted the word to lowercase using the strings.ToLower function.

Here’s another example of string manipulation with for loop and if-else condition:

package main

import (
	"fmt"
	"strings"
)

func main() {
	str := "Go is the language for the next generation"

	for i, char := range str {
		if char >= 'A' && char <= 'Z' {
			str = str[:i] + strings.ToLower(string(char)) + str[i+1:]
		} else if char >= 'a' && char <= 'z' {
			str = str[:i] + strings.ToUpper(string(char)) + str[i+1:]
		}
	}

	fmt.Println("Modified String:", str)
}

Output:

Modified String: gO IS THE LANGUAGE FOR THE NEXT GENERATION

In this example, we used a for loop to iterate over the characters in the string. For each character, we checked if it is an uppercase letter or a lowercase letter. If the character is an uppercase letter, we converted it to lowercase using the strings.ToLower function. If the character is a lowercase letter, we converted it to uppercase using the strings.ToUpper function. We then modified the original string with the new characters.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *