Golang program to remove all whitespaces from a string


When we remove whitespaces from a string it means removing blank spaces, tabs, and other white space characters, including newline characters. This can be helpful for parsing user input or working with data in a particular format, among other instances where we wish to clean up or standardize a string before processing it further. There are various ways to eliminate whitespace from a string. A few examples include: Using the TrimSpace function, whitespace characters at the beginning and end of a string are eliminated. Using the Replace function to replace any whitespace in the string with a string that is entirely empty and using the regexp package, all whitespace can be eliminated. In this article we will see how to remove whitespaces from a string using Golang examples.

Method 1: Using strings.Replace() function

In this method, the strings.Replace() function is instructed to replace all instances with the third argument (-1). After that, a string devoid of whitespace is printed. Let’s see through the Example: and algorithm to get a better understanding of the concept.

Syntax

strings.Replace()

The strings package in Golang offers a built-in Replace function that may be used to swap out a certain substring in a supplied string for another substring. Three parameters are required by the Replace function: the first string The substituted substring and the substitute substring. It gives back a new string with the replacement substring substituted for every instance of the original substring.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package) and strings package in the program where main produces executable Examples and fmt helps in formatting input and output.

  • Step 2 − Set the string from which you want to remove whitespaces as the initial value of the variable mystr.

  • Step 3 − To replace every instance of the space character (" ") with an empty string, use the Replace function (""). The function is instructed to replace all instances with the third argument (-1).

  • Step 4 − The updated string is printed on the console using fmt.Println() function where ln means new line.

Example

In this example, to replace every instance of the space character (" ") with an empty string, we use the Replace function ("").

package main
import (
   "fmt"
   "strings"
)

func main() {
   mystr := "Hi i am an engineer i work as a frontend developer" //create strings
   fmt.Println("The string before removal of whitespaces is:", mystr)
   mystr = strings.Replace(mystr, " ", "", -1)  //using this built-in function remove whitespaces
   fmt.Println("The string after removing whitespaces is:")
   fmt.Println(mystr) //remove string without whitespaces
}

Output

The string before removal of whitespaces is: Hi i am an engineer i work as a frontend developer
The string after removing whitespaces is:
Hiiamanengineeriworkasafrontenddeveloper

Method 2: Using regexp.Compile method

The string in this sample has all whitespaces removed using regular expressions. To create a regular expression pattern that matches one or more whitespace characters (s+), we utilise the regexp package. The ReplaceAllString function is then used to replace every instance where the pattern matches with an empty string. After that, a string devoid of whitespace is printed. Let’s see through the Example: and algorithm to know another way to remove whitespace from the string.

Syntax

regexp.Compile()

A regular expression in Go can be compiled into a more effective form for matching using the Compile function in the regexp package. The regular expression pattern is passed as a single parameter to the Compile function, which returns a *Regexp struct.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package) and regexp package in the program where main produces executable Example:s and fmt helps in formatting input and output.

  • Step 2 − Set the string from which you want to remove whitespaces as the initial value of the variable mystr in the main function.

  • Step 3 − Make use of regexp to create a regular expression pattern that matches one or more whitespace characters.

  • Step 4 − Compile("\s+")

  • Step 5 − Use the ReplaceAllString method to replace all pattern matches with an empty string using reg.

  • Step 6 − The syntax will be put like ReplaceAllString(str, "") to remove the whitespace.

  • Step 7 − Print the updated string on the console using fmt.Println() function where ln means new line.

Example

In this example we are going to use regexp.Compile() function of golang to remove all whitespace of a string

package main
import (
   "fmt"
   "regexp"
)

func main() {
   mystr := "Hi i am a frontend developer"  //create string
   fmt.Println("The string before removal of whitespaces is:", mystr)
   reg, _ := regexp.Compile("s+")  //compile 
   mystr = reg.ReplaceAllString(mystr, "") //remove whitespaces
   fmt.Println("The string after removal of whitespaces is:")
   fmt.Println(mystr) //print string without whitespaces
}

Output

The string before removal of whitespaces is: Hi i am a frontend developer
The string after removal of whitespaces is:
Hi i am a frontend developer

Conclusion

We executed the program of removing all whitespaces from string using two examples. In the first example we used strings.Replace() function which is a built-in function and in the second example we used regexp.Compile() function.

Updated on: 20-Feb-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements