INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Replace variable string

Replace variable string

(OP)
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="http://www.mysite.com"><img src="test.gif" ></a><font size="3" face="Arial"> <u><b>Title</b></u> - Text ............................................................................... ............................... ...................................... .....................</font></p>
<p><a href="http://www.mysite.com"><img 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");
}
}
}

RE: Replace variable string

I'd try replaceAll method from String class. Here you have an example

Cheers,
Dian

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close