Solved

How to insert some text into the string ?

Posted on 2002-04-19
14
179 Views
Last Modified: 2010-04-01
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.
0
Comment
Question by:tanc02
  • 9
  • 2
  • 2
  • +1
14 Comments
 
LVL 33

Expert Comment

by:knightEknight
ID: 6954759
<%
 String strTest = new String();
 int intPos = 1;
 
 strTest = "<SPAN id='" + intPos + "' CLASS=obj97BF5 STYLE=\"position:absolute; left:0.000in; top:0.000in; height:1.073in\";}\">";
 intPos++;
 strTest += "<SPAN id='" + intPos + "' CLASS=obj97BF6 STYLE=\"position:absolute; left:0.052in; top:0.833in; width:2.771in\";}\">";
 strTest += "<A HREF=\"\" NAME=\"test1\" >Test1</A></SPAN>";
 intPos++;
 strTest += "<SPAN id='" + intPos + "' CLASS=obj97BF6 STYLE=\"position:absolute; left:2.905in; top:0.833in; width:2.500in\";}\">";
 strTest += "<A HREF=\"\" NAME=\"test2\">Test2</A></SPAN>";
 intPos++;
 strTest += "<SPAN 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>";
%>
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 6954765
oops, everywhere you see this:


   "<SPAN id='" + intPos + "'


change it to this:


   "<SPAN id='id" + intPos + "'


0
 
LVL 1

Author Comment

by:tanc02
ID: 6954766
I don't want to do that.

I want to scan the string and use substring method to
insert id=idx.
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 6954768
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>";
%>

0
 
LVL 33

Expert Comment

by:knightEknight
ID: 6954770
ooooh, stand by
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 6954783
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+srch.length()),repl);
        ndx = buffer.toString().indexOf(srch);
    }

    return buffer.toString();
 }

//then do this:

strTest = strReplace( strTest, "<SPAN ", "<SPAN id='"+nextPos()+"' " );

%>
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 6954788
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?
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 19

Expert Comment

by:cheekycj
ID: 6954789
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>";
 }
%>
0
 
LVL 1

Author Comment

by:tanc02
ID: 6954791
I got this error :

org.apache.jasper.JasperException: 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\examples\t$jsp.java:39: Identifier expected.
        strTest = strReplace(strTest, "<SPAN ", "<SPAN id='"+nextPos()+"' " );
               ^
1 error
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 6954814
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+srch.length()),repl+String.valueOf(++intPos)+"' ");
        ndx = buffer.toString().indexOf(srch);
    }

    return buffer.toString();
 }

strTest = addID(strTest);
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 6954824
doooooh!

change this:

   ndx = buffer.toString().indexOf(srch);

to this:

   ndx = buffer.toString().indexOf(srch,ndx+1);
0
 
LVL 2

Expert Comment

by:coreyit
ID: 6954827
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
0
 
LVL 2

Expert Comment

by:coreyit
ID: 6954838
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
0
 
LVL 33

Accepted Solution

by:
knightEknight earned 100 total points
ID: 6954950
another small typo corrected, but I am reposting just in case:

String addID( String str )
{
   if ( str==null )
      return str;

   String srch = "<SPAN ";
   String repl = srch + "id='id";

   int ndx = str.indexOf(srch);
   int intPos = 0;

   StringBuffer buffer = new StringBuffer(str);

   while ( ndx >= 0 )
   {
       buffer.replace(ndx,(ndx+srch.length()),repl+String.valueOf(++intPos)+"' ");
       ndx = buffer.toString().indexOf(srch,ndx+1);
   }

   return buffer.toString();
}
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

In this article, I show you step by step with screenshots to assist you - HOW TO: Deploy and Install the VMware vCenter Server Appliance 6.5 (VCSA 6.5), with some helpful tips along the way.
Is your company's data protection keeping pace with virtualization? Here are 7 dynamic ways to adapt to rapid breakthroughs in technology.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now