检查数组中的任何值 [英] checking for any value in an array
问题描述
让我们说我有一个字符串,可以是逗号分隔的任意数量的项目:
2,4,6,12,345
>
我想在if / then语句中检查这些值:
如果整数=字符串中的任何值,则...
看起来最好的方法是将字符串转换为
数组。一旦我这样做,是否有一种简单的方法可以写出
如果[我的值] = [数组集合中的任何项目]那么...
>
或者我是否需要逐个遍历每个数组项目以寻找匹配?
-Darrel
是的,唯一的方法是使用拆分
将字符串转换为整数数组并在数组中搜索。如果你的数组非常大,你可能想要b
考虑对它进行排序并对该值进行二元搜索以减少你的
搜索空间。
另一种非推荐的方式(在某些情况下仍适用)是
取原始字符串并取出所有空格并添加,在
开始和结束,看起来像:
,num1,num2,num3,num4,
如果你想搜索对于你的号码numX,你可以这样一个子串
搜索,numX,。
又一次,这个方法是一个有点破解,所以我推荐第一个
方法。
希望这有帮助
-
- Shuvro
SDE,MSFT
此帖子按原样提供。没有保证,也没有授予任何权利。
使用包含的脚本样本须遵守
http://www.microsoft.com/info/cpyright.htm 。
darrel ; <无***** @ hotmail.com>在消息中写道
新闻:OB ************** @ TK2MSFTNGP09.phx.gbl ...让我们说我有一个字符串,可以是逗号分隔的任意数量的项目:
2,4,6,12,345
我想检查if /中的那些值然后声明:
如果整数=字符串中的任何值,那么...
似乎最好的方法是将字符串转换为
数组。一旦我这样做,有没有一种简单的方法可以写出
如果[我的价值] = [数组集合中的任何项目]那么......
或者我需要逐个遍历每个数组项目寻找
匹配吗?
-Darrel
< blockquote>是的,唯一的方法是使用拆分
将字符串转换为整数数组并在数组内搜索。如果你的数组非常大,你可能想要b
考虑对它进行排序并对该值进行二元搜索以减少你的
搜索空间。
另一种非推荐的方式(在某些情况下仍适用)是
取原始字符串并取出所有空格并添加,在
开始和结束,看起来像:
,num1,num2,num3,num4,
如果你想搜索对于你的号码numX,你可以这样一个子串
搜索,numX,。
又一次,这个方法是一个有点破解,所以我推荐第一个
方法。
希望这有帮助
-
- Shuvro
SDE,MSFT
此帖子按原样提供。没有保证,也没有授予任何权利。
使用包含的脚本样本须遵守
http://www.microsoft.com/info/cpyright.htm 。
darrel ; <无***** @ hotmail.com>在消息中写道
新闻:OB ************** @ TK2MSFTNGP09.phx.gbl ...
让我们说我有一个字符串,可以是逗号分隔的任意数量的项目:
2,4,6,12,345
我想检查if /中的那些值然后声明:
如果整数=字符串中的任何值,那么...
似乎最好的方法是将字符串转换为
数组。一旦我这样做,有没有一种简单的方法可以写出
如果[我的价值] = [数组集合中的任何项目]那么......
或者我需要逐个遍历每个数组项目寻找
匹配吗?
-Darrel
< BLOCKQUOTE>"达雷尔" <无***** @ hotmail.com>在消息中写道
新闻:OB ************** @ TK2MSFTNGP09.phx.gbl ...
或我是否需要逐个遍历每个数组项目以寻找
匹配?
使用ArrayList而不是Array - ArrayList对象具有Contains ()
方法,它告诉你ArrayList中是否包含一个值。
let''s say I have a string that can be any number of items comma delimited:
2, 4, 6, 12, 345
I want to check against those values in an if/then statement:
if integer = any value in the string, then...
It seems the best way to do this would be to convert the string into an
array. Once I do that, is there a simple way to write out
if [my value] = [any item in the array collection] then...
or do I need to loop through each array item one by one looking for a match?
-Darrel
Yes, the only way is to convert the string to an integer array using a split
and search inside the array. If your array is very large, you might want to
consider sorting it and doing a binary search for the value to reduce your
search space.
Another non-recommended way (which nevertheless works in some cases) is to
take the original string and take out all spaces and add a "," at the
begining and end so that it looks like:
,num1,num2,num3,num4,
And if you want to search for your number "numX", you can so a substring
search for ",numX,".
But again, this method is a bit of hack, so I would recommend the first
method.
Hope this helps
--
- Shuvro
SDE, MSFT
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm.
"darrel" <no*****@hotmail.com> wrote in message
news:OB**************@TK2MSFTNGP09.phx.gbl...let''s say I have a string that can be any number of items comma delimited:
2, 4, 6, 12, 345
I want to check against those values in an if/then statement:
if integer = any value in the string, then...
It seems the best way to do this would be to convert the string into an
array. Once I do that, is there a simple way to write out
if [my value] = [any item in the array collection] then...
or do I need to loop through each array item one by one looking for a
match?
-Darrel
Yes, the only way is to convert the string to an integer array using a split
and search inside the array. If your array is very large, you might want to
consider sorting it and doing a binary search for the value to reduce your
search space.
Another non-recommended way (which nevertheless works in some cases) is to
take the original string and take out all spaces and add a "," at the
begining and end so that it looks like:
,num1,num2,num3,num4,
And if you want to search for your number "numX", you can so a substring
search for ",numX,".
But again, this method is a bit of hack, so I would recommend the first
method.
Hope this helps
--
- Shuvro
SDE, MSFT
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm.
"darrel" <no*****@hotmail.com> wrote in message
news:OB**************@TK2MSFTNGP09.phx.gbl...let''s say I have a string that can be any number of items comma delimited:
2, 4, 6, 12, 345
I want to check against those values in an if/then statement:
if integer = any value in the string, then...
It seems the best way to do this would be to convert the string into an
array. Once I do that, is there a simple way to write out
if [my value] = [any item in the array collection] then...
or do I need to loop through each array item one by one looking for a
match?
-Darrel
"darrel" <no*****@hotmail.com> wrote in message
news:OB**************@TK2MSFTNGP09.phx.gbl...
or do I need to loop through each array item one by one looking for a
match?
Use an ArrayList instead of an Array - ArrayList objects have a Contains()
method which tells you if a value is contained within the ArrayList.
这篇关于检查数组中的任何值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!