Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • 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!

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

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...I can't start my day without this site!..."

Geography

Where in the world do Tek-Tips members come from?

Replace variable string

k4ghg (TechnicalUser)
13 May 12 16:40
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");
}
}
}
Diancecht (Programmer)
14 May 12 3:08
I'd try replaceAll method from String class. Here you have an example

Cheers,
Dian

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!

Back To Forum

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