Go Lang Tutorial - String methods and examples

Last updated Oct 08, 2021


In thig go lang tutorials we will learn about Strings. String is a data type that can be used to store alphanumeric characters. In this example, the variable called name is using string data type.

var name string = "Nadir sha"

 

In Go, there are two main packages that can be used together with string including strconv and strings.


The strconv package

This package is used to convert a certain data type into string and vice versa. In this example, the integer is converted into string data type using Itoa() method.

package main

import (
    "fmt"
    "strconv"
)

func main() {
    var number int = 33

    // convert int into string data type
    var stringNumber string = strconv.Itoa(number)

    fmt.Println("Convert result: ", stringNumber)
}

 

Output

Convert result: 33

 

In order to convert string into another type like integer, the Atoi() method can be used.

package main

import (
    "fmt"
    "strconv"
)

func main() {
    var number string = "456"

    num, err := strconv.Atoi(number)
    // if error occurred, show the error message
    if err != nil {
        fmt.Println("Conversion failed: ", err.Error())
    }

    fmt.Println("Convert result: ", num)
}

 

Output

Convert result:  456

 

The strings package

This package is used to perform string manipulation. There are many methods that can be used in this package.


Using Split()

The Split() method is used to split a certain string based on a specified separator. The result from this method will be a list of strings or slice of strings.

package main

import (
    "fmt"
    "strings"
)

func main() {
    var name string = "Nadir sha"

    var result []string = strings.Split(name, "")

    fmt.Println("The result: ", result)
}

Output

The result:  [N a d i r   s h a]

 

Using Contains()

The Contains() method is used to check if certain substring or characters exists inside specified string. This method is returning true if certain substring exists whilst if the substring is not exists the false is returned.

package main

import (
    "fmt"
    "strings"
)

func main() {
    var name string = "john doe"

    var isExists bool = strings.Contains(name, "doe")

    fmt.Println("The result: ", isExists)
}

 

Output

The result: true

 

Using Compare()

The Compare() method is used to compare two strings. If the two strings are equal, the 0 is returned.

package main

import (
    "fmt"
    "strings"
)

func main() {
    var password string = "secret"

    var confirmationPass string = "secret"

    var result int = strings.Compare(password, confirmationPass)

    if result == 0 {
        fmt.Println("The two strings are equal")
    }
}

Output

The two strings are equal

 

Using ToUpper()

The toUpper() method can be used to convert all characters into uppercase characters.

package main


import (
    "fmt"
    "strings"
)

func main() {
    var name string = "nathan mckane"

    var result string = strings.ToUpper(name)

    fmt.Println("result:", result)
}

 

Output

result: NATHAN MCKANE

 

Using ToLower()

The toLower() method can be used to convert all characters into lowercase characters

package main


import (
    "fmt"
    "strings"
)

func main() {
    var name string = "fIrStNaMe lasTnAmE"

    var result string = strings.ToLower(name)

    fmt.Println("result:", result)
}

Output

result: firstname lastname

 

Using Replace()

The Replace() method can be used to replace certain string with another string. In this method, the number of string that will be replaced can be defined.

package main

import (
    "fmt"
    "strings"
)

func main() {
    // in this string, there are two words called "world"
    var text string = "hello world, what a beautiful world"

    // replace word "world" with "guys!"
    // replace 1 word that equals "world"
    var result string = strings.Replace(text, "world", "guys!", 1)

    fmt.Println("The result: ", result)
}

Output

The result:  hello guys!, what a beautiful world

 

Using ReplaceAll()

The ReplaceAll() method is used to replace all the certain string with the new string.

package main

import (
    "fmt"
    "strings"
)

func main() {
    var text string = "hello guest, have a nice day guest!"

    // replace the "guest" words with "joe"
    var result string = strings.ReplaceAll(text, "guest", "joe")

    fmt.Println("Result: ", result)
}

Output

Result:  hello joe, have a nice day joe!

 

I hope this article is helpful to learn String in Go Programming Language.

 

 


Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

16 Views

Subscribe For Daily Updates

Flutter Questions
Android Questions