过滤输入字符串

发布于 2024-11-07 05:30:58 字数 111 浏览 0 评论 0原文

当我将数据插入表中时,我遇到了过滤字符串的问题。我的意思是像 ' < 等特殊字符,我可以进行简单的替换,但我需要保存原始的字符序列。提前感谢您的帮助。

I faced with problem filtering string when i insert data into my table. I mean special characters like ' < etc i can do simple replace but i need to save the original sequence of characters. Thanks for help in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

⒈起吃苦の倖褔 2024-11-14 05:30:58

如果您希望通过代码更改字符串,您可以指定要从字符串中删除的字符数组。 String.Join 可以使用 split 方法删除这些字符并将其重新组合在一起

    string s = "He<llo;< I -am a $stri-ng;";

    // Add other characters that you deem unacceptable
    char[] invalidChars = { ';', '<', '>', '-' };
    s = String.Join("", s.Split(invalidChars));

就保存原始序列而言,这是非常模糊的,所以我不确定你的意思,但我想到的是使用数据库并创建第二列,以便您有一列用于输入他们输入的内容,另一列用于固定值。我的脑海中突然浮现出两种方法来控制这个

  1. 数据库触发器 -
    设计一些功能来剥离字符并保存到
    固定值列。这样做的风险是,如果有人决定键入一个您尚未捕获的字符,您不仅会修改该函数,而且还必须针对
  2. 计算列规范 - 滑动的任何记录运行此函数
    根据要剥离的函数创建表达式
    字符并让列控制这个值
    这足以消除通过触发器增加的风险。当您修改函数时,因为这是一个计算列,理论上它会更新该值。 (如果我在这里错了,请纠正我,未经测试,只是理论上)

可能不是最有效的方法,但这是我将在当前的工具集中使用的方法。

If you're looking to change the string through code you could specify an array of characters to strip from the string. The String.Join can use the split method to remove those characters and put it back together

    string s = "He<llo;< I -am a $stri-ng;";

    // Add other characters that you deem unacceptable
    char[] invalidChars = { ';', '<', '>', '-' };
    s = String.Join("", s.Split(invalidChars));

In terms of saving the original sequence, this is very vague so I'm not sure what you mean but what pops into my mind is to use the database and create a second column so that you have one for what they entered in, and another for the fixed value. Two ways pop into my head to control this

  1. Database Trigger -
    Design some function to strip away characters and save to the
    fixed value column. The risk in this is that if someone decides to key a character you have not trapped yet you would not only modify the function but have to run this function against any record that slipped by

  2. Computed Column Specification -
    Create an expression based on the function to strip off
    characters and let the column control this value
    This is sufficient enough to defeat the risk added through the trigger. When you modify the function since this is a computed column it would in theory update the value. (Correct me if I am wrong here, not tested, just in theory)

Probably not the most efficient approaches but this is what I would use with my current toolset.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文