Remove Multiple Whitespaces

Is there a simple way to remove multiple spaces in a string?

>>> import re
>>> re.sub(' +', ' ', 'The quick brown fox')
'The quick brown fox'

Substitute multiple whitespace with single whitespace in Python

A simple possibility (if you'd rather avoid REs) is

' '.join(mystring.split())

The split and join perform the task you're explicitly asking about -- plus, they also do the extra one that you don't talk about but is seen in your example, removing trailing spaces;-).

How to best replace multiple whitespaces by one (in python)?

You can use:

import re
a = re.sub(' +',' ',a)

Merge Multiple spaces to single space; remove trailing/leading spaces

This seems to meet your needs.

string <- "  Hi buddy   what's up   Bro "
library(stringr)
str_replace(gsub("\\s+", " ", str_trim(string)), "B", "b")
# [1] "Hi buddy what's up bro"

How to remove multiple spaces in Strings with Swift 2

In Swift 2, join has become joinWithSeparator and you call it on the array.

In filter, isEmpty should be called on the current iteration item $0.

To replace whitespaces and newline characters with unique space characters as in your question:

extension String {
func condenseWhitespace() -> String {
let components = self.componentsSeparatedByCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
return components.filter { !$0.isEmpty }.joinWithSeparator(" ")
}
}

let result = "Hello World.\nHello!".condenseWhitespace() // "Hello World. Hello!"

Because your function does not take any parameter you could make it a property instead:

extension String {
var condensedWhitespace: String {
let components = self.componentsSeparatedByCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
return components.filter { !$0.isEmpty }.joinWithSeparator(" ")
}
}

let result = "Hello World.\nHello!".condensedWhitespace // "Hello World. Hello!"

In Swift 3 there's even more changes.

Function:

extension String {
func condenseWhitespace() -> String {
let components = self.components(separatedBy: NSCharacterSet.whitespacesAndNewlines)
return components.filter { !$0.isEmpty }.joined(separator: " ")
}
}

let result = "Hello World.\nHello!".condenseWhitespace()

Property:

extension String {
var condensedWhitespace: String {
let components = self.components(separatedBy: NSCharacterSet.whitespacesAndNewlines)
return components.filter { !$0.isEmpty }.joined(separator: " ")
}
}

let result = "Hello World.\nHello!".condensedWhitespace

In Swift 4.2 NSCharacterSet is now CharacterSet, and you can omit and use dot syntax:

extension String {
func condenseWhitespace() -> String {
let components = self.components(separatedBy: .whitespacesAndNewlines)
return components.filter { !$0.isEmpty }.joined(separator: " ")
}
}

let result = "Hello World.\nHello!".condenseWhitespace() // "Hello World. Hello!"

Remove multiple whitespaces by single space in a python list

You can use re.sub with a regular expression for this:

s = "HELLO     MR      GOOD    SOUL"
re.sub(r"\s+", " ", s)

\s is whitespace, + means one or more. Since Regex is greedy by default, the pattern \s+ will match as many consecutive spaces as possible.

The output:

'HELLO MR GOOD SOUL'

If you actually have a list of strings, you can do a simple list comprehension:

list_of_strings = [...]
[re.sub(r"\s+", " ", s) for s in list_of_strings]

If you want to do it in-place:

for idx, s in enumerate(list_of_strings):
list_of_strings[idx] = re.sub(r"\s+", " ", s)

How do I replace multiple spaces with a single space in C#?

string sentence = "This is a sentence with multiple    spaces";
RegexOptions options = RegexOptions.None;
Regex regex = new Regex("[ ]{2,}", options);
sentence = regex.Replace(sentence, " ");

Not able to remove multiple whitespace(s) in a string in java

I can only guess that the spaces are not really space character (U+0020), but some Unicode space character, like U+00A0 NO BREAK SPACE. \s by default only matches space characters in the ASCII range, so they are not removed.

If you want to remove all Unicode spaces, you have to enable the UNICODE_CHARACTER_CLASS flag with inline construct (?U)

String myString2 = myString1.replaceAll("(?U)\\s+", "");


Related Topics



Leave a reply



Submit