2

I have this piece of code:

description="EUR CMS Swap";
description_token=description.Split(" ".ToCharArray());
bool check;

if(description_token.Contains("EUR CMS"))
   check=true;

But check is always false, even though

description_token[0]="EUR"  
description_token[1]="CMS"

What am I doing wrong? Is there an alternative method?

0

4 Answers 4

5

The source value you have gets split into the array by spaces, so you should have three objects in your array

(EUR, CMS and Swap)

So you should rewrite your check to be

if(description_token.Contains("EUR") || description_token.Contains("CMS"))

(if it is enough if just one of the values is inside that array)

or

if(description_token.Contains("EUR") && description_token.Contains("CMS"))

(if both values HAVE to be inside that array)

Sign up to request clarification or add additional context in comments.

Comments

3

You either have to look for substrings or you have to compare all items:

  1. substring:

    bool check = description.Contains("EUR CMS");
    
  2. LINQ with Enumerable.All:

    string[] tokens = "EUR CMS".Split();
    bool check = tokens.All(t => description_token.Contains(t));
    
  3. LINQ with Enumerable.Any (at least one match)

    bool check = tokens.Any(t => description_token.Contains(t));
    

The first approach checks if the given string is a substring of a larger, the second approach checks if all tokens are contained in the larger collection.

Comments

2

You can't use Contains in that way. No single element contains "EUR CMS" after it's been split. You can either perform the check prior to splitting the string (using description.Contains("EUR CMS") or after the split by checking for EUR and CMS separately.

Comments

1
if(description_token.Contains("EUR") && description_token.Contains("CMS"))
 check=true;

Comments

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.