Rectangle 27 245

String.Trim() will trim all white-spaces from start and end of a string:

"   A String   ".Trim() -> "A String"

String.TrimStart() will trim all white-spaces from the start of a string:

"   A String   ".TrimStart() -> "A String   "

String.TrimEnd() will trim all white-spaces from the end of a string:

"   A String   ".TrimEnd() -> "   A String"

for the MS definition of whitespace. I met a weird behavior that .TrimEnd() doesn't work (for non-breaking space character), but in the end is just that the character not listed in documentation.

There are numerous ways to trim strings, and quite a few are bench-marked. Still, I like .Trim() as being the quickest to write and easiest to read.

c# - How to remove all white spaces from the start or end of a string?...

c# string trim removing-whitespace
Rectangle 27 186

Use String.trim() method to get rid of whitespaces (spaces, new lines etc.) from the beginning and end of the string.

String trimmedString = myString.trim();

If you want to get rid of, say, every line break, you could use regular expressions.

The question is about newlines. This removes more than just newlines

How to remove newlines from beginning and end of a string (Java)? - St...

java string line blank-line
Rectangle 27 179

Use String.trim() method to get rid of whitespaces (spaces, new lines etc.) from the beginning and end of the string.

String trimmedString = myString.trim();

If you want to get rid of, say, every line break, you could use regular expressions.

The question is about newlines. This removes more than just newlines

How to remove newlines from beginning and end of a string (Java)? - St...

java string line blank-line
Rectangle 27 227

SELECT REPLACE(fld_or_variable, ' ', '')

Edit Just to clarify; its a global replace, there is no need to trim() or worry about multiple spaces:

create table #t (c char(8), v varchar(8))
insert #t (c, v) values 
    ('a a'    , 'a a'    ),
    ('a a  '  , 'a a  '  ),
    ('  a a'  , '  a a'  ),
    ('  a a  ', '  a a  ')

select
    '''' + c + '''' [IN], '''' + replace(c, ' ', '') + '''' [OUT]
from #t  
union all select
    '''' + v + '''', '''' + replace(v, ' ', '') + ''''
from #t 

IN            OUT
'a a     '   'aa'
'a a     '   'aa'
'  a a   '   'aa'
'  a a   '   'aa'
'a a'        'aa'
'a a  '      'aa'
'  a a'      'aa'
'  a a  '    'aa'

It should replace all whitespace everywhere

It WILL remove all white spaces except for trailing. To remove trailing add TRIM(REPLACE(fld_or_variable, ' ', '')). The reason why it remove even multiple spaces is because... well it change every space characters to nothing wether the spaces are next to each other or not.

Replace removes all spaces even trailing one. If there are still apprently spaces in the data after this is done, it is likely they are not spaces but unprintable characters like tabs or carraige returns.

Remove all spaces from a string in SQL Server - Stack Overflow

sql sql-server string whitespace trim
Rectangle 27 2

if you want to remove spaces,-, and another text from string then use following :

suppose you have a mobile number in your Table like '718-378-4957' or ' 7183784957' and you want replace and get the mobile number then use following Text.

select replace(replace(replace(replace(MobileNo,'-',''),'(',''),')',''),' ','') from EmployeeContactNumber

Remove all spaces from a string in SQL Server - Stack Overflow

sql sql-server string whitespace trim
Rectangle 27 10

The trim from jQuery is what you are looking for.

$.trim('  string with spaces at the ends   ');
'  string with spaces at the ends   '.trim()

javascript - How to remove last and first spaces from a string? - Stac...

javascript
Rectangle 27 10

The trim from jQuery is what you are looking for.

$.trim('  string with spaces at the ends   ');
'  string with spaces at the ends   '.trim()

javascript - How to remove last and first spaces from a string? - Stac...

javascript
Rectangle 27 10

The trim from jQuery is what you are looking for.

$.trim('  string with spaces at the ends   ');
'  string with spaces at the ends   '.trim()

javascript - How to remove last and first spaces from a string? - Stac...

javascript
Rectangle 27 10

The trim from jQuery is what you are looking for.

$.trim('  string with spaces at the ends   ');
'  string with spaces at the ends   '.trim()

javascript - How to remove last and first spaces from a string? - Stac...

javascript
Rectangle 27 1

To remove the spaces in a string left and right. To remove space in middle use Replace.

You can use RTRIM() to remove spaces from the right and LTRIM() to remove spaces from the left hence left and right spaces removed as follows:

SELECT * FROM table WHERE LTRIM(RTRIM(username)) = LTRIM(RTRIM("Bob alias baby"))

Remove all spaces from a string in SQL Server - Stack Overflow

sql sql-server string whitespace trim
Rectangle 27 34

I want to remove the "(", ")", and "-" characters from the string.

You can use the std::remove_if() algorithm to remove only the characters you specify:

The std::remove_if() algorithm requires something called a predicate, which can be a function pointer like the snippet above.

You can also pass a function object (an object that overloads the function call () operator). This allows us to create an even more general solution:

#include <iostream>
#include <algorithm>
#include <string>

class IsChars
{
public:
    IsChars(const char* charsToRemove) : chars(charsToRemove) {};

    bool operator()(char c)
    {
        for(const char* testChar = chars; *testChar != 0; ++testChar)
        {
            if(*testChar == c) { return true; }
        }
        return false;
    }

private:
    const char* chars;
};

int main()
{
    std::string str("(555) 555-5555");
    str.erase(std::remove_if(str.begin(), str.end(), IsChars("()- ")), str.end());
    std::cout << str << std::endl; // Expected output: 5555555555
}

You can specify what characters to remove with the "()- " string. In the example above I added a space so that spaces are removed as well as parentheses and dashes.

ispunct(int c)

