Eren
Eren

Reputation: 183

Replace a Specific Text From a Text

I'm writing a chat helper tool for a game with a custom library.

I want to change specific variables when player sends the message.

This is my code

static List<string> asciis = new List<string> { "shrug", "omg" };
static List<string> converteds = new List<string> { @"¯\_(ツ)_/¯", @"◕_◕"};

private static void Game_OnInput(GameInputEventArgs args)
{
    newtext = args.Input;
    foreach (var ascii in asciis)
    {
        foreach (var converted in converteds)
        {              
            if (args.Input.Contains(ascii))
            {
                newtext = args.Input.Replace(ascii, converted);
                Game.Say(newtext);
            }
        }
    }
}

As you can see I'm trying to get the texts from "asciis" and convert them to "converteds" (in order).

Whenever I type something that not in "asciis" list it perfectly works. But whenever I type shrug it prints ¯\_(ツ)_/¯ + ◕_◕ + ◕_◕ (it prints omg 2 times). Same in omg too.

You probably understand that I'm really beginner. I really didn't understand what is wrong with this code...

Upvotes: 0

Views: 105

Answers (3)

spectacularbob
spectacularbob

Reputation: 3228

Your main problem is that you are always replacing on args.Input, but storing the results in newtext each time, overwriting your previous replacements. Your next problem is that you are outputting the result after each replacement attempt so that's why you are getting multiple weird output results.

I also suggest a dictionary since by definition, it is a mapping of one thing to another. Also, note my changes below, I have moved the Game.Say call outside of the loops and changed "args.Input.Replace" to "newtext.Replace"

Dictionary<string, string> dictionary = new Dictionary<string, string>
{
    {"shrug", @"¯\_(ツ)_/¯" },
    {"omg", "◕_◕" }
};

private static void Game_OnInput(GameInputEventArgs args)
{
    string newtext = args.Input;
    foreach(string key in dictionary.Keys){
        newtext = newtext.Replace(key,dictionary[key]);
    }

    Game.Say(newtext);
}

Upvotes: 0

Steve
Steve

Reputation: 216363

It seems that your two lists have the same length (in terms of elements contained) and each element in one list has its replacement in the same position in the other list.

Then you could treat the two lists as two arrays and use a different way to search for the input term and replace it with the substitution text

private static void Game_OnInput(GameInputEventArgs args)
{
    newtext = args.Input;
    for(int x = 0; x < ascii.Count; x++)
        if (args.Input.Contains(ascii[x]))
        {
           newtext = args.Input.Replace(ascii[x], converted[x]);
           Game.Say(newtext);
        }
}

While i don't think there is a big improvement, you could also implement the same with a dictionary

static Dictionary<string, string> converter = new Dictionary<string, string>()
{
    {"shrug", @"¯\_(ツ)_/¯"},
    {"omg", @"◕_◕"}
};

private static void Game_OnInput(GameInputEventArgs args)
{
    newtext = args.Input;
    foreach(KeyValuePair<string, string> kvp in converter)
        if (args.Input.Contains(kvp.Key))
        {
           newtext = args.Input.Replace(kvp.Key, kvp.Value);
           Game.Say(newtext);
        }
}

Well, probably is a bit more readable, but still we need traversing the dictionary Keys one by one.

Upvotes: 1

async
async

Reputation: 1537

As Daniel pointed out in his comment, this is a good use case for dictionaries.

Have a dictionary that maps the text you want replaced to the stuff you want to be replaced with:

Dictionary<string, string> dict = new Dictionary<string, string>
{
    {"shrug", @"¯\_(ツ)_/¯" },
    {"omg", "◕_◕" }
}; // etc

Then find all occurrences of the keys from the dictionary and replace them with the corresponding values.

Also why are you using static methods and fields? I may be wrong, but I expect most, if not all of your other methods and fields are static as well. I strongly recommend avoiding getting used to them. Try learning more about OOP instead.

Upvotes: 0

Related Questions