DEV Community

iyiola-dev
iyiola-dev

Posted on

Strings in GO

#go

The Strings and Strconv library

Unlike other programming languages like Dart, Java, or Javascript, you can easily add a method to a String or convert a string to int without a package/library. In golang that is not very easy without a library.

THE STRCONV LIBRARY

The first one we will be talking about will be the strconv library.

link to the docs

The most common use of the strconv library/package is converting string to int or int to String and few other things will start by importing the package like this:

import (
    "strconv"
)

Enter fullscreen mode Exit fullscreen mode

So for converting int to string or vice versa there are 2 commands :

  • Itoa: used to convert int to string.
  • Atoi: used to convert string to int.

The first one we will use is Itoa

    num := 64
    letter := strconv.Itoa(num)
    fmt.Printf("letter is a %T", letter)
Enter fullscreen mode Exit fullscreen mode

When you run this you should see something like

letter is a string

The next thing is Atoi

    letter := "4"
    num, err := strconv.Atoi(letter)
    if err != nil {
        fmt.Println(err.Error())
    }
    fmt.Printf("%T is a number", num)
Enter fullscreen mode Exit fullscreen mode

When you run this you should see something like

int is a number

Of course, there are a lot more other things you can do with this package. you can check the docs for more functions.

### THE STRINGS PACKAGE

Link to the official documentation

You can do many things with this package, but we will only be treating a few for this article.

  • The Replace function

  • The Split function

  • The ToLower function

  • The ToUpper function

  • The Contains function

The Replace function

The Replace function accepts 4 arguments:

  1. The string
  2. The string to be replaced
  3. What should the string it should be replaced with
  4. The number of replacement occurrences

NB: for the number of occurrences, the string should be replaced if the number is a negative integer. It will replace all occurrences, but then instead of passing a negative integer, one can use the ReplaceAll function.

So I am going to show 2 examples with different cases of how this is used below.

1.

word:= "The boy is stupid stupid"
    fmt.Println(strings.Replace(word,"stupid","wise", 1))
Enter fullscreen mode Exit fullscreen mode

This replaces the first occurrence of stupid with wise because we specified that it should be done only once with the integer 1. If we replace it with 2 it will change both stupid to wise.

2.

    word:= "The boy is stupid stupid"
    fmt.Println(strings.Replace(word,"stupid","wise", 2))
Enter fullscreen mode Exit fullscreen mode

This should show The boy is wise wise as a result when you run it.

The Split Function

The split function takes in 2 arguments the strings to be separated and what should be used as the string to separate them then it returns a slice containing the strings separated. If there is no result it will return the slice with only one element which is the string. An example will be shown below:

word:= "season 03,season02,season01"
    fmt.Printf("%q\n",strings.Split(word,","))
Enter fullscreen mode Exit fullscreen mode

The result when this runs will be ["season 03" "season02" "season01"]

The ToLower Function

The ToLower function as the name suggests turns a string to lower case. It accepts only one argument, the string to be turned. An example will be shown below

    word:= "The Boy is Foolish"
    fmt.Println(strings.ToLower(word))
Enter fullscreen mode Exit fullscreen mode

when this runs the result will be the boy is foolish which means the case has been changed to lowercase.

The ToUpper Function

The ToUpper function as the name suggests turns a string to uppercase. It accepts only one argument, the string to be turned. An example will be shown below

word:= "the boy is foolish"
    fmt.Println(strings.ToUpper(word))
Enter fullscreen mode Exit fullscreen mode

when this runs the result will be THE BOY IS FOOLISH which means the case has been changed to uppercase.

The Contains Function

The contains functions check if a substring is part of a string provided, so it takes 2 arguments, the string to be checked and the substring, then it returns a bool. An example is provided below

word:= "trinkets"
fmt.println(string.Contains(word, "rin"))
Enter fullscreen mode Exit fullscreen mode

This returns a boolean of true because the word rin can be found in trinkets otherwise it would have returned false.

A whole lot more can be done with the Strconv and the Strings package check out the docs for more

Top comments (0)