This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Java in General and the fly likes Equals VS indexOf Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Equals VS indexOf" Watch "Equals VS indexOf" New topic
Author

Equals VS indexOf

Jordan Smith
Ranch Hand

Joined: Apr 06, 2008
Posts: 89
Hi!
I want to check if "News" appears in a string
what is being calculated quicker?


or
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

If your requirement is to find out if a pattern appears in a String you can't use equals. This will only match if the whole String is equal to your test.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Jordan Smith
Ranch Hand

Joined: Apr 06, 2008
Posts: 89
right.
this is why my condition is different in each case
actually the string for sure will start with www.mysite.com/ and the question is about the word News.

I thought to write endsWith instead of equals. but still, what is quicker? endsWith or indexOf?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16695
    
  19

I don't think this will make a notable difference, as you application will do something else after it validates the URL. However, if you really are concerned, then you can write a test -- and test both options.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Sean Clark
Rancher

Joined: Jul 15, 2009
Posts: 377

Hey,

I don't think it will make a lot of of difference, you could also use the matches method, I prefer using this as it allows you to add a REGEX which is I think is more powerful. so...



Also indexOf() only searches for a character so would be no use for you.
Ends with would also work so long as you weren't going to be adding any GET parameters on the end of your URL.
You could also use .contains("News");

You have a load of choices!

Sean


I love this place!
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
odelya yomtov-glick wrote:what is quicker? endsWith or indexOf?

endsWith is absolutely quicker.
But you wrote
I want to check if "News" appears in a string

in this case endWith wont work, because it cheks if the string ENDS with given substring,
but not if the substring appears somewhere in the string - you must use indexOf to check it.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

I agree with Ireneusz that endsWith, startsWith and equals are most likely faster than indexOf.

In those 3 methods, the start position is known, so you need only one loop: over the string to check for. With indexOf, you will need to loop over the string to search in, checking each time if the string to search for is located at that position. I've confirmed this by checking String.java - equals, startsWith and endsWith only have one single loop whereas indexOf has a loop in a loop.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
R van Vliet
Ranch Hand

Joined: Nov 10, 2007
Posts: 144
You could, ofcourse, test it
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3011
    
    9
Sean Clark wrote:Also indexOf() only searches for a character so would be no use for you.


Unless you use the version of indexOf that searches for a String


Joanne
Sean Clark
Rancher

Joined: Jul 15, 2009
Posts: 377

Joanne Neal wrote:
Sean Clark wrote:Also indexOf() only searches for a character so would be no use for you.


Unless you use the version of indexOf that searches for a String


Good point, I'm not sure how I missed that.
I have a lot to learn!
Anjali Raman
Ranch Hand

Joined: Nov 28, 2007
Posts: 57
Hi,
You could use the contains() method too. Its pretty fast.

Cheers
Anjali
D. Ogranos
Ranch Hand

Joined: Feb 02, 2009
Posts: 213
Sean Clark wrote:Hey,

I don't think it will make a lot of of difference, you could also use the matches method, I prefer using this as it allows you to add a REGEX which is I think is more powerful. so...


Since the OP asked about the quickest way, I wouldn't recommend using regular expressions. They are a LOT slower than using indexOf().

That said, I ran two quick tests. I found that equals() is about 6-7 times faster than indexOf() (rather, I compared the the expressions equals(x) and indexOf(x) != -1). However, endsWith() is only slightly faster than indexOf(), which in turn is slightly faster than contains(). A test of matches() vs. indexOf() showed that indexOf() was about 33 times faster than matches(). Of course, all these tests are simple and with short example strings only.
Sean Clark
Rancher

Joined: Jul 15, 2009
Posts: 377

I wouldn't recommend using regular expressions. They are a LOT slower than using indexOf().


I agree... Regexs have their place, but not here.

apparently equals() and contains() are about the same speed.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Equals VS indexOf
 
Similar Threads
�JavaScript to Java and back again� A developers adventure
Getting a couple errors on scribble applet
Applets init()
JSP String value obtained from servlet out
Quotes in strings