Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Not claiming to be the fastest, but here are two Python solutions suggested by Dave Beazley [0], plus one I extended using compiled regular expressions:

    # string replacement
    s = ' hello  world \n'
    s.replace(' ', '')
    # 'helloworld'

    # regular expression
    import re
    re.sub('\s+', '', s)
    # 'helloworld'

    # compiled regular expression
    pat = re.compile('\s+')
    pat.sub('', s)
    # 'helloworld'
[0] https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&c...


my first python thought was

  s = ' hello  world \n'
  s = ''.join(s.split())
again probably not fast, but simple


Scala: val nospaces = "a string with spaces" filter { _ != ' ' }




Consider applying for YC's Fall 2026 batch! Applications are open till July 27.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: