Home > Blockchain >  How to retrieve a string element from a list ignoring string spaces in c#?
How to retrieve a string element from a list ignoring string spaces in c#?

Time:12-17

simple question.

I have got a string variable called "searchTerm" and a list of strings. How could I retrieve a certain element matching my searchTerm variable ignoring whitespaces. An example of what I've been trying to do with no success:

string searchTerm = "The Lord Of The Rings"
List<string> films = new List<string>(){ "Harry Potter", "Avangers", "The Lord  Of The Rings", "Back to the future"};
string film = films.where(film => film.Contains(searchTerm, StringComparison.OrdinalIgnoreCase));

This code is not working. Please understand thatin the films list "The Lord Of The Rings" string has got two whitespaces after Lord word and searchTerm just has got one space.

I've tried the following with no success:

string film = films.where(film => film.Replace(" ","").Contains(searchTerm.Replace(" ",""), StringComparison.OrdinalIgnoreCase));

Please could you help me to find a solution? thanks

}

CodePudding user response:

I would use a "normalize"-method that removes consecutive spaces and replace punctuation characters with spaces as well:

static string NormalizeWhiteSpacesAndPunctuations(string text)
{
    if (string.IsNullOrWhiteSpace(text))
        return string.Empty;

    text = text.Trim();
    StringBuilder sb = new StringBuilder(text.Length);
    bool lastCharWasSpace = false;
    foreach(char c in text)
    {
        if (char.IsWhiteSpace(c) || char.IsPunctuation(c))
        {
            // prevent consecutive spaces, only one should remain
            if (!lastCharWasSpace)
            {
                sb.Append(' ');
                lastCharWasSpace = true;
            }
        }
        else
        {
            sb.Append(c);
            lastCharWasSpace = false;
        }
    }

    return sb.ToString().Trim();
}

Now this works:

string searchTerm = "The Lord Of The Rings";
searchTerm = NormalizeWhiteSpacesAndPunctuations(searchTerm); // no-op in this case, but you should do it if searchTerm is an input
List<string> films = new List<string>() { "Harry Potter", "Avangers", "The Lord  Of The Rings", "Back to the future" };
string film = films
    .FirstOrDefault(film => NormalizeWhiteSpacesAndPunctuations(film).IndexOf(searchTerm, StringComparison.OrdinalIgnoreCase) >= 0);

.NET Fiddle: https://dotnetfiddle.net/d8JM75

CodePudding user response:

Why not just use a simple Regular Expression Replacement and string comparison? DotNet Fiddle

using System;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Linq;                  

string searchterm = "The Lord Of The Rings";
List<string> films = new List<string>(){ "Harry Potter", "Avangers", "The Lord  Of The Rings", "Back to the future"};
string film = films
.FirstOrDefault(f => string.Equals(Regex.Replace(f, "\\s", ""), Regex.Replace(searchterm, "\\s", ""), StringComparison.CurrentCultureIgnoreCase));
  • Related