Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trim the string using node.js?

Tags:

I am trying to trim the string. I google it for trim.

The tips i am getting.

  1. name.trim(); -- Not recognized trim() function
  2. npm install --save string

           var S = require('string');
           S('hello ').trim().s;    -- this also same issue    
    

Can you any one assist me what is the problem of the above method or anything i have missed it?

like image 881
Vanarajan Avatar asked Jun 11 '15 10:06

Vanarajan


People also ask

How do I trim a string in node?

The trim() function is a string function of Node. js which is used to remove white spaces from the string. Parameter: This function does not accepts any parameter. Return Value: The function returns the string without white spaces.

How do you trim strings?

Java String trim()The Java String class trim() method eliminates leading and trailing spaces. The Unicode value of space character is '\u0020'. The trim() method in Java string checks this Unicode value before and after the string, if it exists then the method removes the spaces and returns the omitted string.

What does trim () do in JavaScript?

trim() The trim() method removes whitespace from both ends of a string and returns a new string, without modifying the original string. Whitespace in this context is all the whitespace characters (space, tab, no-break space, etc.)

How do I remove a space from a string in node JS?

You can remove the whitespace in a string by using a string. replace(/\s/g, "") regular expression or a string. split(" "). join("") method.


1 Answers

You don't need jquery for this...

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

This will output the following in the console:

"Hello World!"

like image 150
Alex Avatar answered Oct 09 '22 02:10

Alex