DEV Community

Cover image for String Manipulation With Golang
Amiruddin Saddam
Amiruddin Saddam

Posted on

String Manipulation With Golang

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)
Enter fullscreen mode Exit fullscreen mode

ToUpper Example

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

strings.ToLower(text)
Enter fullscreen mode Exit fullscreen mode

ToLower Example

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)
Enter fullscreen mode Exit fullscreen mode

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

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)
Enter fullscreen mode Exit fullscreen mode

Title Example

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)
Enter fullscreen mode Exit fullscreen mode

Compare Non-Case Sensitive word Example

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)
Enter fullscreen mode Exit fullscreen mode

Replace Example

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)
Enter fullscreen mode Exit fullscreen mode

Join String Example

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))
Enter fullscreen mode Exit fullscreen mode

Split String Example

8. Count the specific number of characters

We can counting specific character using Count.

strings.Count(text,character_will_counted)
Enter fullscreen mode Exit fullscreen mode

Count String Example

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))
Enter fullscreen mode Exit fullscreen mode

Convert String Into Int Example

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)
Enter fullscreen mode Exit fullscreen mode

Convert string to float example

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/

  2. https://golang.org/pkg/strconv/

See at github

Discussion (0)