字符串转换功能不起作用 [英] String converts function not working

查看:105
本文介绍了字符串转换功能不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上我需要脚本(文本框)只接受一些特殊符号('/<>),我从网上复制这个代码它不起作用,但它如何在后端工作。这段代码的确切含​​义是什么。



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.

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







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;
    }

推荐答案

1。您枚举的字符在Web应用程序中是特殊的,因为它们用于创建HTML标记,并使用特殊的Web编码代码来管理它们。详情请见:

http://demo.nickname.net/demo/testpak/ encode.pl [ ^ ]



2.您提供的代码是使用他们的代码(来自上面的链接)搜索特殊字符,然后用真实字符替换它们(解码它们) 。
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).


参见 http://msdn.microsoft.com/en-us/library/system.web.httputility.htmldecode(v = vs.110).aspx [ ^ ]



如果你坚持自己编写,那么至少要删除测试( if s),因为他们所做的只是浪费周期。





相当于一个字符串到当前字符串,除了oldValue的所有实例都替换为newValue。 如果在当前实例中找不到oldValue,则该方法返回当前实例不变。

- String.Replace Method(String,String) [ ^ ]
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)[^]


你遇到的主要问题有的是它遍历字符串至少 n (要替换的字符串数)次(如果没有找到任何字符串),可能多达 2n 次(如果找到了所有字符串)。只需移除<$ c $即可将其减少到 n 次c> if s。



但这是一项真正不需要遍历字符串的任务。以下方法构建一个 RegularExpression ,它搜索你想要替换的字符串,查找匹配项,然后构造一个新字符串,如果有的话匹配。



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.

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





因为要搜索的字符串用于RegularExpression,这种技术更强大,但需要特别注意提供的值。





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.

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 ) ;
}


这篇关于字符串转换功能不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