Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Equals VS indexOf

 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
I want to check if "News" appears in a string
what is being calculated quicker?


or
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Jordan Smith
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 377
Android Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Sheriff
Posts: 22684
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could, ofcourse, test it
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Sean Clark
Rancher
Posts: 377
Android Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
You could use the contains() method too. Its pretty fast.

Cheers
Anjali
 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 377
Android Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
These are the worst of times and these are the best of times. And this is the best tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic