无法从字符串中删除特殊字符 [英] Unable to remove a special character from string

查看:44
本文介绍了无法从字符串中删除特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景

有一个应用程序要求用户输入将存储在数据库中的信息.然后我有一个每 5 分钟运行一次的应用程序,并获取用户使用前一个应用程序输入的信息.然后我的应用程序从数据库中获取所有信息,然后继续创建给定的文档,然后将其放置在服务器中供用户获取.然而,用户开始遇到特定文档的问题,其中某些功能没有正确执行.所以我确定问题是用户在输入应用程序中输入的字符串,在标题栏中他们有Jame's Bond Story",所以我的应用程序创建了文档并且没有任何问题.所以在调试之后我发现了以下问题.

There is an application where users are required to enter information that will be stored in a DB. I then have an application that runs every 5 minute and gets the information that was entered by the user using the previous application. My app then grabs all the information from the database and then proceed to do create the given document and then places it in a server for the user to get. However users started having issues with a specific document, where certain functionalities were not executing correctly. So I identified the issue as being the string which a user entered in the entry application, in the title column they had "Jame's Bond Story" so my application creates the document and does not have any issue what so ever. So after debugging I identified the following problem.

问题

不确定特定用户是如何做他所做的,但单引号 ' 并不是真正的单引号,而是某种其他类型的奇怪字符异常.我通过运行以下代码来证明这一点,看看我是否可以将其删除.

Not sure how the specific user did what he did but the single quote ' was not really a single quote but some other type of weird character anomaly. I proved this by running the following code to see if I can remove it.

 string cleanTitle = BookRec.TitleName.Replace("'","");

然而,这对我根本不起作用.然后我将字符串分解成一个字符数组,而不是得到一个字符,我得到了一个奇怪的数字.然后我继续使用这个正则表达式代码来清理每个字符,只允许数字和字母.

However this did not work for me at all. I then broke the string into a character array and instead of getting the character I got a weird digit. So then I proceeded into using this regex code to clean every character and only allow numbers and letters.

string cleanTitle = Regex.Replace(BookRec.TitleName, "[^\\w\\. _]", "");

这现在已经成为一个问题,因为用户希望标题包含以下特殊字符 ( ) _ , - .

This has now become an issue because the users want the Title to contain special the following characters ( ) _ , - .

我正在寻找一种方法来过滤掉任何字符,包括我本周遇到的类型,并且只允许用户同意的 6 个字符.我可以使用以下正则表达式,但我得到的是一个空字符串.

I am looking for a way to to filter out any characters including the type I ran into this week and only allow the 6 characters which the users have agreed to. I can up with the following regex formula bu I am getting an empty string.

Regex fomrula = new Regex(@"^[a-zA-Z0-9_\[])(,\-.'");

但是,当我替换标题时,我得到了一个空字符串.我不是正则表达式的忠实粉丝,我也愿意使用子字符串方法来解决这个问题.

However I am getting an empty string when I am replacing the title. I am not a big fan of regex, I am also open to a a sub string approach to this as well.

附加信息

我无法访问将信息插入给定数据库的应用程序.我只能从数据库中读取数据,然后执行操作.

I am not able to access the application that inserts the information to the given database. I am only able to read from the database and then preform actions.

推荐答案

你可能想尝试这样的事情:

You may want to try something like this:

string cleanTitle = Regex.Replace(BookRec.TitleName, @"[^\u0000-\u007F]+", "");

这将替换不在这些值之间的任何 Unicode 字符.我不确定这些是否是导致您出现问题的原因,但希望它可以为您提供正确方向的提示.

This will replace any Unicode character that is not between those values. I'm not sure if those are the ones that are causing you problems but hopefully it may give you a hint in the right direction.

这篇关于无法从字符串中删除特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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