Go by Example: Strings manupulation

In Go, you can manipulate strings using a variety of built-in functions and methods. Here are some common string manipulation techniques in Go:

  1. Concatenating strings: You can concatenate two strings using the + operator, like this:
package main

import "fmt"

func main() {
	str1 := "Hello"
	str2 := " world"
	str3 := str1 + str2
	fmt.Println(str3)
}

Output:

Hello world

2. Converting strings to integers: You can convert a string to an integer using the strconv package, like this:

package main

import (
	"fmt"
	"strconv"
)

func main() {
	str := "123"
	num, _ := strconv.Atoi(str)
	fmt.Println(num)
}

Output:

123

3. Converting integers to strings: You can convert an integer to a string using the strconv package, like this:

package main

import (
	"fmt"
	"strconv"
)

func main() {
	num := 123
	str := strconv.Itoa(num)
	fmt.Println(str)
}

Output:

123

4. Splitting strings: You can split a string into substrings using the strings package, like this:

package main

import (
	"fmt"
	"strings"
)

func main() {
	str := "apple,banana,cherry"
	fruits := strings.Split(str, ",")
	fmt.Println(fruits)
}

Output:

[apple banana cherry]

5. Replacing strings: You can replace a substring in a string using the strings package, like this:

package main

import (
	"fmt"
	"strings"
)

func main() {
	str := "Hello, world!"
	newStr := strings.Replace(str, "world", "Go", 1)
	fmt.Println(newStr)
}

Output:

Hello, Go!

6. Checking string equality: You can compare two strings to see if they are equal using the == operator, like this:

package main

import "fmt"

func main() {
	str1 := "Go"
	str2 := "Go"
	if str1 == str2 {
		fmt.Println("Equal")
	} else {
		fmt.Println("Not equal")
	}
}

Output:

Equal

7. Checking if a string contains another string: You can check if a string contains another string using the strings package, like this:

package main

import (
	"fmt"
	"strings"
)

func main() {
	str := "Hello, world!"
	if strings.Contains(str, "world") {
		fmt.Println("Contains")
	} else {
		fmt.Println("Does not contain")
	}
}

Output:

Contains

8. Trimming whitespace from a string: You can trim whitespace from a string using the strings package, like this:

package main

import (
	"fmt"
	"strings"
)

func main() {
	str := "   Hello, world!   "
	newStr := strings.TrimSpace(str)
	fmt.Println(newStr)
}

Output:

Hello, world!

9. Converting strings to lowercase: You can convert a string to lowercase using the strings package, like this:

package main

import (
	"fmt"
	"strings"
)

func main() {
	str := "HELLO, WORLD!"
	newStr := strings.ToLower(str)
	fmt.Println(newStr)
}

Output:

hello, world!

10. Converting strings to uppercase: You can convert a string to uppercase using the strings package, like this:

package main

import (
	"fmt"
	"strings"
)

func main() {
	str := "hello, world!"
	newStr := strings.ToUpper(str)
	fmt.Println(newStr)
}

Output:

HELLO, WORLD!

These are just a few more examples of how you can manipulate strings in Go. Whether you are checking if two strings are equal, checking if a string contains another string, trimming whitespace, or converting strings to lowercase or uppercase, the Go standard library provides a variety of functions and methods to help you work with strings efficiently.


Comments

Leave a Reply

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