CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Guest

    HTML - Escaping string values

    Is there a utility function that I can use to escape string values so that they can be used as values in an HTML form?

    E.g: "Barnes & Nobles" would be used as the value of an HTML TextArea

    <TEXTAREA NAME="txt">Barnes & Nobles</TEXTAREA>

    Java provides a function to encode URLs but I could't find a similar function that can be used to encode form contents.


    Thanks,

    Frederic


  2. #2
    Join Date
    Nov 1999
    Location
    Tilburg-Breda, Noord-Brabant, The Netherlands
    Posts
    135

    Re: HTML - Escaping string values

    Hi there,

    I don't exactly know what you mean but I guess that you mean that you want to set the values on an returned or an existing HTML form. Can you explain in which context? Do you do this with servlets? From an applet running on the same page? Just be a little more specific.
    I might be able to help you.

    Greets,
    Jan Meeuwesen


  3. #3
    Guest

    Re: HTML - Escaping string values

    Hi Jan,

    Yes, I would use the strings to generate the contents of an HTML page or set initial form values that might contain reserved HTML characters.

    All I want to do is to be able to replace the reserved HTML characters with their corresponding character entities.

    E.g.: The following will not display propery

    <TEXTAREA>
    The TEXTAREA element indicates a multi-line text entry field.
    The contents between the beginning <TEXTAREA> and end </TEXTAREA> tags represent the initial contents of the field in the browser.
    </TEXTAREA>

    ...because the end tag in the content needs to be escaped.

    or suppose that I want to display fragments of HTML source within an HTML page. You'd need to escape nearly all the character.

    I.e.: "&" would be translated into "&" if I wanted to display the the html fragment as source.

    I would only need to take care of the important characters: '<', '>', '"' and '&' but I'd rather not write my own utility when there is something thats already available.

    Hope this clears things up.

    Frederic



  4. #4
    Join Date
    Nov 1999
    Location
    Tilburg-Breda, Noord-Brabant, The Netherlands
    Posts
    135

    Re: HTML - Escaping string values

    Hi,

    I'm very sorry to disappoint you (and this probably is, because my English is (assumingly) not so good) but I'm not certain what the main problem is.

    for example if this is the line of HTML code you want to write to the HTML file:
    <B>This is a line of "Bold" text! And this are some escape characters: < > " & .</B>

    You just make a method that accepts a String and returns the string with escape characters for example:

    public class EscapeCharacters {

    private String ConvertToEscapeCharacters(String Temp) {
    String ReturnValue = new String();
    ReturnValue = "";
    int i = 1;
    while (i<=Temp.length()) {
    if ((Temp.substring(i-1,i)).equals("<"))
    ReturnValue = ReturnValue + "<";
    else if ((Temp.substring(i-1,i)).equals(">"))
    ReturnValue = ReturnValue + ">";
    else if ((Temp.substring(i-1,i)).equals("\""))
    ReturnValue = ReturnValue + """;
    else if ((Temp.substring(i-1,i)).equals("&"))
    ReturnValue = ReturnValue + "&";

    else {
    ReturnValue = ReturnValue + Temp.substring(i-1,i);
    }
    i++;
    }

    return ReturnValue;
    }

    public static void main(String[] args) {
    //The following line will be converted.
    //<B>This is a line of "Bold" text! And this are some escape characters: < > " & .</B>
    EscapeCharacters MyEC = new EscapeCharacters();
    String Temp = new String();
    Temp = MyEC.ConvertToEscapeCharacters("<B>This is a line of \"Bold\" text! And this are some escape characters: < > \" & .</B>");
    System.out.println(Temp);
    }

    }




    The Output will be:
    <B>This is a line of "Bold" text! And this are some escape characters: < > " & .</B>

    You see... It's pretty easy to make your own converter!!!
    The only thing that could be a little difficult is that I use this String :
    "<B>This is a line of \"Bold\" text! And this are some escape characters: < > \" & .</B>
    Instead of:
    "<B>This is a line of "Bold" text! And this are some escape characters: < > " & .</B>

    Because in java \" is an escape character for "

    I hope this is of help to you,

    Greets,
    Jan Meeuwesen.
    (Don't forget to vote)




  5. #5
    Guest

    Re: HTML - Escaping string values

    Hi,

    That's exactly it. Except that I would have expected that such a utility exists already and that I could skip on future maintenance. But, in the end you end up having to right your own code anyway... which I did.


    Thanks,

    Frederic




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured