WorksOnMyLocal
WorksOnMyLocal

Reputation: 1689

check for a substring(of a string) in the dictionary and return the key's(substring) value

I have a dictionary like below,

PropStreetSuffixDict.Add("ROAD", "RD");    
PropStreetSuffixDict.Add("STREET","ST"); and many more.

Now my requirement says when a string contains a substring of either ROAD or STREET i want to return the related value for that substring.

For example..CHURCH ACROSS ROAD should return RD

This is what i tried, which only works if the input string is exactly same as key of the dict.

private string GetSuffix(string input)
{
   string suffix=string.Empty;
   suffix = PropStreetSuffixDict.Where(x => x.Key.ToUpper().Trim() == 
   input.ToUpper().Trim()).FirstOrDefault().Value;
   return suffix;
}

Note: In case a string contains more than one of such substrings, then it should return the value of the first occurence of the any of the substrings. i.e. if STREET CHURCH ACROSS ROAD is the input, it should return ST not RD

Upvotes: 1

Views: 409

Answers (3)

Bradley Uffner
Bradley Uffner

Reputation: 16991

I would recommend using using RegEx to split apart your words, that way you can efficiently split on multiple characters, not just spaces, if required. This solution also allows replacing the individual words very easily, without having to deal with tracking the position and length of the matched word, vs the length of the replacement value.

You could use a function like this:

public string ReplaceWords(string input, Dictionary<string,string> dictionary)
{
    var result = Regex.Replace(input, @"\w*", (match) =>
    {
        if (dictionary.TryGetValue(match.Value, out var replacement))
        {
            return replacement;
        }
        return match.Value;
    });
    return result;
}

It will take an input string, split it up, and replace the individual words with those in the supplied dictionary. The particular RegEx of \w* will match any continuous run of "word" characters, so it will break on spaces, commas, dashes, and anything else that isn't part of a "word".

This code does use some newer C# language features that you may not have access too (inline out parameters). Just let me know if you can't use those and I'll update it to work without them.

You can use it like this:

Console.WriteLine(ReplaceWords("CHURCH ACROSS ROAD", PropStreetSuffixDict));
Console.WriteLine(ReplaceWords("CHURCH ACROSS STREET", PropStreetSuffixDict));
Console.WriteLine(ReplaceWords("CHURCH ACROSS ROAD, LEFT AT THE OTHER STREET", PropStreetSuffixDict));

For the following results:

CHURCH ACROSS RD
CHURCH ACROSS ST
CHURCH ACROSS RD, LEFT AT THE OTHER ST

Upvotes: 0

Viplock
Viplock

Reputation: 3319

You can try something like this

private string GetSuffix(string input)
{
   string suffix=string.Empty;
   string[] test =input.ToUpper().Split(' ');
   suffix =(from dic in PropStreetSuffixDict 
           join inp in test on dic.Key equals inp
           select dic.Value).LastOrDefault();
  return suffix;     
}

Split the input and then use linq

Upvotes: 1

Justinas Marozas
Justinas Marozas

Reputation: 2664

If you want it to return first occurrence in the input string (GetSuffix("CHURCH STREET ACROSS ROAD) ==> "STREET") it becomes a little tricky.

Code below will find where in the input string all keys occur, and return value of first found position.

private string GetSuffix(string input)
{
    var suffix = PropStreetSuffixDict
        .Select(kvp => new
        {
            Position = input.IndexOf(kvp.Key.Trim(), StringComparison.CurrentCultureIgnoreCase),
            Value = kvp.Value
        })
        .OrderBy(x => x.Position)
        .FirstOrDefault(x => x.Position > -1)?.Value;

    return suffix ?? string.Empty;
}

If you didn't care about the order of occurrence in input string you could simplify it to this:

private string GetSuffix(string input)
{
    var suffix = PropStreetSuffixDict.FirstOrDefault(kvp => input.Containts(kvp.Key.Trim(), StringComparison.CurrentCultureIgnoreCase))?.Value;
    return suffix ?? string.Empty;
}

Upvotes: 0

Related Questions