包含字符串? [英] Contains for string?

查看:78
本文介绍了包含字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

VB.NET中的字符串变量是否包含CONTAINS?对于

实例,我想做类似以下的事情:


如果strTest包含(A,B,C和)然后

Debug.WriteLine(" Found characters")

Else

Debug.WriteLine("找不到字符!) )

结束如果

解决方案

String.IndexOfAny()你在寻找什么?


汤姆 < to*@nospam.com>在留言中写道

新闻:大江************** @ TK2MSFTNGP11.phx.gbl ...

有没有这样的东西作为VB.NET中字符串变量的CONTAINS?对于

实例,我想做类似以下的事情:


如果strTest包含(A,B,C和)然后

Debug.WriteLine(" Found characters")

Else

Debug.WriteLine("找不到字符!) )

结束如果



Tom,

正如Shiva建议的那样,你可以使用String.IndexOfAny,类似于:

如果strTest.IndexOfAny(" ABC" .ToCharArray)<> -1然后
Debug.WriteLine(" Found characters")
另外
Debug.WriteLine(找不到字符!)
结束如果





如果strTest.IndexOfAny(New Char(){" A" c," B" c," C" c}) <> -1然后


注意:Ac是字面文字,而A是字母。是一个字符串文字。


根据我使用上述内容的频率,我会考虑将

char数组静态化为例程。


静态anyOf()As Char = New Char(){" A" c," B" c," C" c}

如果是strTest。 IndexOfAny(anyOf)<> -1然后


希望这有帮助

Jay


" Tom" < to*@nospam.com>在消息中写道

新闻:大江************** @ TK2MSFTNGP11.phx.gbl ...是否存在字符串变量的CONTAINS之类的东西VB.NET?对于
实例,我想做类似以下的事情:

如果strTest包含(A,B,C)那么
调试。 WriteLine(找到的字符)
另外
Debug.WriteLine(找不到字符!)
结束如果



Shiva:是的,适用于CHARACTER,但不适用于字符串。我可能

应该是我的榜样

如此:


strTest ="猫跳过昏昏欲睡的狗。

如果strTest包含(cat,dog,sleep)那么

Debug.WriteLine(" Found strings")

Else

Debug.WriteLine(找不到字符串!)

结束如果


我可以为此轻松编写我自己的函数,但看起来好像

框架应该在某个地方有类似的字符串函数。


Tom


" Shiva" < SH ****** @ online.excite.com>在消息中写道

news:uj ************** @ TK2MSFTNGP11.phx.gbl ...

是String.IndexOfAny( )你在找什么?

汤姆 < to*@nospam.com>在消息中写道
新闻:大江************** @ TK2MSFTNGP11.phx.gbl ...
是否存在字符串变量的CONTAINS之类的东西VB.NET?对于
实例,我想做类似以下的事情:

如果strTest包含(A,B,C)那么
调试。 WriteLine(找到的字符)
另外
Debug.WriteLine(找不到字符!)
结束如果



Is there such a thing as a CONTAINS for a string variable in VB.NET? For
instance, I want to do something like the following:

If strTest Contains ("A","B", "C") Then
Debug.WriteLine("Found characters")
Else
Debug.WriteLine("Did NOT find characters!")
End If

解决方案

Is String.IndexOfAny() what you are looking for?

"Tom" <to*@nospam.com> wrote in message
news:Oe**************@TK2MSFTNGP11.phx.gbl...
Is there such a thing as a CONTAINS for a string variable in VB.NET? For
instance, I want to do something like the following:

If strTest Contains ("A","B", "C") Then
Debug.WriteLine("Found characters")
Else
Debug.WriteLine("Did NOT find characters!")
End If



Tom,
As Shiva suggests, you can use String.IndexOfAny, something like:

If strTest.IndexOfAny("ABC".ToCharArray) <> -1 Then
Debug.WriteLine("Found characters")
Else
Debug.WriteLine("Did NOT find characters!")
End If
Or

If strTest.IndexOfAny(New Char() {"A"c, "B"c, "C"c}) <> -1 Then

NOTE: "A"c is a char literal, while "A" is a string literal.

Depending on how often I was using the above, I would consider making the
char array Static to the routine.

Static anyOf() As Char = New Char() {"A"c, "B"c, "C"c}
If strTest.IndexOfAny(anyOf) <> -1 Then

Hope this helps
Jay

"Tom" <to*@nospam.com> wrote in message
news:Oe**************@TK2MSFTNGP11.phx.gbl... Is there such a thing as a CONTAINS for a string variable in VB.NET? For
instance, I want to do something like the following:

If strTest Contains ("A","B", "C") Then
Debug.WriteLine("Found characters")
Else
Debug.WriteLine("Did NOT find characters!")
End If



Shiva: Yes, that works for CHARACTERs, but not for strings. I probably
should have made my example
as such:

strTest = "The cat jumped over the sleepy dog."
If strTest Contains ("cat","dog", "sleep") Then
Debug.WriteLine("Found strings")
Else
Debug.WriteLine("Did NOT find strings!")
End If

I -could- write my own function easily enough for this, but it seems like
the framework should have a similar string function somewhere.

Tom

"Shiva" <sh******@online.excite.com> wrote in message
news:uj**************@TK2MSFTNGP11.phx.gbl...

Is String.IndexOfAny() what you are looking for?

"Tom" <to*@nospam.com> wrote in message
news:Oe**************@TK2MSFTNGP11.phx.gbl...
Is there such a thing as a CONTAINS for a string variable in VB.NET? For
instance, I want to do something like the following:

If strTest Contains ("A","B", "C") Then
Debug.WriteLine("Found characters")
Else
Debug.WriteLine("Did NOT find characters!")
End If



这篇关于包含字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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