aspose file tools*
The moose likes Beginning Java and the fly likes indexOf Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "indexOf" Watch "indexOf" New topic
Author

indexOf

Vallaru smitha
Ranch Hand

Joined: Aug 19, 2008
Posts: 87

Hi

i was looking a for particular set of words is string using indexOf. Below is my code

String prefix1 = "Private India's Electronic Components, Inc ";
String prefix2 = "Private India's Electronic Components inc";
String prefix3 = "Private India's Electronic Components";
String prefix4 = "Private India's Electronic Components and Semiconductors";
String prefix5 = "Private India's Electronic Components And Semiconductors";
String prefix6 = "Private India's Electronic Components & Semiconductors";
String prefix7 = "- Private India'sElectronic Components, Inc.";


if(title.indexOf(prefix1) >=0){
title = title.replaceAll(prefix1, "");

}else if(title.indexOf(prefix2) >=0){
title = title.replaceAll(prefix2, "");

}else if(title.indexOf(prefix3) >=0){
title = title.replaceAll(prefix3, "");

}else if(title.indexOf(prefix4) >=0){
title = title.replaceAll(prefix4, "");

}else if(title.indexOf(prefix5) >=0){
title = title.replaceAll(prefix5, "");

}else if(title.indexOf(prefix6) >=0){
title = title.replaceAll(prefix6, "");

}else if(title.indexOf(prefix7) >=0){
title = title.replaceAll(prefix7, "");
}


when i execute this i with " This is india first Private India's Electronic Components & Semiconductors" its always going to 3rd else condition.

Is there limit for no of characters used for indexOf. Is there any other I can look for.

Thanks
smiths
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18836
    
  40


Can you change the order? Look for the longer prefixes first? There is no chance of one prefix being a substring of another if you do that.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Vallaru smitha
Ranch Hand

Joined: Aug 19, 2008
Posts: 87

thanks Henry.

that's what i did finally. But still shouldn't there be some other way we could check for the exact word.

Smitha.
Lalit Mehra
Ranch Hand

Joined: Jun 08, 2010
Posts: 384

Henry Wong wrote:
Can you change the order? Look for the longer prefixes first? There is no chance of one prefix being a substring of another if you do that.

Henry


Mr. Henry is right ...

and as far as there is some other way ... then you might have to do it char by char and a quite (though not that complex) complex algorithm

cheers. Lucky


http://plainoldjavaobject.blogspot.in
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11313
    
  16

How would this limit to the number of characters work, exactly? Either I'm not understanding what you are asking for, or you haven't really thought it out - that's not an insult, it's a challenge for you to explain exactly what you would have it do that would not break what it already does.

take a simpler case, if that's easier, such as:

prefix1 = "fred"
prefix2 = "freder"
prefix3 = "fre"

input string "My father's name was frederick"

How would your method with a limit work?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: indexOf