PC Review


Reply
Thread Tools Rate Thread

Case insensitive Arraylist.indexof search

 
 
JohnR
Guest
Posts: n/a
 
      11th Oct 2005
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


 
Reply With Quote
 
 
 
 
Matt
Guest
Posts: n/a
 
      11th Oct 2005
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
>
>

 
Reply With Quote
 
Matt
Guest
Posts: n/a
 
      11th Oct 2005
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
>>

 
Reply With Quote
 
Peter Proost
Guest
Posts: n/a
 
      11th Oct 2005
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" <(E-Mail Removed)> schreef in bericht
news:(E-Mail Removed)...
> 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
> >>



 
Reply With Quote
 
Ken Tucker [MVP]
Guest
Posts: n/a
 
      11th Oct 2005
Hi,

In addition to Peters comments. Option compare text will make all
of your string compares case insensitive.

http://msdn.microsoft.com/library/de...ionCompare.asp

Ken
---------------
"Matt" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>>>



 
Reply With Quote
 
JohnR
Guest
Posts: n/a
 
      11th Oct 2005

Thanks, Peter. I didn't think of that.

John

"Peter Proost" <(E-Mail Removed)> wrote in message
news:%23Dg%(E-Mail Removed)...
> 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" <(E-Mail Removed)> schreef in bericht
> news:(E-Mail Removed)...
>> 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
>> >>

>
>



 
Reply With Quote
 
JohnR
Guest
Posts: n/a
 
      11th Oct 2005
Hi Ken,

I tried option Compare but it didn't work for the IndexOf which gets
evaluated in the object.equals method.

Ron

"Ken Tucker [MVP]" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> Hi,
>
> In addition to Peters comments. Option compare text will make all
> of your string compares case insensitive.
>
> http://msdn.microsoft.com/library/de...ionCompare.asp
>
> Ken
> ---------------
> "Matt" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> 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
>>>>

>
>



 
Reply With Quote
 
Matt
Guest
Posts: n/a
 
      12th Oct 2005
I learnt something new today!

Thanks Peter

Peter Proost wrote:
> 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.
>
>

 
Reply With Quote
 
=?Utf-8?B?RGVubmlz?=
Guest
Posts: n/a
 
      12th Oct 2005
Just curious as to why you have to sort the arraylist first...trying to learn
all I can and don't undetstand?
--
Dennis in Houston


"Peter Proost" wrote:

> 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" <(E-Mail Removed)> schreef in bericht
> news:(E-Mail Removed)...
> > 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
> > >>

>
>
>

 
Reply With Quote
 
Peter Proost
Guest
Posts: n/a
 
      12th Oct 2005
Hi Dennis, is far as I remember, you have to do it because otherwise you can
get the wrong index returned.
Or as stated in the msdn:

"If comparer is provided, the elements of the ArrayList are compared to the
specified value using the specified IComparer implementation. If the
ArrayList is not already sorted according to the sort order defined by
comparer, the result might be incorrect."

you can try it for yourself, if you remove the
arrList.Sort(CaseInsensitiveComparer.Default) from my small example you
should get the wrong result.

Hth Greetz Peter



--
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.


"Dennis" <(E-Mail Removed)> schreef in bericht
news891F9E9-68B4-41CB-912C-(E-Mail Removed)...
> Just curious as to why you have to sort the arraylist first...trying to

learn
> all I can and don't undetstand?
> --
> Dennis in Houston
>
>
> "Peter Proost" wrote:
>
> > 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" <(E-Mail Removed)> schreef in bericht
> > news:(E-Mail Removed)...
> > > 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
> > > >>

> >
> >
> >



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
string.indexof case insensitive Guoqi Zheng Microsoft ASP .NET 3 10th Jul 2004 03:18 AM
Re: Fast String Case Insensitive IndexOf Jon Skeet [C# MVP] Microsoft C# .NET 0 27th May 2004 08:29 AM
Re: Fast String Case Insensitive IndexOf William Stacey [MVP] Microsoft C# .NET 2 27th May 2004 08:28 AM
Re: Fast String Case Insensitive IndexOf William Stacey [MVP] Microsoft C# .NET 0 26th May 2004 10:35 PM
Any way to make IndexOf case-insensitive? MattB Microsoft VB .NET 3 14th Jan 2004 06:48 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:29 AM.