Remove ALL white spaces from text
You have to tell replace() to repeat the regex:
.replace(/ /g,'')
The g character makes it a "global" match, meaning it repeats the search through the entire string. Read about this, and other RegEx modifiers available in JavaScript here.
If you want to match all whitespace, and not just the literal space character, use \s
instead:
.replace(/\s/g,'')
You can also use .replaceAll
if you're using a sufficiently recent version of JavaScript, but there's not really any reason to for your specific use case, since catching all whitespace requires a regex, and when using a regex with .replaceAll
, it must be global, so you just end up with extra typing:
.replaceAll(/\s/g,'')
How can I remove all spaces from file in Notepad++?
To delete all spaces in the file, replace ' +'
with ''
(quotes only for demonstration, please remove them). You need to have the checkbox "Regular expression" checked.
To remove all spaces and tabs, replace '[ \t]+'
with ''
(remove quotes).
This works for big files, too, where the solution of @Learner will be tiresome.
How to remove all whitespace from a string?
In general, we want a solution that is vectorised, so here's a better test example:
whitespace <- " \t\n\r\v\f" # space, tab, newline,
# carriage return, vertical tab, form feed
x <- c(
" x y ", # spaces before, after and in between
" \u2190 \u2192 ", # contains unicode chars
paste0( # varied whitespace
whitespace,
"x",
whitespace,
"y",
whitespace,
collapse = ""
),
NA # missing
)
## [1] " x y "
## [2] " ← → "
## [3] " \t\n\r\v\fx \t\n\r\v\fy \t\n\r\v\f"
## [4] NA
The base R approach: gsub
gsub
replaces all instances of a string (fixed = TRUE
) or regular expression (fixed = FALSE
, the default) with another string. To remove all spaces, use:
gsub(" ", "", x, fixed = TRUE)
## [1] "xy" "←→"
## [3] "\t\n\r\v\fx\t\n\r\v\fy\t\n\r\v\f" NA
As DWin noted, in this case fixed = TRUE
isn't necessary but provides slightly better performance since matching a fixed string is faster than matching a regular expression.
If you want to remove all types of whitespace, use:
gsub("[[:space:]]", "", x) # note the double square brackets
## [1] "xy" "←→" "xy" NA
gsub("\\s", "", x) # same; note the double backslash
library(regex)
gsub(space(), "", x) # same
"[:space:]"
is an R-specific regular expression group matching all space characters. \s
is a language-independent regular-expression that does the same thing.
The stringr
approach: str_replace_all
and str_trim
stringr
provides more human-readable wrappers around the base R functions (though as of Dec 2014, the development version has a branch built on top of stringi
, mentioned below). The equivalents of the above commands, using [str_replace_all][3]
, are:
library(stringr)
str_replace_all(x, fixed(" "), "")
str_replace_all(x, space(), "")
stringr
also has a str_trim
function which removes only leading and trailing whitespace.
str_trim(x)
## [1] "x y" "← →" "x \t\n\r\v\fy" NA
str_trim(x, "left")
## [1] "x y " "← → "
## [3] "x \t\n\r\v\fy \t\n\r\v\f" NA
str_trim(x, "right")
## [1] " x y" " ← →"
## [3] " \t\n\r\v\fx \t\n\r\v\fy" NA
The stringi
approach: stri_replace_all_charclass
and stri_trim
stringi
is built upon the platform-independent ICU library, and has an extensive set of string manipulation functions. The equivalents of the above are:
library(stringi)
stri_replace_all_fixed(x, " ", "")
stri_replace_all_charclass(x, "\\p{WHITE_SPACE}", "")
Here "\\p{WHITE_SPACE}"
is an alternate syntax for the set of Unicode code points considered to be whitespace, equivalent to "[[:space:]]"
, "\\s"
and space()
. For more complex regular expression replacements, there is also stri_replace_all_regex
.
stringi
also has trim functions.
stri_trim(x)
stri_trim_both(x) # same
stri_trim(x, "left")
stri_trim_left(x) # same
stri_trim(x, "right")
stri_trim_right(x) # same
Remove all whitespace in a string
If you want to remove leading and ending spaces, use str.strip()
:
>>> " hello apple ".strip()
'hello apple'
If you want to remove all space characters, use str.replace()
(NB this only removes the “normal” ASCII space character ' ' U+0020
but not any other whitespace):
>>> " hello apple ".replace(" ", "")
'helloapple'
If you want to remove duplicated spaces, use str.split()
followed by str.join()
:
>>> " ".join(" hello apple ".split())
'hello apple'
How do i remove all white spaces from a string?
Try this :
images= images.Replace(" ", String.Empty);
Bash - How to remove all white spaces from a given text file?
$ man tr
NAME
tr - translate or delete characters
SYNOPSIS
tr [OPTION]... SET1 [SET2]
DESCRIPTION
Translate, squeeze, and/or delete characters from standard
input, writing to standard output.
In order to wipe all whitespace including newlines you can try:
cat file.txt | tr -d " \t\n\r"
You can also use the character classes defined by tr (credits to htompkins comment):
cat file.txt | tr -d "[:space:]"
For example, in order to wipe just horizontal white space:
cat file.txt | tr -d "[:blank:]"
Batch: remove all white spaces from a text file?
!line:~1!
removes the first character. What you want to do is removing every space, so use string substitution:
!line: =!
(replace space with nothing)
if there are TABs too, use another !line: =!
(that's a TAB, not spaces)
Remove all white space from string JavaScript
Use this:
str.replace(/\s+/g, '');
Instead of this:
str.trim()
Related Topics
Generating Non-Repeating Random Numbers in Js
What's the Recommended Way to Extend Angularjs Controllers
What Is Returned from a Constructor
A Cors Post Request Works from Plain JavaScript, But Why Not with Jquery
Differencebetween JavaScript Promises and Async Await
Adding 'Click' Event Listeners in Loop
Clone Object Without Reference JavaScript
Manipulating Innerhtml Removes the Event Handler of a Child Element
How to Check in JavaScript If One Element Is Contained Within Another
Convert String in Dot Notation to Get the Object Reference
JavaScript Get Window X/Y Position for Scroll
Create Svg Tag with JavaScript
Coordinating Parallel Execution in Node.Js
Sorting in JavaScript: Should Every Compare Function Have a "Return 0" Statement
React-Router: No Not Found Route
Determine Pixel Length of String in JavaScript/Jquery
How to Logout of an Application Where I Used Oauth2 to Login with Google