不区分大小写的Arraylist.indexof搜索 [英] Case insensitive Arraylist.indexof search

查看:95
本文介绍了不区分大小写的Arraylist.indexof搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串值的arraylist。我想搜索arraylist

来查找特定字符串的索引,我希望搜索是

不区分大小写。


dim al as new arraylist

al.add(one)

al.add(" two")

>
dim x as integer = al.indexof(" ONE")我想找到这个匹配

并返回0


有没有办法让这种情况发生?


John

解决方案

我无法知道这允许你不幸地对

aray对象进行文本比较。您可以使用命令lcase或ucase来比较

将两个字符串转换为小写或大写字母。


例如:


dim al as new arraylist

al.add(one)

al.add(two)


dim x as integer = al.indexof(lcase(" ONE"))


但在这种情况下,如果你有al.add(oNe ;)然后我想它不会工作。

你总是可以做al.add(lcase(oNe))。


或者你可以使用这个简单的搜索模式反而克服了这个问题。希望你的arraylist不是太大,否则这将需要一段时间。


Public al As Collections.ArrayList

Private obj作为对象

私有strtest作为字符串

作为整数的私有索引


Public Sub mysub()

al.Add(你好)

strtest =" HeLLo"


For each obj in al

如果LCase(CStr(al.Item(al.IndexOf(obj))))= LCase(strtest)然后

indexof = al.IndexOf(obj)

结束如果

下一页

结束子

如果它不起作用请不要打我。我经常测试它!


希望这会有所帮助。 Matt


JohnR写道:

我有一个字符串值的arraylist。我想搜索arraylist
来查找特定字符串的索引,我希望搜索对
不区分大小写。

dim al as new arraylist
al.add(one)
al.add(two)

dim x as integer = al.indexof(" ONE")我想这个找到比赛
然后返回0

有没有办法让这种情况发生?

John



糟糕。该循环不会在它应该完成后完成。


后行:indexof = al.IndexOf(obj)

你将放置:退出

Matt写道:

我不知道如何让你不幸地对
aray对象进行文本比较。在比较之前,您可以使用命令lcase或ucase将两个字符串转换为小写或大写。

例如:

dim al as new arraylist
al.add(one)
al.add(two)

dim x as integer = al.indexof(lcase(" ONE"))

但在这种情况下,如果你有al.add(oNe),那么我猜它不会工作。
你总是可以做al.add(lcase(" oNe")) )。

或者您可以使用这种简单的搜索模式来克服这个问题。希望你的arraylist不是太大,否则这将需要一段时间。

Public al As Collections.ArrayList
Private obj As Object
Private strtest As String
整数私有索引

Public Sub mysub()
al.Add(" hello")
strtest =" HeLLo"

对于每个obj In al
如果LCase(CStr(al.Item(al.IndexOf(obj))))= LCase(strtest)那么
indexof = al.IndexOf(obj)
结束如果
下一页
结束子

如果它不起作用请不要打我。我经常测试它!

希望这会有所帮助。 Matt

JohnR写道:

我有一个字符串值的arraylist。我想搜索
arraylist以找到特定字符串的索引,我希望搜索不区分大小写。

dim al as new arraylist
al.add(one)
al.add(two)

dim x as integer = al.indexof(" ONE")我想这个找到
匹配并返回0

有没有办法实现这一目标?

John



如果你添加它们之后没有对项目进行排序,那么

可以使用CaseInsensitiveComparer和binarysearch,但这仅适用于

你用你想要用于

二元搜索的

Icomparer(CaseInsensitiveComparer.Default)对arraylist进行排序。


Hth Greetz Peter


Dim arrList As New ArrayList


arrList.Add(" One")

arrList.Add(" tWo")

arrList.Ad d(thRee)

arrList.Sort(CaseInsensitiveComparer.Default)

MsgBox(arrList.BinarySearch(" three",

) CaseInsensitiveComparer.Default))


-

今天的编程是软件工程师之间的竞争,他们正努力建立更大更好的白痴 - 防程序,宇宙试图产生更大更好的白痴。到目前为止,宇宙正在赢得胜利。

" Matt" <毫安***** @ interactiveideasandsolutions.com> schreef in bericht

新闻:Ov ************** @ TK2MSFTNGP12.phx.gbl ...

糟糕。这个循环不会在它应该完成之后完成。

后行:indexof = al.IndexOf(obj)
你要放:退出

Matt写道:

我不知道如何让你不幸地对
aray对象进行文本比较。在比较之前,您可以使用命令lcase或ucase将两个字符串转换为小写或大写。

例如:

dim al as new arraylist
al.add(one)
al.add(two)

dim x as integer = al.indexof(lcase(" ONE"))

但在这种情况下,如果你有al.add(oNe),那么我猜它不会工作。
你总是可以做al.add(lcase(" oNe")) )。

或者您可以使用这种简单的搜索模式来克服这个问题。希望你的arraylist不是太大,否则这将需要一段时间。

Public al As Collections.ArrayList
Private obj As Object
Private strtest As String
整数私有索引

Public Sub mysub()
al.Add(" hello")
strtest =" HeLLo"

对于每个obj In al
如果LCase(CStr(al.Item(al.IndexOf(obj))))= LCase(strtest)
那么indexof = al.IndexOf(obj)
结束如果
下一页
结束子

如果它不起作用请不要打我。我经常测试它!

希望这会有所帮助。 Matt

JohnR写道:

我有一个字符串值的arraylist。我想搜索
arraylist以找到特定字符串的索引,我希望搜索不区分大小写。

