what is correct way to dectect chars in a string?

  • Thread starter Thread starter Rich P
  • Start date Start date
R

Rich P

I was trying this in the KeyUp event of a textbox
(note: I am using "3", "30", "31" as char datatypes)

If "3,30,31".Contains(txt1.text).Equals(true) then
...

The problem is that if I backspace and remove all chars (where txt1.text
= "")

I am seeing that "3,30,31".Contains(txt1.text) still returns true. My
alternative is to say

If txt1.text = "3" Or txt1.Text = "30" or txt1.Text = "31" then ...

I also tried "3,30,31".IndexOf(txt1.text) and also tried
txt1.Text.IndexOf("3,30,31") but was getting inconsistent results.

What is the correct or most effective way to detect these chars?

Thanks

Rich
 
Hi,

And the goal is ? If you want only 3, 30 or 31 to be entered in the textbox
you could use :


Dim Allowed() As String={"3","30","31"}

If Array.Contains(Allowed,TextBox1.Text) Then
' etc...
Else
' Something else
End If

Note that a single string "3,30,31" would never fit. For example it would
pass if the user entered "3,3" or "0,31" and as you have discovered a string
always contains the empty string.
 
Rich said:
I was trying this in the KeyUp event of a textbox
(note: I am using "3", "30", "31" as char datatypes)

If "3,30,31".Contains(txt1.text).Equals(true) then
...

The problem is that if I backspace and remove all chars (where txt1.text
= "")

I am seeing that "3,30,31".Contains(txt1.text) still returns true. My
alternative is to say

If txt1.text = "3" Or txt1.Text = "30" or txt1.Text = "31" then ...

I also tried "3,30,31".IndexOf(txt1.text) and also tried
txt1.Text.IndexOf("3,30,31") but was getting inconsistent results.

What is the correct or most effective way to detect these chars?

I don't understand why "3", "30" and "31" are chars. These are String literals
Only "3"c (note the suffix) is a character literal.

I'm not sure what's your intention. Do you want to check if the textbox's content
is equal to one of these three strings or if it contains one of these Strings?
Or are you looking for the character codes 3, 30, 31? However, I think you
don't because of the code 3.

If you use

"3,30,31".Contains

it also returns True if txt.text is "0" or "1" or a "," or ....
I think that's not what you want. Instead, I'd create a static (Shared) list and
call it's Contains method instead.
 
I was trying this in the KeyUp event of a textbox
(note: I am using "3", "30", "31" as char datatypes)

If "3,30,31".Contains(txt1.text).Equals(true) then
..

The problem is that if I backspace and remove all chars (where txt1.text
= "")

I am seeing that "3,30,31".Contains(txt1.text) still returns true. My
alternative is to say

If txt1.text = "3" Or txt1.Text = "30" or txt1.Text = "31" then ...

I also tried "3,30,31".IndexOf(txt1.text) and also tried
txt1.Text.IndexOf("3,30,31") but was getting inconsistent results.

What is the correct or most effective way to detect these chars?

Thanks

Rich

Yes, string.Contains("") always returns true, as there is a null (or
multiple nulls) between characters. See the "Return Value" section on
this page: http://msdn.microsoft.com/en-us/library/dy85x1sa.aspx

It looks like I would use a list of acceptable values, and then use

if (oklist.Contains(txt1.Text)) then
....
 
Thanks all for the replies and links. It looks like the List (or array)
is the way to go. I am checking if this textbox contains either a "3"
or a "30" or a "31" any of these three values - for triggering an
action.

I just think this would be better than

If txt1.text = "3" or txt1.text = "30" or txt1.text = "31"

Rich
 
Rich said:
Thanks all for the replies and links. It looks like the List (or array)
is the way to go. I am checking if this textbox contains either a "3"
or a "30" or a "31" any of these three values - for triggering an
action.

I just think this would be better than

If txt1.text = "3" or txt1.text = "30" or txt1.text = "31"

Yes, if it's only these three values. You can use "OrElse" instead of "Or".
The Or-operator always calculates both operands, which is not necessary
if the left operand is True.
 
Back
Top