tanc02
asked on
How to insert some text into the string ?
Here is what I have :
<%
String strTest = new String();
int intPos = 0;
strTest = "<SPAN CLASS=obj97BF5 STYLE=\"position:absolute; left:0.000in; top:0.000in; height:1.073in\";}\">";
strTest += "<SPAN CLASS=obj97BF6 STYLE=\"position:absolute; left:0.052in; top:0.833in; width:2.771in\";}\">";
strTest += "<A HREF=\"\" NAME=\"test1\" >Test1</A></SPAN>";
strTest += "<SPAN CLASS=obj97BF6 STYLE=\"position:absolute; left:2.905in; top:0.833in; width:2.500in\";}\">";
strTest += "<A HREF=\"\" NAME=\"test2\">Test2</A></ SPAN>";
strTest += "<SPAN CLASS=obj97BF8 STYLE=\"position:absolute; left:7.052in; top:0.833in; width:1.645in\";}\">";
strTest += "<A HREF=\"\" NAME=\"test3\">Test3</A></ SPAN>";
%>
I have jsdk1.3, and I would like to insert :
id=idx
into strTest. The first SPAN will have id=id1, and second
will have id=id2 and so on.
<%
String strTest = new String();
int intPos = 0;
strTest = "<SPAN CLASS=obj97BF5 STYLE=\"position:absolute;
strTest += "<SPAN CLASS=obj97BF6 STYLE=\"position:absolute;
strTest += "<A HREF=\"\" NAME=\"test1\" >Test1</A></SPAN>";
strTest += "<SPAN CLASS=obj97BF6 STYLE=\"position:absolute;
strTest += "<A HREF=\"\" NAME=\"test2\">Test2</A></
strTest += "<SPAN CLASS=obj97BF8 STYLE=\"position:absolute;
strTest += "<A HREF=\"\" NAME=\"test3\">Test3</A></
%>
I have jsdk1.3, and I would like to insert :
id=idx
into strTest. The first SPAN will have id=id1, and second
will have id=id2 and so on.
oops, everywhere you see this:
"<SPAN id='" + intPos + "'
change it to this:
"<SPAN id='id" + intPos + "'
"<SPAN id='" + intPos + "'
change it to this:
"<SPAN id='id" + intPos + "'
ASKER
I don't want to do that.
I want to scan the string and use substring method to
insert id=idx.
I want to scan the string and use substring method to
insert id=idx.
ok, so this is cleaner:
<%
String strTest = new String();
int intPos = 1;
strTest = "<SPAN id='id" + (++intPos) + "' CLASS=obj97BF5 STYLE=\"position:absolute; left:0.000in; top:0.000in; height:1.073in\";}\">";
strTest += "<SPAN id='id" + (++intPos) + "' CLASS=obj97BF6 STYLE=\"position:absolute; left:0.052in; top:0.833in; width:2.771in\";}\">";
strTest += "<A HREF=\"\" NAME=\"test1\" >Test1</A></SPAN>";
strTest += "<SPAN id='id" + (++intPos) + "' CLASS=obj97BF6 STYLE=\"position:absolute; left:2.905in; top:0.833in; width:2.500in\";}\">";
strTest += "<A HREF=\"\" NAME=\"test2\">Test2</A></ SPAN>";
strTest += "<SPAN id='id" + (++intPos) + "' CLASS=obj97BF8 STYLE=\"position:absolute; left:7.052in; top:0.833in; width:1.645in\";}\">";
strTest += "<A HREF=\"\" NAME=\"test3\">Test3</A></ SPAN>";
strTest += "</SPAN>";
%>
<%
String strTest = new String();
int intPos = 1;
strTest = "<SPAN id='id" + (++intPos) + "' CLASS=obj97BF5 STYLE=\"position:absolute;
strTest += "<SPAN id='id" + (++intPos) + "' CLASS=obj97BF6 STYLE=\"position:absolute;
strTest += "<A HREF=\"\" NAME=\"test1\" >Test1</A></SPAN>";
strTest += "<SPAN id='id" + (++intPos) + "' CLASS=obj97BF6 STYLE=\"position:absolute;
strTest += "<A HREF=\"\" NAME=\"test2\">Test2</A></
strTest += "<SPAN id='id" + (++intPos) + "' CLASS=obj97BF8 STYLE=\"position:absolute;
strTest += "<A HREF=\"\" NAME=\"test3\">Test3</A></
strTest += "</SPAN>";
%>
ooooh, stand by
You can use this function to do this:
<%!
int intPos = 0;
String nextPos()
{
return String.valueOf(++intPos);
}
String strReplace( String str, String srch, String repl )
{
if ( str==null || srch==null || repl==null )
return str;
int ndx = str.indexOf(srch);
StringBuffer buffer = new StringBuffer(str);
while ( ndx >= 0 )
{
buffer.replace(ndx,(ndx+sr ch.length( )),repl);
ndx = buffer.toString().indexOf( srch);
}
return buffer.toString();
}
//then do this:
strTest = strReplace( strTest, "<SPAN ", "<SPAN id='"+nextPos()+"' " );
%>
<%!
int intPos = 0;
String nextPos()
{
return String.valueOf(++intPos);
}
String strReplace( String str, String srch, String repl )
{
if ( str==null || srch==null || repl==null )
return str;
int ndx = str.indexOf(srch);
StringBuffer buffer = new StringBuffer(str);
while ( ndx >= 0 )
{
buffer.replace(ndx,(ndx+sr
ndx = buffer.toString().indexOf(
}
return buffer.toString();
}
//then do this:
strTest = strReplace( strTest, "<SPAN ", "<SPAN id='"+nextPos()+"' " );
%>
no, that won't work, sorry. Can you build the string one SPAN at a time? Or are you reading in an entire document all at once?
Does this work?
<%
String strTest = new String();
strTest = "<SPAN CLASS=obj97BF5 STYLE=\"position:absolute; left:0.000in; top:0.000in; height:1.073in\";}\">";
for (int intPos = 1; intPos < 3; intPos ++) {
strTest += "<SPAN ID=\"" + intPos + "\" CLASS=obj97BF" + (intPos + 5) + " STYLE=\"position:absolute; left:0.052in; top:0.833in; width:2.771in\";}\">";
strTest += "<A HREF=\"\" NAME=\"test" + intPos + "\" >Test" + intPos + "</A></SPAN>";
}
%>
<%
String strTest = new String();
strTest = "<SPAN CLASS=obj97BF5 STYLE=\"position:absolute;
for (int intPos = 1; intPos < 3; intPos ++) {
strTest += "<SPAN ID=\"" + intPos + "\" CLASS=obj97BF" + (intPos + 5) + " STYLE=\"position:absolute;
strTest += "<A HREF=\"\" NAME=\"test" + intPos + "\" >Test" + intPos + "</A></SPAN>";
}
%>
ASKER
I got this error :
org.apache.jasper.JasperEx ception: Unable to compile class for JSP
An error occurred between lines: 21 and 50 in the jsp file: /t.jsp
Generated servlet error:
D:\Program Files\Apache Tomcat 4.0\work\localhost\example s\t$jsp.ja va:39: Identifier expected.
strTest = strReplace(strTest, "<SPAN ", "<SPAN id='"+nextPos()+"' " );
^
1 error
org.apache.jasper.JasperEx
An error occurred between lines: 21 and 50 in the jsp file: /t.jsp
Generated servlet error:
D:\Program Files\Apache Tomcat 4.0\work\localhost\example
strTest = strReplace(strTest, "<SPAN ", "<SPAN id='"+nextPos()+"' " );
^
1 error
try this:
String addID( String str )
{
if ( str==null )
return str;
String srch = "<SPAN ";
String repl = srch + "id='";
int ndx = str.indexOf(srch);
int intPos = 0;
StringBuffer buffer = new StringBuffer(str);
while ( ndx >= 0 )
{
buffer.replace(ndx,(ndx+sr ch.length( )),repl+St ring.value Of(++intPo s)+"' ");
ndx = buffer.toString().indexOf( srch);
}
return buffer.toString();
}
strTest = addID(strTest);
String addID( String str )
{
if ( str==null )
return str;
String srch = "<SPAN ";
String repl = srch + "id='";
int ndx = str.indexOf(srch);
int intPos = 0;
StringBuffer buffer = new StringBuffer(str);
while ( ndx >= 0 )
{
buffer.replace(ndx,(ndx+sr
ndx = buffer.toString().indexOf(
}
return buffer.toString();
}
strTest = addID(strTest);
doooooh!
change this:
ndx = buffer.toString().indexOf( srch);
to this:
ndx = buffer.toString().indexOf( srch,ndx+1 );
change this:
ndx = buffer.toString().indexOf(
to this:
ndx = buffer.toString().indexOf(
This is very inelegant, but:
-----------
<%
String strTest = new String();
int intPos = 0;
strTest = "<SPAN CLASS=obj97BF5 STYLE=\"position:absolute; left:0.000in; top:0.000in; height:1.073in\";}\">";
strTest += "<SPAN CLASS=obj97BF6 STYLE=\"position:absolute; left:0.052in; top:0.833in; width:2.771in\";}\">";
strTest += "<A HREF=\"\" NAME=\"test1\" >Test1</A></SPAN>";
strTest += "<SPAN CLASS=obj97BF6 STYLE=\"position:absolute; left:2.905in; top:0.833in; width:2.500in\";}\">";
strTest += "<A HREF=\"\" NAME=\"test2\">Test2</A></ SPAN>";
strTest += "<SPAN CLASS=obj97BF8 STYLE=\"position:absolute; left:7.052in; top:0.833in; width:1.645in\";}\">";
strTest += "<A HREF=\"\" NAME=\"test3\">Test3</A></ SPAN>";
String strFinal = "";
char[] charTest = strTest.toCharArray();
int spanCtr = 0;
for( int i = 0, x = charTest.length; i < x; i ++ ) {
if( charTest[ i ] == 'S' && charTest[ i + 1 ] == 'P' && charTest[ i + 2 ] == 'A' && charTest[ i + 3 ] == 'N' ) {
strFinal += "SPAN id=\"id" + ( ++ spanCtr ) + "\"";
i += 3;
}
else {
strFinal += "" + charTest[ i ];
}
}
%>
<html>
<head>
<title>Untitled</title>
</head>
<body>
<%=strFinal%>
</body>
</html>
-----------
-corey
-----------
<%
String strTest = new String();
int intPos = 0;
strTest = "<SPAN CLASS=obj97BF5 STYLE=\"position:absolute;
strTest += "<SPAN CLASS=obj97BF6 STYLE=\"position:absolute;
strTest += "<A HREF=\"\" NAME=\"test1\" >Test1</A></SPAN>";
strTest += "<SPAN CLASS=obj97BF6 STYLE=\"position:absolute;
strTest += "<A HREF=\"\" NAME=\"test2\">Test2</A></
strTest += "<SPAN CLASS=obj97BF8 STYLE=\"position:absolute;
strTest += "<A HREF=\"\" NAME=\"test3\">Test3</A></
String strFinal = "";
char[] charTest = strTest.toCharArray();
int spanCtr = 0;
for( int i = 0, x = charTest.length; i < x; i ++ ) {
if( charTest[ i ] == 'S' && charTest[ i + 1 ] == 'P' && charTest[ i + 2 ] == 'A' && charTest[ i + 3 ] == 'N' ) {
strFinal += "SPAN id=\"id" + ( ++ spanCtr ) + "\"";
i += 3;
}
else {
strFinal += "" + charTest[ i ];
}
}
%>
<html>
<head>
<title>Untitled</title>
</head>
<body>
<%=strFinal%>
</body>
</html>
-----------
-corey
oops, that added it to the closing tags too. Try this:
------------
<%
String strTest = new String();
int intPos = 0;
strTest = "<SPAN CLASS=obj97BF5 STYLE=\"position:absolute; left:0.000in; top:0.000in; height:1.073in\";}\">";
strTest += "<SPAN CLASS=obj97BF6 STYLE=\"position:absolute; left:0.052in; top:0.833in; width:2.771in\";}\">";
strTest += "<A HREF=\"\" NAME=\"test1\" >Test1</A></SPAN>";
strTest += "<SPAN CLASS=obj97BF6 STYLE=\"position:absolute; left:2.905in; top:0.833in; width:2.500in\";}\">";
strTest += "<A HREF=\"\" NAME=\"test2\">Test2</A></ SPAN>";
strTest += "<SPAN CLASS=obj97BF8 STYLE=\"position:absolute; left:7.052in; top:0.833in; width:1.645in\";}\">";
strTest += "<A HREF=\"\" NAME=\"test3\">Test3</A></ SPAN>";
String strFinal = "";
char[] charTest = strTest.toCharArray();
int spanCtr = 0;
for( int i = 0, x = charTest.length; i < x; i ++ ) {
if( charTest[ i ] == '<' &&
charTest[ i + 1 ] == 'S' &&
charTest[ i + 2 ] == 'P' &&
charTest[ i + 3 ] == 'A' &&
charTest[ i + 4 ] == 'N' ) {
strFinal += "<SPAN id=\"id" + ( ++ spanCtr ) + "\"";
i += 4;
}
else {
strFinal += "" + charTest[ i ];
}
}
%>
<html>
<head>
<title>Untitled</title>
</head>
<body>
<%=strFinal%>
</body>
</html>
------------
-corey
------------
<%
String strTest = new String();
int intPos = 0;
strTest = "<SPAN CLASS=obj97BF5 STYLE=\"position:absolute;
strTest += "<SPAN CLASS=obj97BF6 STYLE=\"position:absolute;
strTest += "<A HREF=\"\" NAME=\"test1\" >Test1</A></SPAN>";
strTest += "<SPAN CLASS=obj97BF6 STYLE=\"position:absolute;
strTest += "<A HREF=\"\" NAME=\"test2\">Test2</A></
strTest += "<SPAN CLASS=obj97BF8 STYLE=\"position:absolute;
strTest += "<A HREF=\"\" NAME=\"test3\">Test3</A></
String strFinal = "";
char[] charTest = strTest.toCharArray();
int spanCtr = 0;
for( int i = 0, x = charTest.length; i < x; i ++ ) {
if( charTest[ i ] == '<' &&
charTest[ i + 1 ] == 'S' &&
charTest[ i + 2 ] == 'P' &&
charTest[ i + 3 ] == 'A' &&
charTest[ i + 4 ] == 'N' ) {
strFinal += "<SPAN id=\"id" + ( ++ spanCtr ) + "\"";
i += 4;
}
else {
strFinal += "" + charTest[ i ];
}
}
%>
<html>
<head>
<title>Untitled</title>
</head>
<body>
<%=strFinal%>
</body>
</html>
------------
-corey
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
String strTest = new String();
int intPos = 1;
strTest = "<SPAN id='" + intPos + "' CLASS=obj97BF5 STYLE=\"position:absolute;
intPos++;
strTest += "<SPAN id='" + intPos + "' CLASS=obj97BF6 STYLE=\"position:absolute;
strTest += "<A HREF=\"\" NAME=\"test1\" >Test1</A></SPAN>";
intPos++;
strTest += "<SPAN id='" + intPos + "' CLASS=obj97BF6 STYLE=\"position:absolute;
strTest += "<A HREF=\"\" NAME=\"test2\">Test2</A></
intPos++;
strTest += "<SPAN id='" + intPos + "' CLASS=obj97BF8 STYLE=\"position:absolute;
strTest += "<A HREF=\"\" NAME=\"test3\">Test3</A></
strTest += "</SPAN>";
%>