How to escape special characters through JAVA

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ikilldeath
    New Member
    • Jul 2007
    • 14

    How to escape special characters through JAVA

    For eg:

    [code=java]
    public String DisplayinURLFor mat(String sampleString,in t urlIndex)
    {
    String tempString = "";
    int beginIndex = sampleString.in dexOf("http", urlIndex);
    if(beginIndex!=-1)
    {
    int endIndex = sampleString.in dexOf(' ', beginIndex);
    if(endIndex==-1)
    {
    endIndex = sampleString.le ngth();
    }
    String hyperString = sampleString.su bstring(beginIn dex,endIndex);
    tempString = sampleString.su bstring(0, beginIndex)+"<a href='" + hyperString + "'>"+hyperStrin g+"</a>" + sampleString.su bstring( endIndex,sample String.length() );
    urlIndex = endIndex + hyperString.len gth() + 13;
    sampleString= tempString;
    return DisplayinURLFor mat(sampleStrin g,urlIndex);
    }
    else
    return sampleString;
    }
    [/code]

    Here i am making a string to URl if the string containd any http format.

    like if i enter sampleStruing as "go to http://www.google.com" the http string shud appear in URl. but for this i need to use the escapeXml flag in HTML to ignore the conversion of < and > characters. Is there any other way to make the escaped in JAV code itself
    Last edited by JosAH; Jul 19 '07, 01:19 PM. Reason: added [code] ... [/code] tags
  • prometheuzz
    Recognized Expert New Member
    • Apr 2007
    • 197

    #2
    Originally posted by ikilldeath
    ...

    like if i enter sampleStruing as "go to http://www.google.com" the http string shud appear in URl. but for this i need to use the escapeXml flag in HTML to ignore the conversion of < and > characters. Is there any other way to make the escaped in JAV code itself
    Err, I'm no XML/web man myself, so I don't know exactly what this kind of escape thing is you're talking about.
    However, could you perhaps give a couple of example input-Strings and desired output-String? Perhaps then I can give you a hand.

    Comment

    • ikilldeath
      New Member
      • Jul 2007
      • 14

      #3
      OK, I have a string sampleString which contains a substring which is supposed to be printed in URL format, for that i have written the above function.

      But anyhow i needed to use the escapeXml flag to set it to false in the jsp page not to convert the <a ref> tag to &lt;a ref &gt;.
      The problem doing this is it may escape some of the valid tags which i dint want to escape. Here i want only <a ref></a> tags to be escaped

      Comment

      • prometheuzz
        Recognized Expert New Member
        • Apr 2007
        • 197

        #4
        Originally posted by ikilldeath
        OK, I have a string sampleString which contains a substring which is supposed to be printed in URL format, for that i have written the above function.

        But anyhow i needed to use the escapeXml flag to set it to false in the jsp page not to convert the <a ref> tag to &lt;a ref &gt;.
        The problem doing this is it may escape some of the valid tags which i dint want to escape. Here i want only <a ref></a> tags to be escaped
        Could you provide examples of in- and output?
        For example: String "aaaaaaaabbbbbb bbbbbbbbsssssss sssssssssssbbbb " comes in, and "aaaaaaaabbbXXX XXXXbbsssssXXXX Xssssssssbbb" should come out.

        Comment

        • ikilldeath
          New Member
          • Jul 2007
          • 14

          #5
          "go to <a href='http://www.google.com' ></a>" is input

          "go to &lt;a href=&#039;http ://www.google.com& #039;&gt; &lt;/a&gt;" is the out put

          Comment

          • ikilldeath
            New Member
            • Jul 2007
            • 14

            #6
            but the output should be

            "go to http://www.google.com "

            Comment

            • prometheuzz
              Recognized Expert New Member
              • Apr 2007
              • 197

              #7
              Originally posted by ikilldeath
              but the output should be

              "go to http://www.google.com "
              Output to what? Are we talking about Java here?

              Comment

              • prometheuzz
                Recognized Expert New Member
                • Apr 2007
                • 197

                #8
                Originally posted by ikilldeath
                "go to <a href='http://www.google.com' ></a>" is input

                "go to &lt;a href='http://www.google.com' &gt; &lt;/a&gt;" is the out put
                Look at String's replaceAll(...) method.

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  Originally posted by prometheuzz
                  Look at String's replaceAll(...) method.
                  Yeah, regexps! Gimme an 'r'! gimme an 'e'! gimme a 'g'! gimme an 'e'! ... erm,
                  sorry; it won't happen again.

                  kind regards,

                  Jos ;-)

                  Comment

                  Working...