Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replace variable string

Status
Not open for further replies.

k4ghg

Technical User
Dec 25, 2001
191
US
I convert an old html file to us CSS, so I need to remove various strings and replace them with <span class = "title"> and other tag. I can open and process my file, even change replace the starting tags but not tags that begin at position x and end at position y. Below is an example of the text any help would be appreciated.

<p><a href=" src="test.gif" ></a><font size="3" face="Arial"> <u><b>Title</b></u> - Text ............................................................................... ............................... ...................................... .....................</font></p>
<p><a href=" src="test.gif" ></a><font size="3" face="Arial"> <u><b>Title</b></u> - Text ............................................................................... ............................... ...................................... .....................</font></p>


import java.io.*;
import java.lang.String.*;

public class FileTestReplace{

public static void main(String args[]){
int count = 0, ch;
try{

File f1 = new File("C:/temp/test.HTML");
BufferedReader in = new BufferedReader(new FileReader(f1));
File f2 = new File("C:/temp/JOUT.HTML");
BufferedWriter out = new BufferedWriter(new FileWriter(f2));
String s="";
int lineCount=0;

String s1 = "<p";
String s2 = "RC";

String searchQuery1 = "search"; // 0
String replaceWord = "<span class = \"title\" >";
String searchQuery2 = "b>";


while ((s=in.readLine())!=null){

if (s.length() > 1){

// Need to trim to get rid of extra space
//Want to only change lines that start as paragraphs
if ( s.substring(0,2).trim().equals(s1) ) {

int startIndexVal = 0;
int endIndexVal = 0;
int endofIndexVal = 0;

// look for the starting index of searchQuery
startIndexVal = 0; //str.indexOf(searchQuery);
endofIndexVal = s.indexOf(s);

// evaluate the ending Index of searchQuery
endIndexVal = startIndexVal + endofIndexVal ;

// Form the string again using the substring() method
s = "<p>" + replace(s, 50, endIndexVal);

out.write(s);
out.newLine();

}
}
in.close();
out.close();
}


catch(IOException e){
System.out.println("I/O Error occurred");
}
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top