Click here to Skip to main content
15,908,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I would like to check if textbox1 start with specific number and while end with specific number. I have a problem how to write it.
Thank you for help

What I have tried:

I try:

Dim a As String = "010.551" Or "008.880" Or "008.881" Or "009.701" Or "009.706" Or "009.925" Or "011.149" Or "011.150" Or "009.648"
            
Dim b As String = "1"


 If TextBox1.Text.StartsWith(a) & TextBox1.Text.EndsWith(b) Then
Posted
Updated 3-May-18 22:05pm
Comments
Richard MacCutchan 4-May-18 3:59am    
Use your debugger to look at the above code and see exactly what gets stored in variable a.
Member 13711215 4-May-18 4:12am    
ERROR: Conversion string to long is not valid
Member 13711215 4-May-18 4:16am    
I edit it:
regcis is textbox1.text
and I replace & to andalso

Dim a As String = "010.551" Or "008.880" Or "008.881" Or "009.701" Or "009.706" Or "009.925" Or "011.149" Or "011.150" Or "009.648"
Dim b As String = "5"
If regCis.StartsWith(a) AndAlso regCis.EndsWith(b) Then

1 solution

1. Linq solution
You can use Enumerable.Any(TSource) Method (IEnumerable(TSource), Func(TSource, Boolean)) (System.Linq)[^] to determine whether any element of a sequence satisfies a condition, but you have to make some changes in your code:
VB.NET
Dim a As String() = {"010.551", "008.880", "008.881", "009.701", _
		"009.706", "009.925", "011.149", "011.150", "009.648"}
Dim b As String = "1"

Dim t As String = "010.551ASDJKA1" 'TextBox1.Text

Dim result = a.Any(Function(x) t.StartsWith(x) And t.EndsWith(b))
If result Then 'it is equal to result=True
    'TextBox1.Text starts with one of string in [a] and ends with "1" in [b]
    'further instructions
End If


2. Non-Linq solution:
VB.NET
For Each s As String In a
	Dim result As Boolean = t.StartsWith(s) And t.EndsWith(b)
	If result Then 'it is equal to result=True
	    Console.WriteLine("TextBox1.Text starts with '{0}' and ends with '{1}'", s, b)
		Exit For
	End If
Next
 
Share this answer
 
v3
Comments
Member 13711215 4-May-18 5:10am    
Thank you ;)
Maciej Los 4-May-18 5:29am    
You're very welcome.
If my answer was helpful, please accept it as a solution (green button) - formally to remove your question from unanswered list.
Ralf Meier 4-May-18 6:08am    
Very good ... but I think too much for the Enquirer ... but +5 from me ;-)
Maciej Los 4-May-18 7:34am    
Thank you, Ralf.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900