0

I want to define the character that is written. I think it would be easier to explain by giving an example.

sample;

      void Main(){
    List<account> account = new List<UserQuery.account>{
    new account() { accountCode = "100", accountName = "That is the house which I want. " },
    new account() { accountCode = "101", accountName = "I bought whatever you wanted" },
    new account() { accountCode = "102", accountName = "The flowers which you brought me were very beautiful."} 
    };

    string search = "*house*";
    string search2 = "The flowers*";
    string search3 = "*wanted.";

    var result = account.FirstOrDefault(a => a.accountName.Contains(search)).accountCode;
    var result2 = account.FirstOrDefault(a => a.accountName.StartsWith(search2)).accountCode;
    var result3 = account.FirstOrDefault(a => a.accountName.EndsWith(search3)).accountCode;
}

public class account
{
    public string accountCode { get; set; }
    public string accountName { get; set; }
}



> result=100 
result2=102 
result3=101

This is how I search for star characters.

KdrGny
  • 23
  • 7
  • 4
    What do you want to define exactly? – Izuka Aug 30 '17 at 12:00
  • 4
    I don't understand your question either – Pikoh Aug 30 '17 at 12:01
  • So you want to use wildcards to search instead of `StartsWith`, `EndsWith` and `Contains`? – Silvermind Aug 30 '17 at 12:02
  • 2
    You may be looking for [regular expressions](https://learn.microsoft.com/dotnet/standard/base-types/regular-expressions) to do wildcard searches. – Jeroen Mostert Aug 30 '17 at 12:02
  • You're looking for *wildcard* (`?` and `*`), please, have a look at https://stackoverflow.com/questions/30299671/matching-strings-with-wildcard/30300521#30300521 – Dmitry Bychenko Aug 30 '17 at 12:11
  • I'm guesing your question was something along how to map "*house*" so that you could do a "Contains" search with the word "house". If the ultimate goal is finding it, Dmitry answer should work. Whereas strictly answering your question, you could check if the search string of "*house*" StartsWith and EndsWith a '*' and then remove the '*' by either using a replace '*' into '' OR using substring, which is very winded and not recommended. – hsoesanto Aug 30 '17 at 12:43

1 Answers1

1

You are looking for wildcards which you can turn into corresponding *regular expressions, see

Matching strings with wildcard

E.g.

  private static String WildCardToRegular(String value) {
    return "^" + Regex.Escape(value).Replace("\\?", ".").Replace("\\*", ".*") + "$"; 
  }

Then operate with regular expression as usual:

  using System.Text.RegularExpressions;

  ... 

  var result = account
    .FirstOrDefault(a => Regex.IsMatch(a.accountName, WildCardToRegular(search)))
    .accountCode; //TODO: what if the outcome of FirstOrDefault is null?

  var result2 = account
    .FirstOrDefault(a => Regex.IsMatch(a.accountName, WildCardToRegular(search2)))
    .accountCode;

  var result3 = account
    .FirstOrDefault(a => Regex.IsMatch(a.accountName, WildCardToRegular(search3)))
    .accountCode;

Please, notice that there's no need in StartsWith, Contains, EndsWith when working with wild cards

Edit: if you want a value matched all you need is to modify the query:

var accountFound = account
  .Select(a => a.accountName) 
  .SelectMany(acc => Regex
     .Matches(acc, WildCardToRegular(search))
     .OfType<Match>()
     .Where(match => match.Success)
     .Select(match => match.Value))
  .FirstOrDefault(); 
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • If there was a list on the two sides, how can a formula be applied steep. Ie if we wanted to retrieve the value from a list. – KdrGny Aug 30 '17 at 13:47
  • @KdrGny: If you want to retrieve the *matched value* put `Regex.Matches` instead of `Regex.IsMatch` – Dmitry Bychenko Aug 30 '17 at 13:57