-1

I use this code to check if string starts with 34CDB:

string word = "34CDBXXXX";

if (word.StartsWith("34CDB")) 
{
   // do something 
}

I want many values, not just 34CDB. I try to change it to

if (word.StartsWith("34CDB", "AT4X3", "3AXP3")) 

But doesn't work.

Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
user2651946
  • 21
  • 1
  • 8
  • 2
    Question is extremely unclear. What is `stringArray` and how you are going to use it? What have you tried? How do you know that **nothing works**? Errors? – Sergey Berezovskiy May 12 '14 at 11:26
  • 1
    You tried lots of code, but nothing works. Could you post some of that code? – scheien May 12 '14 at 11:26
  • @user2651946 `columnChars` is an array of `char`. Is `34CDB` valid value for `char`? – Sergey Berezovskiy May 12 '14 at 11:30
  • 2
    `new char[] { '34CDB', 'E808' }` won't compile and `E808` are 4 characters, not 5. Please show some valid and invalid input/output pairs. – CodeCaster May 12 '14 at 11:30
  • `if (txtBox.Text.Length >= 5){var txt = txtBox.Text.Substring(0, 5); }` and then match against the per-defined values!!! – huMpty duMpty May 12 '14 at 11:32
  • 1
    Your code won't even compile, make it an array of `string`. Then add following method: `public bool StartsWithColumn(string toCheck, string[] columnChars) { return columnChars.Any(x => toCheck.StartsWith(x)); }`. – Abbas May 12 '14 at 11:37
  • See [To check if a string contains an element from a list (of strings) - Is there a better way to write this code?](http://stackoverflow.com/questions/500925/to-check-if-a-string-contains-an-element-from-a-list-of-strings-is-there-a-b) and replace `Contains` with `StartsWith`. – CodeCaster May 12 '14 at 11:47
  • 1
    @user2651946 now your question is clear, but its closed and should be reopened for answering. Next time please please provide better context and description of your problem, and it will be answered quickly without being closed – Sergey Berezovskiy May 12 '14 at 12:05

1 Answers1

1

Perhaps you want to check if the user entered one of your strings:

bool enteredAny = stringArray.Contains(textBox1.Text.Trim());
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939