0

I am trying to select an element in array, that matches something in a string.

For example:

string[] splitChar = new string[] { "+=", "-=" };
string content = "5+=2";

So i want to check if content, contains something from splitChar, and if it does then select the value so it can be assigned to a string variable.

2
  • What if it finds more than one element? Or isn´t this possible? 5+=2-=1? Commented May 17, 2017 at 6:26
  • In my case it wouldn't be possible, thank you for pointing that out though. Commented May 17, 2017 at 7:09

2 Answers 2

1

Lambdas and LINQ would be overkill for this. You can just do a simple foreach:

string match = "";
foreach (var str in splitChar)
{
    if (content.Contains(str)) 
    {
        match = str;
        break;
    }
}

if (!String.IsNullOrEmpty(match))
{
    // Do whatever with `match`
}

If you really wanted to use LINQ, though, FirstOrDefault would be your best bet:

string match = splitChar.FirstOrDefault(s => content.Contains(s));

if (!String.IsNullOrEmpty(match))
{
    // Do whatever with `match`
}
Sign up to request clarification or add additional context in comments.

17 Comments

Thank you for both options, worked just as i needed. Yeah i know that a foreach would of been better, but for some reason the person asking for this project, wants lambda to be in place. Will mark as solved, as soon as i can
Why would it be overkill? I think the LINQ version expresses the intent much clearer than the foreach loop...
@Timitry I said it's overkill, not cleaner. It depends on what your priorities are. Personally, I don't like throwing around LINQ methods just because I can when a simple for or foreach loop will do the same job just as effectively. This is production code, not code golf. If intent needs to be expressed, add comments.
@Abion47 Alright, I got what you mean! Although I don't think that LINQ has anything to do with code golf. I do a lot of functional-style programming in C# and always favor LINQ to loops - in production code. But since this is a matter of taste, we don't need to discuss that :-)
@Abion47 I don't like throwing around LINQ methods just because I can when a simple for or foreach loop will do the same job just as effectively. In my opinion, LINQ is the easier way to implement simple iterations. It cuts down on nesting and unnecessarily long foreach statements for e.g. a simple filtering action, and it increases readability (once you understand the basics behind lambdas). LINQ really shines when you can process every item of a list separately and the outcome of processing one row does not influence another row's outcome.
|
1

Have you tried checking with FirstOrDefault like the following?

string[] splitChar = new string[] { "+=", "-=" };
string content = "5+=2";        
var stringPresent = splitChar.FirstOrDefault(x=>content.Contains(x));
if(String.IsNullOrEmpty(stringPresent))
    Console.WriteLine("Not found");
else
Console.WriteLine(stringPresent);

Check this Example

4 Comments

Should of mentioned that i'd like to assign the value it finds, to a string variable, not check if its present
@SimonIsaksen: Could you please take a look into the updates as well as the example and let me know is that what you are looking
Yeah, this does the work just aswell, thank you for your solution :)
@SimonIsaksen: Glad to hear that.. Happy coding

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.