1

I have a string street that may contains:

street= "Siegfriedst strasse st 16.";

street= "Frontos strasse s .";

I want to remove the extra "st", "strasse" and "s".

I used:

 street= street.Replace("(", "").Replace(")", "").Replace(".", "").
                             Replace("-", "").Replace("strasse","").
                             Replace("st","").Replace("s","");

But I don't want to remove "st" from "Siegfriedst" and "s" from "Frontos".

Kenny Evitt
  • 9,291
  • 5
  • 65
  • 93
Kamran
  • 4,010
  • 14
  • 60
  • 112
  • What if the string is `"st strasse"`? Should then `"strasse"` be removed? – Tim Schmelter May 08 '14 at 14:35
  • @TimSchmelter Yes, it should be removed even the string is "st strasse" – Kamran May 08 '14 at 14:38
  • I'm guessing, tho it's not entirely clear, that you should probably be using regular expressions. See the (closed) question [c# - Regex replace full word](http://stackoverflow.com/questions/21413072/regex-replace-full-word), for which yours is likely a duplicate. – Kenny Evitt May 08 '14 at 14:40
  • possible duplicate of [Replace multiple string elements](http://stackoverflow.com/questions/1321331/replace-multiple-string-elements) – Bob. May 08 '14 at 14:44

6 Answers6

2

Perhaps this is what you want, it's not clear if you only want to remove duplicate words or sub-strings:

public static string RemoveDuplicates(string input, params string[] wordsToCheck)
{
    var wordSet = new HashSet<string>(wordsToCheck);
    int taken = 0;
    var newWords = input.Split()
        .Select(w => !wordSet.Contains(w) || ++taken == 1 ? w : "");
    return string.Join(" ", newWords);
}

Usage:

string text = RemoveDuplicates("Siegfriedst strasse st 16.", "st", "strasse", "s");

Result: Siegfriedst strasse 16.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • Thanks a lot Once again!! – Kamran May 08 '14 at 15:21
  • I don't think this works as expected, if removing duplicates is your aim. The variable "taken" needs to be keyed by the word ... i.e. an input of "strasse Siegfriedst strasse st 16. st" will yield "strasse Siegfriedst 16. ", not the expected "strasse Siegfriedst st 16." (or wordSet can be made a Dictionary). – Michael May 08 '14 at 15:24
  • @mick: of course this is a simple approach, but OP wasnt very specific in his requirement or edge cases. – Tim Schmelter May 08 '14 at 16:03
0
street = street.Replace(".", " ") //To better enable pattern matching
               .Replace(" strasse ", "")
               .Replace(" st ", " ")
               .Replace(" s ", "")
               .Replace("  ", " ")
               .Trim();  //Trim() removes the leading and trailing whitespaces
La-comadreja
  • 5,627
  • 11
  • 36
  • 64
0
street = street.Replace("(", "")
               .Replace(")", "")
               .Replace(".", "")
               .Replace("-", "")
               .Replace(" strasse "," ")
               .Replace(" st "," ")
               .Replace(" s "," ");
decPL
  • 5,384
  • 1
  • 26
  • 36
0

You can write a helper method like:

public static string Cleanup(string text, string[] exclude)
{
    string[] parts = text.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

    List<string> words = new List<string>();

    foreach(string part in parts)
    {
        if (!exclude.Contains(part))
        {
            words.Add(part);
        }
    }

    return string.Join(" ", words.ToArray());
}

and then use it like:

string street = Cleanup("Siegfriedst strasse st 16.", new string[] { "strasse", "st", "s", " " });
HABJAN
  • 9,212
  • 3
  • 35
  • 59
0

You can use regular expressions. This will match whole words "strasse" "st" or "s", but not in parts of words:

using System.Text.RegularExpressions;

Regex rgx = new Regex(@"\b(strasse|st|s)\b|\(|\)|\.");
street = rgx.Replace(street, "");
Danny
  • 1,740
  • 3
  • 22
  • 32
0

Based on the fact that you used concatenated Replace operations (thus not wanting any of the extra strings to appear) I would suggest the following LINQ query:

street = street.Split(' ').Where(s => s != "strasse" && s != "st" && s != "s").Aggregate((x, y) => x + " " + y);
EKrueger
  • 730
  • 8
  • 20