Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Actually i need the Script(Text Box) to accept only some special symbols(' / < >), i copied this code from online it is not working, but How it works on back end. What is the exact meaning of this code.

C#
string s = txtScript.Text;
        s = converts(s);
        string ss = "<p>";
        ss += "</p>";
        txtScript.Text = s;
        ss = converts(ss);


C#
private static string converts(string s)
    {
        if (s.Contains("&#39;"))
        {
            s = s.Replace("&#39;", "'");
        }
        if (s.Contains("&#10;"))
        {
            s = s.Replace("&#10;", "<");
        }
        if (s.Contains("&#25;"))
        {
            s = s.Replace("&#25;", ">");
        }
        if (s.Contains("&#58;"))
        {
            s = s.Replace("&#58;", "/");
        }

        if (s.Contains("="))
        {
            s = s.Replace("=", "e");
        }
        if (s.Contains(";"))
        {
            s = s.Replace(";", "q");
        }
        if (s.Contains(":"))
        {
            s = s.Replace(":", "d");
        }
        return s;
    }
Posted
Updated 8-Dec-14 8:23am
v2
Comments
Praveen Kumar Upadhyay 8-Dec-14 1:43am    
Your string contains some kind of encoding(I am not sure what encoding it is) and converts function is returning the decoded value of it. This will work with only few special characters only as it is defined in the code.
CP_vicky 8-Dec-14 1:53am    
Thank you Praveen, but it is not working. what ever the special symbols i give that value is storing in the data base. How to give exception message here.
PIEBALDconsult 8-Dec-14 14:24pm    
That is some very bad code; put it back where you found it and wash your hands.

1. The chars enumerated by you are special in the web application because they are used to create HTML tags and special web encoded codes are used to manage them. See details here:
http://demo.nickname.net/demo/testpak/encode.pl[^]

2.The code behind provided by you, is searching for special chars by using their codes (from the link above) then replace them with the real chars (decode them).
 
Share this answer
 
Comments
CP_vicky 8-Dec-14 2:03am    
My goodness. Fantastic Tool and Answer. Thank you very much Raul Iloc
Raul Iloc 8-Dec-14 2:09am    
Welcome, I am glad that I could help you!
See http://msdn.microsoft.com/en-us/library/system.web.httputility.htmldecode(v=vs.110).aspx[^]

If you insist on writing your own, then at least remove the tests (ifs), because all they do is waste cycles.

"
A string that is equivalent to the current string except that all instances of oldValue are replaced with newValue. If oldValue is not found in the current instance, the method returns the current instance unchanged.
" -- String.Replace Method (String, String)[^]
 
Share this answer
 
v3
Comments
CP_vicky 8-Dec-14 2:10am    
Thank you PIEBALDconsult
The main problem with what you have is that it traverses the string at least n (the number of strings to replace) times (if none of the strings are found), and perhaps as many as 2n times (if all of the strings are found). This can be reduced to n times simply by removing the ifs.

But it's a task that really doesn't require more than one traversal of the string. The following method builds a RegularExpression that searches for the strings you want to replace, looks for matches, then constructs a new string if there were matches.

C#
somestring.RegexReplace
(
  new System.Collections.Generic.Dictionary<string,string>()
  {
    { "&#39;" , "'" }
  ,
    { "&#10;" , "<" }
  ...
  }
) ;


Because the strings be searched for are used in a RegularExpression, this technique is more powerful, but will require careful attention to the values provided.

C#
public static string
RegexReplace
(
    this string                                          Subject
,
    System.Collections.Generic.Dictionary<string,string> Replacement
)
{
  if ( !System.String.IsNullOrEmpty ( Subject ) && ( Replacement != null ) && ( Replacement.Count > 0 ) )
  {
    System.Text.StringBuilder workarea =
      new System.Text.StringBuilder ( Subject.Length ) ;

    foreach ( string s in Replacement.Keys )
    {
      workarea.AppendFormat ( "({0})|" , s ) ;
    }

    workarea.Length-- ;

    System.Text.RegularExpressions.Regex reg =
      new System.Text.RegularExpressions.Regex ( workarea.ToString() ) ;

    System.Text.RegularExpressions.MatchCollection mat =
      reg.Matches ( Subject ) ;

    if ( mat.Count > 0 )
    {
      workarea.Length = 0 ;

      int offset = 0 ;

      for ( int i = 0 ; i < mat.Count ; i++ )
      {
        workarea.Append ( Subject.Substring ( offset , mat [ i ].Index - offset ) ) ;

        workarea.Append ( Replacement [ mat [ i ].Value ] ) ;

        offset = mat [ i ].Index + mat [ i ].Length ;
      }

      workarea.Append ( Subject.Substring ( offset ) ) ;

      Subject = workarea.ToString() ;
    }
  }

  return ( Subject ) ;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900