dim al as new arraylist
al.add(one)
al.add(two)

dim x as integer = al.indexof(" ONE")我想这个找到
匹配并返回0

有没有办法让这种情况发生?

John



I have an arraylist of string values. I would like to search the arraylist
to find the index of a particular string and I would like the search to be
case insensitive.

dim al as new arraylist
al.add("one")
al.add("two")

dim x as integer = al.indexof("ONE") I would like this to find the match
and return 0

Is there a way to make this happen?

John

解决方案

There is no way i know of that allows you to do a textual compare on
aray objects unfortunatly. You could use the command lcase or ucase to
convert both strings to lower or upper case before you compare.

example:

dim al as new arraylist
al.add("one")
al.add("two")

dim x as integer = al.indexof(lcase("ONE"))

but in this case, if you have al.add("oNe") then i guess it wont work.
you could always do al.add(lcase("oNe")).

or you could use this simple search pattern instead to overcome that
problem. hopefully your arraylist isnt too big, otherwise this would
take a while.

Public al As Collections.ArrayList
Private obj As Object
Private strtest As String
Private indexof As Integer

Public Sub mysub()
al.Add("hello")
strtest = "HeLLo"

For Each obj In al
If LCase(CStr(al.Item(al.IndexOf(obj)))) = LCase(strtest) Then
indexof = al.IndexOf(obj)
End If
Next
End Sub
Please dont hit me if it doesnt work. i havnt tested it!

Hope this helps. Matt

JohnR wrote:

I have an arraylist of string values. I would like to search the arraylist
to find the index of a particular string and I would like the search to be
case insensitive.

dim al as new arraylist
al.add("one")
al.add("two")

dim x as integer = al.indexof("ONE") I would like this to find the match
and return 0

Is there a way to make this happen?

John



Oops. that loop wont finish when its supposed to.

after the line: indexof = al.IndexOf(obj)
you will to put: exit for
Matt wrote:

There is no way i know of that allows you to do a textual compare on
aray objects unfortunatly. You could use the command lcase or ucase to
convert both strings to lower or upper case before you compare.

example:

dim al as new arraylist
al.add("one")
al.add("two")

dim x as integer = al.indexof(lcase("ONE"))

but in this case, if you have al.add("oNe") then i guess it wont work.
you could always do al.add(lcase("oNe")).

or you could use this simple search pattern instead to overcome that
problem. hopefully your arraylist isnt too big, otherwise this would
take a while.

Public al As Collections.ArrayList
Private obj As Object
Private strtest As String
Private indexof As Integer

Public Sub mysub()
al.Add("hello")
strtest = "HeLLo"

For Each obj In al
If LCase(CStr(al.Item(al.IndexOf(obj)))) = LCase(strtest) Then
indexof = al.IndexOf(obj)
End If
Next
End Sub
Please dont hit me if it doesnt work. i havnt tested it!

Hope this helps. Matt

JohnR wrote:

I have an arraylist of string values. I would like to search the
arraylist to find the index of a particular string and I would like
the search to be case insensitive.

dim al as new arraylist
al.add("one")
al.add("two")

dim x as integer = al.indexof("ONE") I would like this to find the
match and return 0

Is there a way to make this happen?

John



If it doesn''t mattter that the items are sorted after you''ve added them you
can use the CaseInsensitiveComparer and binarysearch, but this only works if
you sort the arraylist with the same
Icomparer(CaseInsensitiveComparer.Default) as you want to use for your
binarysearch.

Hth Greetz Peter

Dim arrList As New ArrayList

arrList.Add("One")
arrList.Add("tWo")
arrList.Add("thRee")
arrList.Sort(CaseInsensitiveComparer.Default)
MsgBox(arrList.BinarySearch("three",
CaseInsensitiveComparer.Default))

--
Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to produce
bigger and better idiots. So far, the Universe is winning.
"Matt" <ma*****@interactiveideasandsolutions.com> schreef in bericht
news:Ov**************@TK2MSFTNGP12.phx.gbl...

Oops. that loop wont finish when its supposed to.

after the line: indexof = al.IndexOf(obj)
you will to put: exit for
Matt wrote:

There is no way i know of that allows you to do a textual compare on
aray objects unfortunatly. You could use the command lcase or ucase to
convert both strings to lower or upper case before you compare.

example:

dim al as new arraylist
al.add("one")
al.add("two")

dim x as integer = al.indexof(lcase("ONE"))

but in this case, if you have al.add("oNe") then i guess it wont work.
you could always do al.add(lcase("oNe")).

or you could use this simple search pattern instead to overcome that
problem. hopefully your arraylist isnt too big, otherwise this would
take a while.

Public al As Collections.ArrayList
Private obj As Object
Private strtest As String
Private indexof As Integer

Public Sub mysub()
al.Add("hello")
strtest = "HeLLo"

For Each obj In al
If LCase(CStr(al.Item(al.IndexOf(obj)))) = LCase(strtest) Then indexof = al.IndexOf(obj)
End If
Next
End Sub
Please dont hit me if it doesnt work. i havnt tested it!

Hope this helps. Matt

JohnR wrote:

I have an arraylist of string values. I would like to search the
arraylist to find the index of a particular string and I would like
the search to be case insensitive.

dim al as new arraylist
al.add("one")
al.add("two")

dim x as integer = al.indexof("ONE") I would like this to find the
match and return 0

Is there a way to make this happen?

John



这篇关于不区分大小写的Arraylist.indexof搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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