Click here to Skip to main content
14,934,046 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a given string. Before adding it to the database, i need to check, if a final space is given. If yes it should delete them.

How can i do this?

What I have tried:

I tried that one

input.split(' ').join('');
. That one removes all spaces.
Posted
Updated 26-Jun-18 5:37am
Comments
Kornfeld Eliyahu Peter 26-Jun-18 8:22am
   
Only final? Not leading?
F-ES Sitecore 26-Jun-18 8:26am
   
google "javascript remove trailing space" and you'll get the answer in seconds.
Sascha Manns 26-Jun-18 8:26am
   
Yes just the final one.

For cleaning both leading and trailing white-spaces there is a trim() method...
There is also trimEnd() and trimStart(), but some of the browsers does not support them...
For all there is the regex approach (trim() is actually a wrapper for this)...
JavaScript
input.replace(/\s+$/, '');
   
Comments
Sascha Manns 26-Jun-18 8:41am
   
Great thank you very much.
Maciej Los 26-Jun-18 13:51pm
   
5ed!
Kornfeld Eliyahu Peter 26-Jun-18 15:00pm
   
Thank you...
Regex for this is overkill. There is no need for the performance hit for such a simple task. I would say the fastest is to check if there is a final space and then update;

private static bool CheckEndingBlank(ref string str)
{
    if (str[str.Length - 1] == ' ')
    {
        str = str.TrimEnd(' ');
        return true;
    }
    return false;
}


I am assuming here that you really do want to know if the last character is a space and want to remove the all trailing spaces.
   
Comments
Maciej Los 26-Jun-18 13:53pm
   
5ed!
[no name] 1-Jul-18 12:27pm
   
Hopefully nobody will use it with an empty string ;)
Clifford Nelson 17-Jul-18 19:58pm
   
Well that is true. If it is possible then you would need to add a check. You could make it a condition, and then should have a Debug.Assert. :-}
As you already got answers, here is a few interesting links to help building and debugging RegEx.

Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx:
Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
   
Comments
Kornfeld Eliyahu Peter 26-Jun-18 9:35am
   
Add to your list :-)
https://regex101.com/
https://regexper.com/
Patrice T 26-Jun-18 9:51am
   
Thank you,
I will see to them after work.
Patrice T 26-Jun-18 16:38pm
   
Added to my references.
But I like Debuggex better than regexper because it also allow to test what match the Regex.
Sascha Manns 26-Jun-18 9:42am
   
Thank you for your Links. That helps a lot.
Patrice T 26-Jun-18 9:50am
   
Thank you,
I particularly like the debuggex
If you want to remove space at begin and end of a string, use the below

var str = "       Hello World!     ";
var trim = str.trim()


If you want to remove space only at the end of a string, use the below

var str = "       Hello World!     ";
var trim = str.replace(/(\s+$)/g, "")


Hope this helps you.
   
Comments
Sascha Manns 26-Jun-18 9:43am
   
Thank you very much for helping.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900