• Post Reply Bookmark Topic Watch Topic
  • New Topic

find and add in between char?  RSS feed

 
Niklas Karlsson
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
I want to replace:
<TR><TD>some text</TD><TD>some text</TD>some text...
with:
<TR><TD>some text</TD><TD>some text</TD><TD>NEW</TD>some text...

and Im stuck :-/
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Yeah, I think you are going to have to give us a bit more context here...

First, since you posted this in the Java forum, can we assume that these are held in Strings? Or is this part of a Servlet/JSP?

Anyway, assuming string, then the String class has methods to find and replace string values, to generate new strings. It also have access to regular expressions too, but I probably won't recommend that to a beginner.  Take a look at the JavaDoc regarding the String class.

Henry

 
Niklas Karlsson
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry, ; yes it is in string format :-)
thank you!
 
Fred Kleinschmidt
Bartender
Posts: 572
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One way is to use the lastIndexOf() method of String to find the final "<TD>", use substring() to copy up through that text, append your new text, and then append the rest of the original line.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question looks too difficult for this forum, so I shall move it.

I would probably ues a StringBuilder←link for manipulating ordinary text.
Have you looked at HTML parsers? There is probably something ready‑made which will do what you want.
Have you considered parsing that text? You can create a List<HtmlStatement> (or similar) and insert elements into that List.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!