Rita
Rita

Reputation: 1237

What is wrong with this code? why the List does not identify?

what is wrong with this code?

bool claimExists;
        string currentClaimControlNo = "700209308399870";
        List<string> claimControlNo = new List<string>();
        claimControlNo.Add("700209308399870");

        if (claimControlNo.Contains(currentClaimControlNo.Substring(0, 14)))
            claimExists = true;
        else
            claimExists = false;

Why the claimControlNo above is coming into false? Since I know the value exists, how can i tune the code?

Upvotes: 0

Views: 96

Answers (6)

musefan
musefan

Reputation: 48415

Do you really need this explaining?

You are calling Substring for 14 characters when the string is of length 15. Then you are checking if your list (which only has one item of length 15) contains an item of length 14. It doesn;t event need to check the value, the length is enough to determine it is not a match.

The solution of course is to not do the Substring, it makes not sense.

Which would look like this:

if (claimControlNo.Contains(currentClaimControlNo))
    claimExists = true;
else
    claimExists = false;

Then again, perhaps you know you are trimming the search, and are in fact looking for anything that has a partial match within the list?

If this is the case, then you can simply loop the list and do a Contains on each item. Something like this:

bool claimExists = false;
string searchString = currentClaimControlNo.Substring(0, 14);
foreach(var s in claimControlNo)
{
   if(s.Contains(searchString))
   {
      claimExists = true;
      break;
   }
}

Or use some slightly complex (certainly more complex then I can remember off the top of my head) LINQ query. Quick guess (it's probably right to be fair, I am pretty freaking awesome):

bool claimExists = claimControlNo.Any(x => x.Contains(searchString));

Upvotes: 2

devshorts
devshorts

Reputation: 8872

Because contains on a list looks for the whole item, not a substring:

currentClaimControlNo.Substring(0, 14)
"70020930839987"

Is not the same as

 700209308399870

You're missing a digit, hence why your list search is failing.

I think you are trying to find something in the list that contains that substring. Don't use the lists contain method. If you are trying to find something in the list that has the subset do this

claimExists = claimControlNo.Any(item => item.Contains(currentClaimControlNo.Substring(0, 14)))

This goes through each item in claimControlNo and each item can then check if it contains the substring.

Why do it this way? The Contains method on a string

Returns a value indicating whether the specified System.String object occurs within this string.

Which is what you want.

Contains on a list, however

Determines whether an element is in the System.Collections.Generic.List.

They aren't the same, hence your confusion

Upvotes: 2

Ann L.
Ann L.

Reputation: 13965

It's reporting false because you aren't asking whether the list contains the currentClaimControlNo, you're asking whether it contains a string that is the first fourteen characters of the fifteen-character string currentClaimControlNo.

Try this instead:

 claimExists = claimControlNo.Any(ccn => ccn.StartsWith(currentClaimControlNo.Substring(0,14)));

Upvotes: 4

Gojira
Gojira

Reputation: 3041

Because you're shaving off the last digit in your substring.

if you change the line

if (claimControlNo.Contains(currentClaimControlNo.Substring(0, 14)))

to

if (claimControlNo.Contains(currentClaimControlNo.Substring(0, 15)))

it works.

Upvotes: 2

Matten
Matten

Reputation: 17621

Check it:

// str will be equal to 70020930839987
var str = currentClaimControlNo.Substring(0, 14);

List<string> claimControlNo = new List<string>();
claimControlNo.Add("700209308399870");

The value str isn't contained in the list.

Upvotes: 1

Brandon
Brandon

Reputation: 69953

Your count is wrong. There are 15 characters. Your substring is cutting off the last 0 which fails the condition.

Upvotes: 2

Related Questions