0

I needed to test a string to see whether it ends with any of an array of strings.

I found the perfect solution using LINQ by following this answer:

string test = "foo+";
string[] operators = { "+", "-", "*", "/" };
bool result = operators.Any(x => test.EndsWith(x));

Now I want to get the string that matched and that's where I'm currently stuck with.


I tried adding at the end

text_field.Text = x;

and that errored out with a message about scope - and rightfully so, I was expecting that error. I also tried to declare a string variable named x at the very top and another error came out of it - something about not being able to re-declare the variable in a different scope. I guess I'm used to PHP so much where you can re-declare a variable with no issues.

dokgu
  • 4,957
  • 3
  • 39
  • 77
  • 5
    `Any()` returns a bool, if you actually want something maybe try `FirstOrDefault()` instead. Keep in mind though that you can have multiple items match, what do you intend to do with the rest? Do you care? Should the first match arbitrarily be returned? – maccettura May 01 '18 at 19:47
  • are you trying to get the + at the end of the entire string? – Simon Price May 01 '18 at 19:51
  • @SimonPrice yes but only if it matches one of the `operators` array. – dokgu May 01 '18 at 19:52
  • The regex is a correct answer, but then so is mine. It comes down to which is your preference – Simon Price May 01 '18 at 19:53
  • @maccettura In my case I only expect to get 1 item to match or none at all. There shouldn't be any instances where it would match 2 or more of the `operators`. – dokgu May 01 '18 at 20:08

3 Answers3

2

I would use Regex for this

string test = "foo+";
var match = Regex.Match(test, @".+([\+\-\*\\])$").Groups[1].Value;

match will be "" if the string doesn't end with +-*/

Eser
  • 12,346
  • 1
  • 22
  • 32
  • Why introduce regex for such a simple matter? – gilliduck May 01 '18 at 19:50
  • 2
    @gilliduck which rule of SO did I miss? – Eser May 01 '18 at 19:51
  • The one where you don't complicate things with difficult to read/understand answers. Regex is way too involved for such a simple concept that OP is asking about. – gilliduck May 01 '18 at 19:52
  • 1
    @gilliduck `Regex is way too involved` maybe just for you? – Eser May 01 '18 at 19:53
  • ok, lets take too involved out of the mix. Regex is a different answer than the one the OP requested. He asked for how to do this in LINQ. You didn't answer that. – gilliduck May 01 '18 at 19:54
  • 1
    @gilliduck SO We have to answer just what OP asks and doesn't post other alternatives that may work? What if ,for ex, post is an XY problem (I don't mean this case)? – Eser May 01 '18 at 19:55
  • that's all well and good if there is a good indication of that, but in the OP's question there is no indication that anything other than a LINQ method is better. The question was about LINQ, the answer should be about LINQ unless the answer isn't doable or is more complicated than can be done via another method. – gilliduck May 01 '18 at 19:58
  • 1
    I'm okay with regex. I just don't know how to write them so in this case I'll prefer the LINQ answers. In case anyone hadn't noticed I used the exact same code in the answer I linked to. To modify the regex using the actual strings I'm supposed to test is a little bit of work for me. Anyways - upvoted since it seems to work https://regex101.com/r/5aT2eH/1 – dokgu May 01 '18 at 20:01
0

Your best bet is to do a FirstOrDefault and then check if that is null/empty/etc as if it were your bool. Though this is a very basic example it should get the point across. What you do with that result and if it should just be one or more, etc is up to your circumstances.

    static void Main()
    {
        string test = "foo+";
        string[] operators = { "+", "-", "*", "/" };
        bool result = operators.Any(x => test.EndsWith(x));

        string actualResult = operators.FirstOrDefault(x => test.EndsWith(x));

        if (result)
        {
            Console.WriteLine("Yay!");
        }

        if (!string.IsNullOrWhiteSpace(actualResult))
        {
            Console.WriteLine("Also Yay!");
        }
    }
gilliduck
  • 2,762
  • 2
  • 16
  • 32
0

If i understand right, this will get you the operator

string test = "foo+";
string[] operators = { "+", "-", "*", "/" };
var result = operators.Where(x => test.EndsWith(x)) ;

This will only return the last used operator so if it ends in -+* it will give you the last character in the string

Simon Price
  • 3,011
  • 3
  • 34
  • 98
  • I would be grateful if you could explain the mark down, this is a correct answer and gets the value the OP is after – Simon Price May 01 '18 at 19:55
  • 1
    Probably just Eser being negative of all answers other than their own amazing Regex solution. Heaven forbid you answer within the context of the question. – gilliduck May 01 '18 at 20:00
  • This is not a correct answer though. `result` will not be the single operator that matches – maccettura May 01 '18 at 20:01
  • It is a viable answer if the OP understands that there may be one or more possible hits on their .Any() and want to know any or all of them. My answer just gives the first that matches. – gilliduck May 01 '18 at 20:02
  • Thanks for clarifying that this returns multiple hits. In my case there will only be 1 or 0 hits since a string can only end in 1 character. Upvoted though. – dokgu May 01 '18 at 20:05
  • @SimonPrice I am only pointing out that your answer does not give the matching _operator_, it gives the matching _operators_. It might be worth pointing that out to the OP instead of giving a different response than they have asked for. Not saying your approach is bad, but its not what the OP asked for so you should explain why this is better. – maccettura May 01 '18 at 20:06