473,396 Members | 2,030 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 15321
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

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

Similar topics

3
by: Barry Olly | last post by:
Hi, I'm working on a mini content management system and need help with dealing with special characters. The input are taken from html form which are then stored into a varchar column in...
4
by: Ewok | last post by:
let me just say. it's not by choice but im dealing with a .net web app (top down approach with VB and a MySQL database) sigh..... Anyhow, I've just about got all the kinks worked out but I am...
17
by: Carl Mercier | last post by:
Hi, Is it possible to use special characters like \n or \t in a VB.NET string, just like in C#? My guess is NO, but maybe there's something I don't know. If it's not possible, does anybody...
8
by: david.lindsay.green | last post by:
Hello all, I am quite new a web scripting and making web pages in general and I have stumbled across a problem I have as yet been unable to solve. I am trying to take the contents of a textarea box...
0
by: SeanGerman | last post by:
Howdy, Is there a way to explicitly escape special characters (particularly & (&amp;) and ' (&apos;)) when writing a data set with the WriteXml method? I know this is supposed to happen...
5
by: Doc | last post by:
Hello! I'm experiencing a little problem counting the number of characters in a textarea on a html page. This is the content type of my HTML document content="text/html; charset=iso-8859-1" ...
25
by: Wim Cossement | last post by:
Hello, I was wondering if there are a few good pages and/or examples on how to process form data correctly for putting it in a MySQL DB. Since I'm not used to using PHP a lot, I already found...
3
by: forest demon | last post by:
i need to escape characters, such as ( % and/or _ and/or [ ), since i have database entries that have such characters in them. the following piece works fine in an SQL statement (c#), but...
3
KevinADC
by: KevinADC | last post by:
Purpose The purpose of this article is to discuss the difference between characters inside a character class and outside a character class and some special characters inside a character class....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.