VB如何在文本框中找到多个单词? [英] vb how to find more than one word in text box?

查看:115
本文介绍了VB如何在文本框中找到多个单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码在文本框中搜索一个单词,但是如何搜索两个单词?

I have the following code which searches for one word in a text box, but how do I search for two words?

If InStr(SourceC.Text, "word1") Then
	form2.Show()
	Me.Hide()
Else
	form1.Show()
	Me.Hide()
End If



我已经尝试了以下方法,但这不起作用.



I have tried the following, but that doesn''t work.

If InStr(SourceC.Text, "word1, word2") Then
	form2.Show()
	Me.Hide()
Else
	form1.Show()
	Me.Hide()
End If



非常感谢您的帮助.



Any help is much appreciated.

推荐答案

这是简单的方法:
This is the simple way:
If InStr(SourceC.Text, "word1") Or InStr(SourceC.Text, "word2") Then
	form2.Show()
	Me.Hide()
Else
	form1.Show()
	Me.Hide()
End If


如果您使用的是VB.Net,则还可以使用正则表达式:


If you''re using VB.Net, you can also make use of regular expressions:

If System.Text.RegularExpressions.Regex.IsMatch(SourceC.Text, "word1|word2") Then
	form2.Show()
	Me.Hide()
Else
	form1.Show()
	Me.Hide()
End If


当您对正则表达式有了更多的了解时,也可以将其简化一些:


When you get a little more familiar with regular expressions, you can also make that a bit shorter:

If System.Text.RegularExpressions.Regex.IsMatch(SourceC.Text, "word[12]") Then
	form2.Show()
	Me.Hide()
Else
	form1.Show()
	Me.Hide()
End If


尽管我猜测您的需求不是那么具体.另外,某些代码可能必须更改,具体取决于您是否要确保两个单词同时出现以及是否有重叠计数(例如,如果单词"word12"将同时计入"word1"和"word12" ",因为前者是后者的子集).另外,您可以使用包含"而不是"InStr".


Though I''m guessing your needs are not quite that specific. Also, some of that code might have to change depending on if you want to make sure BOTH the words appear at the same time, and if overlaps count (e.g., if the word "word12" will count towards both "word1" and "word12", because the former is a subset of the latter). Also, you could use "Contains" rather than "InStr".


这篇关于VB如何在文本框中找到多个单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