Link to home
Start Free TrialLog in
Avatar of Idarhillgaar
IdarhillgaarFlag for Ireland

asked on

Search for a String in StringBuffer

I load a lot of values into a StringBuffer, then I want to make  a search for a certain String.

#1 Should I use Vectors instead?
#2 How will I implement a search method to go through StringBuffer and return with the String I searched after.

Avatar of aozarov
aozarov

You can use the StringBuffer.indexOf method for searching strings inside a stringbuffer
Avatar of CEHJ
You can use a Matcher. e.g. ni the API docs:


http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Matcher.html
The above method will return you the position of that string inside the stringBuffer or -1 if not found.
>> I load a lot of values into a StringBuffer
If you just load them to the stringBufer so you can search for their existence then
you will definetly better of by adding them to HashSet and checking if exists.
SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Idarhillgaar

ASKER

Yes,

I know hastables so I'm going for those.  Thanks for all your inputs

--
Idar
:-)

A HashMap would be better
oops that's what I meant - trying to multitask. :)