Find all ocurrences of substrings with [tokens] in string and place them in int array

João Gomes :

I have a working code for replacing a single substring within [] in a string. This worked for a "[1]" in a string:

private string returnWithLinks(string adminString, int adminId)
    {
        var returnString = "Nothing at all";

        int pFrom = adminString.IndexOf("[") + "[".Length;
        int pTo = adminString.LastIndexOf("]");

        var getSourceId = adminString.Substring(pFrom, pTo - pFrom);

        int i = 0;
        bool tryParse = int.TryParse(getSourceId, out i);
        if(!tryParse) {returnString = "this is not an int!";} else {
            if(i == 1) {
                returnString = adminString.Replace("[1]", "<a>something</a>" + var1fromsomwhere + i);
            } else if (i == 2) {
                returnString = adminString.Replace("[2]", "<a>other something</a>" + var2fromsomwhere + i);
            } else if (i == 3) {
                returnString = adminString.Replace("[3]", "<a>other something</a>" + var3fromsomwhere + i);
            } else if (i == 4) {
                returnString = adminString.Replace("[4]", "<a>other something</a>" + var4fromsomwhere + i);
            } else if (i == 5) {
                returnString = adminString.Replace("[5]", "<a>other something</a>" + var5fromsomwhere + i);
            }
        }
        return returnString;
    }

However I now need to be able to replace all occurences of the tokens such as:

adminString = "[1][2]somthingelse[2]another[4]aa";

I was think about placing all the ints inside token on an int array, then looping each int with the if clauses, but creating the array is completly out of my leage.

How can I achieve this?

After having the array I will loop through it with the if clauses.

Jonathan Alfaro :

You do not need an array or even a loop.

There are many ways to do this... For example using a dictionary.

But in your case there is no need to complicate things.

A single line of code will replace everything like this:

    private string returnWithLinks(string adminString, int adminId)
    {
            var returnString = adminString
               .Replace("[1]", "<a>something</a>" + var1fromsomwhere + 1)
               .Replace("[2]", "<a>something</a>" + var2fromsomwhere + 2)
               .Replace("[3]", "<a>something</a>" + var3fromsomwhere + 3)
               .Replace("[4]", "<a>something</a>" + var4fromsomwhere + 4)
               .Replace("[5]", "<a>something</a>" + var5fromsomwhere + 5);

            return returnString;
    }

Doing with a dictionary and a loop:

    private static readonly Dictionary<string, string> _replacements = new 
    Dictionary<string, string>
    {
        { "[1]", "<a>something</a>" + var1fromsomwhere + 1 },
        { "[2]", "<a>something</a>" + var2fromsomwhere + 2 },
        { "[3]", "<a>something</a>" + var3fromsomwhere + 3 },
        { "[4]", "<a>something</a>" + var4fromsomwhere + 4 },
        { "[5]", "<a>something</a>" + var5fromsomwhere + 5 },            
    };
    private string returnWithLinks(string adminString, int adminId)
    {
            var keys = _replacements.Keys.ToArray();
            var returnString = adminString;

            for(var i = 0; i < keys.Length; i++)
            {
                var key = keys[0];

                returnString = returnString.Replace(key, _replacements[key]);
            }

            return returnString;
    }

As you can see dictionary seems cleaner but in terms of maintenance and number of lines of code is the exact same thing.

You will still need to add a line to the dictionary for every replacement.

The only reason i would do it in a loop with a dictionary would be if the dictionary is NOT hardcoded.

Meaning if the dictionary was filled with values from a database for example.

Another downside of the loop is the amount of string allocations one extra allocation per iteration.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=405106&siteId=1