• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

indexOf

 
Vallaru smitha
Ranch Hand
Posts: 87
Firefox Browser Hibernate Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20831
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Vallaru smitha
Ranch Hand
Posts: 87
Firefox Browser Hibernate Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 384
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12016
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic