在字符串中搜索多个内容 [英] Search for multiple things in a string
问题描述
你能在另一个字符串中搜索多一个字符串吗?
类似于:
someString.IndexOf(" something1"," something2",some thing3,0)
或者你需要做类似的事情:
if ((someString.IndexOf(" something1",0)> = 0)||
((someString.IndexOf(" something2",0)> = 0)||
((someString.IndexOf(" something3",0)> = 0))
{
做点什么
}
谢谢,
Tom
Can you do a search for more that one string in another string?
Something like:
someString.IndexOf("something1","something2","some thing3",0)
or would you have to do something like:
if ((someString.IndexOf("something1",0) >= 0) ||
((someString.IndexOf("something2",0) >= 0) ||
((someString.IndexOf("something3",0) >= 0))
{
Do something
}
Thanks,
Tom
推荐答案
Tom,
您最好的选择是使用正则表达式。您可以使用System.Text.RegularExpressions命名空间中的
类来执行此操作。
希望这会有所帮助。
-
- Nicholas Paldino [.NET / C#MVP]
- mv*@spam.guard.caspershouse.com
" tshad" < TS ********** @ ftsolutions.com>在留言中写道
新闻:OO ************** @ TK2MSFTNGP09.phx.gbl ...
Tom,
Your best bet would be to use a regular expression. You can use the
classes in the System.Text.RegularExpressions namespace to do this.
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"tshad" <ts**********@ftsolutions.com> wrote in message
news:OO**************@TK2MSFTNGP09.phx.gbl...
你能做一个搜索吗?对于另一个字符串中的一个字符串更多吗?
someString.IndexOf(" something1"," something2"," some thing3",0)
或者你需要做的事情如下:
if((someString.IndexOf(" something1",0)> = 0)||
((someString.IndexOf(" something2",0)> = 0)||
((someString.IndexOf(" something3",0)> = 0))
{
}
谢谢,
Tom
Can you do a search for more that one string in another string?
Something like:
someString.IndexOf("something1","something2","some thing3",0)
or would you have to do something like:
if ((someString.IndexOf("something1",0) >= 0) ||
((someString.IndexOf("something2",0) >= 0) ||
((someString.IndexOf("something3",0) >= 0))
{
Do something
}
Thanks,
Tom
" Nicholas Paldino [.NET / C#MVP]"< mv*@spam.guard.caspershouse.com>写在
消息新闻:O5 ************* @ TK2MSFTNGP12.phx.gbl ...
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:O5*************@TK2MSFTNGP12.phx.gbl...
Tom,
你最好的选择是使用正则表达式。你可以使用系统中的
类.Text.RegularExpr essions命名空间来做到这一点。
这对多个if测试更合适吗?
我不知道哪个更有效率。对于所有不同的物品,两者都必须返回并测试
。
谢谢,
Tom <希望这会有所帮助。
-
- Nicholas Paldino [.NET / C#MVP]
- mv*@spam.guard.caspershouse.com
tshad < TS ********** @ ftsolutions.com>在消息中写道
新闻:OO ************** @ TK2MSFTNGP09.phx.gbl ...
Tom,
Your best bet would be to use a regular expression. You can use the
classes in the System.Text.RegularExpressions namespace to do this.
This would be preferrable to the multiple if tests?
I don''t know which is more efficient. Both would have to go back and test
for all the different items.
Thanks,
Tom
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"tshad" <ts**********@ftsolutions.com> wrote in message
news:OO**************@TK2MSFTNGP09.phx.gbl...
你可以搜索更多的那个字符串在另一个字符串中?
类似于:
someString.IndexOf(" something1"," something2"," some thing3",0)
或者你必须做类似的事情:
if((someString.IndexOf(" something1",0)> = 0)||
((someString。 IndexOf(" something2",0)> = 0)||
((someString.IndexOf(" something3",0)> = 0))
{
做点什么
}
谢谢,
Tom
Can you do a search for more that one string in another string?
Something like:
someString.IndexOf("something1","something2","some thing3",0)
or would you have to do something like:
if ((someString.IndexOf("something1",0) >= 0) ||
((someString.IndexOf("something2",0) >= 0) ||
((someString.IndexOf("something3",0) >= 0))
{
Do something
}
Thanks,
Tom
tshad< ts ********** @ ftsolutions.com>写道:
tshad <ts**********@ftsolutions.com> wrote:
您最好的选择是使用正则表达式。可以使用System.Text.RegularExpressions命名空间中的
类来执行此操作。
Your best bet would be to use a regular expression. You can use the
classes in the System.Text.RegularExpressions namespace to do this.
这将是preferra如果进行测试,可以使用多个?
我不知道哪个更有效率。两者都必须回去测试所有不同的项目。
This would be preferrable to the multiple if tests?
I don''t know which is more efficient. Both would have to go back and test
for all the different items.
就个人而言,我会选择if。测试 - 可能使用辅助方法
使用params字符串数组来提高可读性 - 除非性能确实是一个问题,在这种情况下测量性能和那个/>
的正则表达式是绝对必要的。
正则表达式非常强大,但可能更难阅读
比一系列非常简单的字符串操作。
-
Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet
如果回复该组,请不要给我发邮件
Personally, I''d go for the "if" tests - possibly with a helper method
using a params string array to aid readability - unless the performance
is really a problem, in which case measuring that performance and that
of the regular expressions would be an absolute necessity.
Regular expressions are really powerful, but can be much harder to read
than a series of very simple string operations.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
这篇关于在字符串中搜索多个内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!