How can I remove new line from the user input in Node.js?
The code:
var net = require("net");
var clientData = null;
var server = net.createServer(function(client) {
client.on("connect", function() {
client.write("Enter something: ");
});
client.on("data", function(data) {
var clientData = data;
if (clientData != null) {
client.write("You entered " + "'" + clientData + "'" + ". Some more text.");
}
});
});
server.listen(4444);
Let's say I type "Test" in the console, then the following is returned:
You entered 'Test
'. Some more text.
I would like such an output to appear in the single line. How can I do this?
You just need to strip trailing new line.
You can cut the last character like this :
clientData.slice(0, clientData.length - 1)
Or you can use regular expressions :
clientData.replace(/\n$/, '')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With