以VB:NET开头并结束 [英] Startswith and endswith VB:NET
本文介绍了以VB:NET开头并结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想检查textbox1是否以特定号码开头,并以特定号码结束。我有一个问题如何写它。
谢谢你的帮助
我尝试了什么:
我试试:
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
如果TextBox1.Text.StartsWith(a)& TextBox1.Text.EndsWith(b)然后
解决方案
1。 Linq解决方案
您可以使用 Enumerable.Any( TSource)方法(IEnumerable(TSource),Func(TSource,Boolean))(System.Linq) [ ^ ]确定序列中的任何元素是否满足条件,但您必须对代码进行一些更改:
Dim a As 字符串()= { 010.551,< span class =code-string> 008.880, 008.881, 009.701,_
009.706, 009.925, 011.149, 011.150, 009.648}
Dim b 作为 字符串 = 1
Dim t 作为 字符串 = 010.551ASDJKA1 ' TextBox1.Text
Dim result = a.Any( Function (x)t.StartsWith(x)和 t.EndsWith(b ))
如果结果然后 ' 它是相等的to result = True
' TextBox1.Text以[a]中的一个字符串开头,在[b]中以1结尾
' 进一步说明
结束 如果
2.非Linq解决方案:
对于 每个 s 作为 字符串 在 a
Dim 结果作为 Boolean = t.StartsWith(s)和 t.EndsWith(b)
如果温泉n>结果然后 ' 它等于result = True
Console.WriteLine( TextBox1.Text以{0}开头并以'{1}',s,b)
退出 对于
结束 如果
下一步跨度>
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
解决方案
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:
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:
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
这篇关于以VB:NET开头并结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文