Be concise.
Be useful.
All contributions dictatorially edited by webmasters to match personal tastes.
Please do not paste any copyright violating material.
Please try to avoid dependencies to third-party libraries and frameworks.
- C++
- C#
- Erlang
- Go
- Haskell
- JS
- Java
- Java
- Java
- Java
- Lisp
- Pascal
- Pascal
- Perl
- Python
- Python
- Python
- Ruby
- Ruby
- Rust
auto t = s;
t.erase(std::ranges::remove_if(t, [](const char c) { return std::isspace(c); }).begin(),t.end()) ;
[Ch || Ch <- "This is a\tstring with\nwhite\rspaces", Ch /= 8, Ch /= 9, Ch /= 10, Ch /= 13, Ch /= 32 ].
using list comprehension
String t = s.replaceAll("\\s+", "");
String t = "";
for (char c : s.toCharArray())
if (!isWhitespace(c)) t = t + c;
String t = "";
for (char c : s.toCharArray())
switch (c) {
case ' ', '\t', '\n', '\r' -> {}
default -> t = t + c;
}
import static java.lang.Character.isWhitespace;
import static java.lang.String.valueOf;
import static java.util.stream.Collectors.joining;
String t = s.chars()
.filter(c -> !isWhitespace(c))
.mapToObj(c -> valueOf((char) c))
.collect(joining());
(defun remove-whitespace (s &aux |t|)
"Return a copy of S (a sequence) excluding all the characters space, tab, linefeed, return, and formfeed."
(setf |t|
(remove-if
(lambda (item) (find item '(#\Space #\Tab #\Linefeed #\Return #\Page)))
s))
|t|)
It would be better to simply return the result of REMOVE-IF, but the assignment requires the variable t. Note that T is a reserved constant in Lisp, so we use t (lowercase). The Lisp reader normally interns symbol names in uppercase, but enclosing the symbol name in vertical bars (pipes) forces the reader to intern the lowercase t. As the Lisp interpreter respects the case of symbol names, t is not the same name as T.
SetLength(t, Length(s));
i := 0;
for Ch in S do
if (Ch > #32) then
begin
Inc(i);
t[i] := Ch;
end;
SetLength(t, i);
Without regular expressions.
Probably faster then using consecutive calls to Delete()
Using SetLength is faster than repetitive string concatenation.
Probably faster then using consecutive calls to Delete()
Using SetLength is faster than repetitive string concatenation.
my $t = $s;
$t =~ s/\s*//g;
t = ''.join(s.split())
"... If sep is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace."
t = s.gsub(/[[:space:]]/, "")
[[:space:]] is a POSIX bracket expression.
t = s.gsub(/\s/, "")