| Author |
Regarding remove word in a string
|
santhosh kumar vk
Ranch Hand
Joined: Aug 25, 2009
Posts: 117
|
|
Hi
how to remove a word to another word from string
for example
i have a string like String s= "<html><body>this is test</body></html>
in the above string i have to remove the starting <body> tag to ending</ body> tag in between the value will come dynamically,the out put should be s="<html></html>"
can you please help me
thanks,
Santhosh
|
 |
K Amit Gupta
Greenhorn
Joined: Feb 22, 2011
Posts: 4
|
|
You can use Regex in java to manuplate these kind of thing...
I have one programme.. which can help you if you don't want to google Regrx.
but this programme is for one occurance. you can run it in a loop....
i think you must try with Regrx... that will really help...
class Demo{
public static void main(String args[]){
String str="<html><body>this is test</body></html>";
String str1="<body>";
String str2="</body>";
Demo d1=new Demo();
int startpara=d1.startindex(str,str1);
int endpara=d1.endindex(str,str2);
String finalString=str.substring(0,startpara)+str.substring(endpara+str2.length(),str.length());
System.out.println(finalString);
}
public int startindex(String str, String str1){
int indexstart= str.indexOf(str1,0);
return indexstart;
}
public int endindex(String str,String str2){
int endindex= str.indexOf(str2,0);
return endindex;
}
}
|
 |
Riaan Nel
Ranch Hand
Joined: Apr 23, 2009
Posts: 157
|
|
I'd look at using the substring() method with the index of "<html>" and "</html>".
On a side note, do you always want to get a String with only the opening and closing HTML tags?
|
"The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man." - George Bernard Shaw
|
 |
santhosh kumar vk
Ranch Hand
Joined: Aug 25, 2009
Posts: 117
|
|
Hi
Thank you for your reply,can you tell me how to do using regular expression.
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 10043
|
|
<pedant mode>
You cannot remove a word from a string. Strings are immutable. All you can do is create a new string formed how you want.
</pedant mode>
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
santhosh kumar vk
Ranch Hand
Joined: Aug 25, 2009
Posts: 117
|
|
|
i need new string,just i have to replace as a "" in that string
|
 |
 |
|
|
subject: Regarding remove word in a string
|
|
|