7

I have a string like

↵my name is Pankaj↵

I want to remove the character from the string.

"↵select * from eici limit 10".replace("↵", "") 

Works fine. But I want to know if there is any other way to remove these kind of unnecessary characters.

gc5
  • 9,468
  • 24
  • 90
  • 151
ipankaj
  • 77
  • 1
  • 1
  • 8
  • One more info that "↵" character is not available in code editors like sublime-text or notepad++. – ipankaj Mar 17 '15 at 11:31
  • 1
    *way to remove these kind of unnecessary characters.* Do you wish to remove a set of characters like `↵`. If its just `↵` I would go for `replace` – nu11p01n73R Mar 17 '15 at 11:32

5 Answers5

9

works fine.

If it works fine, then why bother? Your solution is totally acceptable.

but I want to know is there is any other way to remove these kind of unnecessary characters.

You could also use

"↵select * from eici limit 10".split("↵").join("") 
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
9

Instead of using the literal character, you can use the codepoint.

Instead of "↵" you can use "\u21b5".

To find the code, use '<character>'.charCodeAt(0).toString(16), and then use like \u<number>.

Now, you can use it like this:

string.split("\u21b5").join(''); //split+join

string.replace(/\u21b5/g,'');  //RegExp with unicode point
Ismael Miguel
  • 4,185
  • 1
  • 31
  • 42
  • 2
    This is a safe option instead of using ↵ directly, it will be converted into unsupported character which may not in all environments. – Ashvin777 Mar 24 '20 at 13:17
2

You can also try regex as well,

"↵select * from eici limit 10".replace(/↵/g, "")
KodeFor.Me
  • 13,069
  • 27
  • 98
  • 166
1

With something like

s.replace(/[^a-zA-Z0-9_ ]/g, "")

you can for example keep only alphabetic (a-zA-Z), numeric (0-9) underscores and spaces.

Some characters require to be specified with a backslash in front of them (for example ], -, / and the backslash itself).

You are however going to run into problems when your start deploying to an international audience, where "strange characters" are indeed the norm.

6502
  • 112,025
  • 15
  • 165
  • 265
  • This answer points out too generic. OP is just asking how can he "replace" or remove a specific character, not how to use regex over a string. It can lead, as you clarify, to internationalization problems. – Sergio A. Jan 28 '20 at 12:27
-1

I had the same issue but used the trim() function to clean out all extra spaces and ↵ characters.

str.trim();