0

I have this code:

string[] pa = new string[] { "Maia", "Porto", "Valongo", "Gondomar", "Gaia", "Matosinhos"};

private string[] concelho;

public string[] Concelho
{
     get { return concelho; }
     set { concelho = value; }
 }

I need to validate the string pa to see if user put one of that values on text box. The validation needs to be case sensitive, can help??

1
  • You can call the Contains method of the array. Commented Mar 6, 2017 at 1:24

3 Answers 3

2

I need to validate the string pa to see if user put one of that values on text box.

Simply do this:

if(pa.Contains(TextBoxData.Text.ToString())){
// Do something.
}
Sign up to request clarification or add additional context in comments.

5 Comments

@un-lucky it is case sesitive
yes your answer is fine but you need to change toString() to ToString().
@CodingYoshi true, i get mixed up between JAVA and C# every now and then.
.Text will give you a string then what is the significance for .ToString() there?
@os.: not much in this line but will not be a good practice. Since .Text gives you a string object you can directly apply them in contains. I don't think .ToString() will do anything special here
1

This might do the trick for you

string[] pa = new string[] { "Maia", "Porto", "Valongo", "Gondomar", "Gaia", "Matosinhos" };
string inpstr = "Maia";
if(pa.Contains(inpstr))
{
    //Your Textbox or Input String is one of the in the List / Array.
}

You can use your Textbox.Text instead of inpstr and yes it is case sensitive.

4 Comments

@un-lucky Yes it is case sensitive. :)
@un-lucky Contains() is case sensitive.
@un-lucky it is case sentive
i can put Textbox.Text because im progamming in a class
1

You can just use Linq and call Any method and pass the user entered value to it. It will return true if the value is in the array, or it will return false.

var isValid = pa.Any(x => x == "uservalue");

Make sure you import the Linq namespace by using System.Linq;

1 Comment

@un-lucky what is your point, that will do a case sensitive comparison.

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.