Excellent implementation. This method worked flawlessly and has a lot of room for further dynamics. Thank you for the response. MSalters, I will also look up the ispunct(int c) function and report back on my workings.

How to remove certain characters from a string in C++? - Stack Overflo...

c++ string character
Rectangle 27 6

$string = str_replace(' ', '', $original_string);
str_replace()
var string = original_string.replace(' ', '');
String.replace()

If you need to remove all whitespace from a string (including tabs etc) then you can use:

$string = preg_replace('/\s/', '', $original_string);
preg_replace()
var string = original_string.replace(/\s/g, '');

How to remove all spaces from a string in PHP and Javascript - Stack O...

php javascript string
Rectangle 27 1

The gsub should work but probably the input string also has a space in front so you can do params[:to].strip.gsub("+", "") this removes the spaces before and after the string.

params[:to].gsub(/[^\d]/,"")

ruby on rails - How to remove certain characters in a string without c...

ruby-on-rails ruby
Rectangle 27 5

$string = str_replace(' ', '', $original_string);
str_replace()
var string = original_string.replace(' ', '');
String.replace()

If you need to remove all whitespace from a string (including tabs etc) then you can use:

$string = preg_replace('/\s/', '', $original_string);
preg_replace()
var string = original_string.replace(/\s/g, '');

How to remove all spaces from a string in PHP and Javascript - Stack O...

php javascript string
Rectangle 27 1

This method will remove the spaces out of your string. What it does is iterates through each char and then compares it to the UTF-16 value for space. If it's a space it won't add it.

public String RemoveSpaces(String withSpaces){
    StringBuilder sb = new StringBuilder();

    for (int i = 0; i < withSpaces.Length; i++)
    {
        if (withSpaces[i] != 32)
        {
           sb.Append(withSpaces[i]);
        }
    }

    return sb.ToString();
}

Well, your code certainly does what it is doing correctly, and certainly in a much more creative way than just using the boring string.Replace(" ", string.Empty) ;)

Haha Okay, so yes, I admit there ARE easier ways. But who wants a plain jane string.replace?

You know, some times you have to look at the standard and say "I know you're more efficiently written. I know you are less code for me to write and I know that my peers will look down on me if I don't use you. But I'm in college damn it and my professor wants me to do things the hard way!"

This works because 32 is the UTF-16 code-unit for the Unicode space character. Thanks to the Unicode designers, it's the same as ASCII. (withSpaces[i] is a char, which is a UTF-16 code-unit.)

Remove spaces from a string C# (something simple!) - Stack Overflow

c# string spaces
Rectangle 27 7

If there are multiple white spaces in a string, then replace may not work correctly. For that, the following function should be used.

CREATE FUNCTION RemoveAllSpaces
(
    @InputStr varchar(8000)
)
RETURNS varchar(8000)
AS
BEGIN
declare @ResultStr varchar(8000)
set @ResultStr = @InputStr
while charindex(' ', @ResultStr) > 0
    set @ResultStr = replace(@InputStr, ' ', '')

return @ResultStr
END
select dbo.RemoveAllSpaces('aa  aaa       aa aa                 a')
aaaaaaaaaa

"@InputStr" should be "@ResultStr" in the body of the while loop.

@jjoelson would you mind to create this function and test it before pointing out mistakes?

Farhan, if REPLACE worked the way you think it does then @jjoelson's first comment is correct. Otherwise you'll have an infinite loop because InputStr never changes so ResultStr will always be the first REPLACE result. The reason your code works is because REPLACE is needed only 1 time. It's never called a second time no matter what string you throw at it. Add a counter and print it out with each iteration. It'll always be 1. REPLACE(InputStr, ' ', '') will remove every space with one call.

Remove all spaces from a string in SQL Server - Stack Overflow

sql sql-server string whitespace trim
Rectangle 27 5

column_name

You can use column_name or column_name

Sounds good, but did not work for me !!

Remove all spaces from a string in SQL Server - Stack Overflow

sql sql-server string whitespace trim
Rectangle 27 2

var output = '/var/www/site/Brand new document.docx'.replace(/ /g, ""); 
    or
  var output = '/var/www/site/Brand new document.docx'.replace(/ /gi,"");

Note: Though you use 'g' or 'gi' for removing spaces both behaves the same.

If we use 'g' in the replace function, it will check for the exact match. but if we use 'gi', it ignores the case sensitivity.

jquery - How to remove spaces from a string using JavaScript? - Stack ...

javascript jquery text
Rectangle 27 2

Following @rsplak answer: actually, using split/join way is faster than using regexp. See the performance test case

var result = text.split(' ').join('')
var result = text.replace(/\s+/g, '')

On small texts this is not relevant, but for cases when time is important, e.g. in text analisers, especially when interacting with users, that is important.

On the other hand, \s+ handles wider variety of space characters. Among with \n and \t, it also matches \u00a0 character, and that is what is turned in, when getting text using textDomNode.nodeValue.

So I think that conclusion in here can be made as follows: if you only need to replace spaces ' ', use split/join. If there can be different symbols of symbol class - use replace(/\s+/g, '')

jquery - How to remove spaces from a string using JavaScript? - Stack ...

javascript jquery text
Rectangle 27 2

If you want all whitespace removed, use this:

"abc def. fds sdff.".replace(/\s/g, '');
Returns: "abcdef.fdssdff."

If you want only double-spaces removed, use:

"abc   def.  fds      sdff.".replace(/\s\s/g, ' ');
Returns: "abc def. fds sdff."

If you want the space left after a period, use:

"abc def. fds   sdff.".replace(/[^.]\s/g, '')
Returns: "abcdef. fdssdff."

javascript - How to remove duplicate white spaces in a string? - Stack...

javascript regex