• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding remove word in a string

 
santhosh kumar vk
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
Hibernate Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 160
IntelliJ IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
santhosh kumar vk
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Thank you for your reply,can you tell me how to do using regular expression.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12186
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<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>
 
santhosh kumar vk
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i need new string,just i have to replace as a "" in that string
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic