String Manipulation With Golang
Tags: go, string, programming, api
Published: 2021-09-26 06:38:53


When creating a program or an API, we will not be separated from data type manipulation. one of the most frequently used data types is strings.String data type is a data type that contains text or characters.

In the go programming language, a package has been provided to make it easier for us to manipulate or process data with string types, namely ***strings***.

How to use it is very easy. we just need to import the package. then we can use it.

 1. **Change text from lowercase to uppercase or uppercase to lowercase**

We can change the text from lowercase to uppercase or vice versa. if we want to change the text from lowercase to uppercase we can use ***ToUpper***

    strings.ToUpper(text)

![ToUpper Example](https://cdn-images-1.medium.com/max/5760/1*SzYnFUlcUMQ48VRG6KC55g.png)

If we want to change from uppercase to lowercase we can use ***ToLower***

    strings.ToLower(text)

![ToLower Example](https://cdn-images-1.medium.com/max/5760/1*9pcMjg2kPpXkQeu-s4jsGw.png)

**2**. **Checks whether a word or sentence contains certain letters or characters**

We can also check whether a word or sentence contains certain letters or characters. This condition is usually needed to check whether a parameter contains characters or symbols that are not as expected using ***Contains***.

    strings.Contains(text, specific_char)

This statement will produce a boolean output (true or false). if the output is true, then the sentence contains the specific characters to check.

![Contains Example](https://cdn-images-1.medium.com/max/5760/1*6obdndrlrRpINAPd7qsdRA.png)

**3**. **Change the first character of each word to uppercase**

Besides changing a word to uppercase or lowercase, we can also change the first character of each word to uppercase using ***Title.***

    strings.Title(text)

![Title Example](https://cdn-images-1.medium.com/max/5760/1*fiuZR4fyfFE8LefX7U2faQ.png)

**4**. **Comparing a word with other words is non-case sensitive**

Sometimes we need to compare the similarities between one word and another. and we can do that by using ***EqualFold***.

    strings.EqualFold(text1, text2)

![Compare Non-Case Sensitive word Example](https://cdn-images-1.medium.com/max/5760/1*anOKZFEZhfKviMtCNUqvTg.png)

**5**. **Replace characters with other characters**

The code below will be used to replace all the words that will be replaced with the words that will replace using ***Replace***

    strings.Replace(text,text_to_replace,text_that_will_replace,-1)

![Replace Example](https://cdn-images-1.medium.com/max/5760/1*1XF27cvvsAGrGF_c4uHtGw.png)

**6**. **Combine one word with another**

To be able to combine words that come from the slice data type, we can use ***Join***.

    var kata3 = []string{"asdam","khan","student"}

    strings.Join(slice_text,connector)

![Join String Example](https://cdn-images-1.medium.com/max/5760/1*AAHMWSFKe-guwLwcocY9zw.png)

**7**. **Break the sentence into words**

As opposed to joins, we can also split strings into slices using ***Split.***

    strings.Split(text,separator_between_word))

![Split String Example](https://cdn-images-1.medium.com/max/5760/1*egmRT5FqaVU1SAsWP5Jh-w.png)

**8**. **Count the specific number of characters**

We can counting specific character using ***Count.***

    strings.Count(text,character_will_counted)

![Count String Example](https://cdn-images-1.medium.com/max/5760/1*ELwsIyLn-zHJRtja7tisKw.png)

**9**. **Converts string to integer / int32 / int64 data type**

To convert string data types to other data types, we can use the strconv package. To convert string data types to integer data types we can use ***Atoi / ParseInt***.

    strconv.Atoi(text)

    strconv.ParseInt(text,base,bitsize))
    

![Convert String Into Int Example](https://cdn-images-1.medium.com/max/5760/1*i-tDXyFNPLrArwi0USEbDA.png)

In the code above, you can see that strconv. Atoi / strconv.ParseInt will return 2 variables namely the output and error values. because no error is generated, the error value is nil. for base commonly fill with 10 and bitsize is which integer type we want to return. if we want to return int32, we must fill it with 32 but if 64 we can fill it with 64.

**10**. **Convert string to float**

Apart from converting to the int data type, strings can also be converted to float data types using ***ParseFloat.***

    strconv.ParseFloat(text,bitsize)

![Convert string to float example](https://cdn-images-1.medium.com/max/5760/1*JKrLbE7ulIrKD547EK_fMw.png)

In the code above, also you can see that strconv. ParseFloat will return 2 variables namely the output and error values. because no error is generated, the error value is nil. for bitsize is which integer type we want to return. if we want to return int32, we must fill it with 32 but if 64 we can fill it with 64.

Now we know some functions that we can use to manage string data types.

Reference:

 1. [https://golang.org/pkg/strings/](https://golang.org/pkg/strings/)

 2. [https://golang.org/pkg/strconv/](https://golang.org/pkg/strconv/)

See at [github](https://github.com/asdamwongmantap/stringmanipulation.git)