469,271 Members | 1,313 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

How to escape special characters through JAVA

For eg:

Expand|Select|Wrap|Line Numbers
  1.  public String DisplayinURLFormat(String sampleString,int urlIndex)
  2.     {
  3.           String tempString = "";
  4.              int beginIndex = sampleString.indexOf("http", urlIndex); 
  5.              if(beginIndex!=-1)
  6.              {
  7.                  int endIndex = sampleString.indexOf(' ', beginIndex);
  8.                  if(endIndex==-1)
  9.                  {
  10.                      endIndex = sampleString.length();
  11.                  }
  12.                  String hyperString = sampleString.substring(beginIndex,endIndex);
  13.                  tempString = sampleString.substring(0, beginIndex)+"<a href='" + hyperString + "'>"+hyperString+"</a>" + sampleString.substring( endIndex,sampleString.length());
  14.                  urlIndex = endIndex + hyperString.length() + 13;
  15.                  sampleString= tempString;
  16.                  return DisplayinURLFormat(sampleString,urlIndex);  
  17.              }
  18.              else
  19.                  return sampleString;
  20.     }
  21.  
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
Jul 19 '07 #1
8 15081
prometheuzz
197 Expert 100+
...

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.
Jul 19 '07 #2
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
Jul 19 '07 #3
prometheuzz
197 Expert 100+
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 "aaaaaaaabbbbbbbbbbbbbbssssssssssssssssssbbbb" comes in, and "aaaaaaaabbbXXXXXXXbbsssssXXXXXssssssssbbb" should come out.
Jul 19 '07 #4
"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
Jul 19 '07 #5
but the output should be

"go to http://www.google.com "
Jul 19 '07 #6
prometheuzz
197 Expert 100+
but the output should be

"go to http://www.google.com "
Output to what? Are we talking about Java here?
Jul 19 '07 #7
prometheuzz
197 Expert 100+
"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.
Jul 19 '07 #8
JosAH
11,448 Expert 8TB
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 ;-)
Jul 19 '07 #9

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Barry Olly | last post: by
5 posts views Thread by Doc | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.