• Post Reply Bookmark Topic Watch Topic
  • New Topic

indexOf(str)  RSS feed

 
Arjunkumar Shastry
Ranch Hand
Posts: 986
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How efficient is method indexOf(str) which is in java.lang.String class?
In an application I m writing,program is picking up a string value from column in a databse table.Average size of this string exceeds 25,000 characters(Its kind of Diary field.In SQL 2000,its varchar and that source application allows user to enter data till 35000 characters.)I wanted to find whether some String is a substring of this big string.Is there any other way than using indexOf(String str) method?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roughly all that method does is steps through the string as an array of characters and stops when it finds the specified character pattern. How quickly it performs is very dependant on the String you are testing, for example looking for "ab" in "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaba" will probably be far slower than in "cccccccccccccccccccccccccccccccabc" since every character in the first string looks like the start of your pattern. The only real saving you can make is providing an offset to start the search at.

But as with any performance question, your best approach is not to worry too much about the performance now, write the application so it does what you need it to do, then look to any improvements you can make to speed things up if necessary.
[ April 01, 2005: Message edited by: Paul Sturrock ]
 
Arjunkumar Shastry
Ranch Hand
Posts: 986
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.
There are methods matches(String regex)and replaceFirst(String regex,String replacement)too.But I will do comparison latter.
 
Pradeep bhatt
Ranch Hand
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Arjunkumar Shastry:
Thanks.
There are methods matches(String regex)and replaceFirst(String regex,String replacement)too.But I will do comparison latter.


Those methods are useful if you have a RE , otherwise indexOf will do.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!