[Golang] String Manipulation

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

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

strings.ToLower(text)
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)

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)
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)
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)
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)
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))
Split String Example

8. Count the specific number of characters

We can counting specific character using Count.

strings.Count(text,character_will_counted)
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))
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)
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/

--

--

--

All about go language

Recommended from Medium

Remote Car Control

What traits do successful pentesters have in common?

🔰 Create a Setup so that you can ping google  but not able to ping Facebook from same system

Big O: Simplified

MUGA Line Markings in Northamptonshire #MUGA #Court #Line #Marking #Northamptonshire https://t.co/7n

My thoughts on learning Web in 2021

Sensor and actuator communication via Mainflux MQTT broker

Loading and Displaying Well Log Data

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Amiruddin Saddam

Amiruddin Saddam

Backend Engineer at Jamtangan.com And will always learn something new.

More from Medium

Build simple gRPC server with Golang

Blockchain in Golang using Docker

Web Sockets in Golang

Very basic JWT Authentication with Golang