Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strip all whitespace from a string

Tags:

go

What is the fastest way to strip all whitespace from some arbitrary string in Go.

I am chaining two function from the string package:

response = strings.Join(strings.Fields(response),"") 

Anyone have a better way to do this?

like image 212
Omn Avatar asked Aug 18 '15 20:08

Omn


People also ask

How do I remove all white spaces from a string?

The replaceAll() method of the String class replaces each substring of this string that matches the given regular expression with the given replacement. You can remove white spaces from a string by replacing " " with "".

How do I strip spaces from a string in Python?

Use the . strip() method to remove whitespace and characters from the beginning and the end of a string. Use the . lstrip() method to remove whitespace and characters only from the beginning of a string.

How do you remove all the spaces from a string in JS?

Using Split() with Join() method To remove all whitespace characters from the string, use \s instead. That's all about removing all whitespace from a string in JavaScript.


2 Answers

Here is some benchmarks on a few different methods for stripping all whitespace characters from a string: (source data):

 BenchmarkSpaceMap-8                     2000       1100084 ns/op      221187 B/op          2 allocs/op BenchmarkSpaceFieldsJoin-8              1000       2235073 ns/op     2299520 B/op         20 allocs/op BenchmarkSpaceStringsBuilder-8          2000        932298 ns/op      122880 B/op          1 allocs/op 
  • SpaceMap: uses strings.Map; gradually increases the amount of allocated space as more non-whitespace characters are encountered
  • SpaceFieldsJoin: strings.Fields and strings.Join; generates a lot of intermediate data
  • SpaceStringsBuilder uses strings.Builder; performs a single allocation, but may grossly overallocate if the source string is mainly whitespace.
package main_test  import (     "strings"     "unicode"     "testing" )  func SpaceMap(str string) string {     return strings.Map(func(r rune) rune {         if unicode.IsSpace(r) {             return -1         }         return r     }, str) }  func SpaceFieldsJoin(str string) string {     return strings.Join(strings.Fields(str), "") }  func SpaceStringsBuilder(str string) string {     var b strings.Builder     b.Grow(len(str))     for _, ch := range str {         if !unicode.IsSpace(ch) {             b.WriteRune(ch)         }     }     return b.String() }  func BenchmarkSpaceMap(b *testing.B) {     for n := 0; n < b.N; n++ {         SpaceMap(data)     } }  func BenchmarkSpaceFieldsJoin(b *testing.B) {     for n := 0; n < b.N; n++ {         SpaceFieldsJoin(data)     } }  func BenchmarkSpaceStringsBuilder(b *testing.B) {     for n := 0; n < b.N; n++ {         SpaceStringsBuilder(data)     } } 
like image 143
Tim Cooper Avatar answered Oct 03 '22 08:10

Tim Cooper


I found the simplest way would be to use strings.ReplaceAll like so:

randomString := "  hello      this is a test" fmt.Println(strings.ReplaceAll(randomString, " ", ""))  >hellothisisatest 

Playground

like image 33
Datsik Avatar answered Oct 03 '22 08:10

Datsik