Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How remove some special words from a string content?

I have some strings containing code for emoji icons, like :grinning:, :kissing_heart:, or :bouquet:. I'd like to process them to remove the emoji codes.

For example, given:

Hello:grinning: , how are you?:kissing_heart: Are you fine?:bouquet:

I want to get this:

Hello , how are you? Are you fine?

I know I can use this code:

richTextBox2.Text = richTextBox1.Text.Replace(":kissing_heart:", "").Replace(":bouquet:", "").Replace(":grinning:", "").ToString();

However, there are 856 different emoji icons I have to remove (which, using this method, would take 856 calls to Replace()). Is there any other way to accomplish this?

like image 218
JAC Avatar asked May 26 '15 05:05

JAC


People also ask

How do I remove special characters from a string in node?

Use the replace() method to remove all special characters from a string, e.g. str. replace(/[^a-zA-Z0-9 ]/g, ''); . The replace method will return a new string that doesn't contain any special characters.


2 Answers

You can use Regex to match the word between :anything:. Using Replace with function you can make other validation.

string pattern = @":(.*?):";
string input = "Hello:grinning: , how are you?:kissing_heart: Are you fine?:bouquet: Are you super fan, for example. :words not to replace:";
string output = Regex.Replace(input, pattern, (m) =>
{
    if (m.ToString().Split(' ').Count() > 1) // more than 1 word and other validations that will help preventing parsing the user text
    {
        return m.ToString();
    }
    return String.Empty;
}); // "Hello , how are you? Are you fine? Are you super fan, for example. :words not to replace:"

If you don't want to use Replace that make use of a lambda expression, you can use \w, as @yorye-nathan mentioned, to match only words.

string pattern = @":(\w*):";
string input = "Hello:grinning: , how are you?:kissing_heart: Are you fine?:bouquet: Are you super fan, for example. :words not to replace:";
string output = Regex.Replace(input, pattern, String.Empty); // "Hello , how are you? Are you fine? Are you super fan, for example. :words not to replace:"
like image 101
adricadar Avatar answered Sep 17 '22 08:09

adricadar


string Text = "Hello:grinning: , how are you?:kissing_heart: Are you fine?:bouquet:";

i would solve it that way

List<string> Emoj = new List<string>() { ":kissing_heart:", ":bouquet:", ":grinning:" };
Emoj.ForEach(x => Text = Text.Replace(x, string.Empty));

UPDATE - refering to Detail's Comment

Another approach: replace only existing Emojs

List<string> Emoj = new List<string>() { ":kissing_heart:", ":bouquet:", ":grinning:" };
var Matches = Regex.Matches(Text, @":(\w*):").Cast<Match>().Select(x => x.Value);
Emoj.Intersect(Matches).ToList().ForEach(x => Text = Text.Replace(x, string.Empty));

But i'm not sure if it's that big difference for such short chat-strings and it's more important to have code that's easy to read/maintain. OP's question was about reducing redundancy Text.Replace().Text.Replace() and not about the most efficient solution.

like image 43
fubo Avatar answered Sep 19 '22 08:09

fubo