What is a Substring in Golang?

Learn via video courses
Topics Covered

Overview

Golang Substring is a part of the string in the Go language. It refers to the characters in a sequence that are contained within the larger set of the string. In most cases, a part of the string is to be extracted. In this article, we will cover everything about golang substring.

Introduction

The string is a slice of bytes enclosed in “ “ whereas the substring is a part of the string in the Go language. The substring is also known as a contiguous sequence of characters in a string. To check whether the string contains a substring or not, two options are available. The first option to check if the string contains a substring or not is to use the in-built function called Contains() whereas the second option is to write the logic for the same.

The syntax for using the Contains() function is as follows:

There are two parameters in the above Contains() function, the first parameter is a string that is trying to find a pattern whereas the second parameter is the pattern that is to be found.

Extract a Single Character

Getting a single character from the string, simple indexing can be done. Below is an example that shows how to extract a single character from the string.

Output:

Explanation:

In this example, the character is been accessed from the string via the index and hence it is called indexing. Note: convert the index value to the string else it will return the ASCII value.

Using Range-Based Slicing To Create a Substring

This is another method for creating a golang substring`. In range-based slicing, we define the starting and the end index for the targetted index which is needed to convert to string. To understand this better, look at the example given below:

Output:

Explanation:

In this example, the substring starts from index 7 up to the last index of the string. The output is shown as “to Scaler”. The range-based slicing is one of the most effective ways of generating a substring in the Go language.

You can also slice the start, end, or middle of the string. Let's take an example to slice the start of the string.

Output:

Explanation:

In this example, the syntax works similarly to that of the previous one except that it eliminates the 0th index. This results in the output as “Welcome”.

Using the Split Method

The split method can also be used for slicing the string in the Go language and extracting the substring. In this, the method separates the strings based on the characters specified. To understand this better, let's take an example given below:

Output:

Explanation:

In this example, the split method is used for separating the strings using spaces. An array of individual string elements is created from the main string. We can then use the indexing of the string to access individual items.

Check if a String is a Substring

For checking if a string is a substring or not, the Contains() method is used. To understand how to use this method, look at the example given below:

Output:

Explanation:

In this example, if we run the above code, we will get the output as true. This is because the string Scaler contains the word ‘ler’ in the string and hence the output is shown as true.

Other methods that can be used for checking substring in the string are checkIfExists() which is used for matching patterns. To understand how to use this function, let’s look at the example given below:

Output:

Explanation:

In this example, the function checkIfExists() is used for finding a pattern in the string which calls the function patternMatch in return.

Which Method to Use

For creating a golang substring, there are many methods available. The slicing however is the most intuitive method of all the options available. It is also the most efficient method among all of them and hence is widely used. The other method however are very specific according to the usage are requires careful consideration for creating a substring from a string.

Conclusion

  • Substrings are a part of the string in the Go language. It refers to the characters in a sequence that are contained within the larger set of the string.
  • The string is a slice of bytes enclosed in “ “ whereas the substring is a part of the string in the Go language. The substring is also known as a contiguous sequence of characters in a string.