简体   繁体   English

string.replace每个',除了第一个和最后一个

[英]string.replace every ' except the first and last

I have a project where I call a MySQL query, which is built up from a string I send to the method. 我有一个项目,在这里我调用一个MySQL查询,该查询是由发送到该方法的字符串构建的。

I have a problem if the string has a single quote ' 如果字符串中有单引号'

I want to replace it with a \\' so it's safe for MySQL 我想将其替换为\\'这样对于MySQL来说是安全的

But when I build up the query I add a single quote and a % in the beginning and the end of the string; 但是,当我建立查询时,我在字符串的开头和结尾添加了一个单引号和一个% for example '%texthere%' 例如'%texthere%'

So I need to know how do I replace every single quote except the first and last one? 因此,我需要知道如何替换除第一个和最后一个引号之外的所有单引号?

So it will replace only from: '%thi'stext%' 因此它将仅替换为: '%thi'stext%'

To this: '%thi\\'stext'% 为此: '%thi\\'stext'%

Simple: 简单:

int first = s.IndexOf('\'');
int last = s.LastIndexOf('\'');
string prefix = s.Substring(0, first+1);
string query = s.Substring(first+1, last-first-1);
string suffix = s.Substring(last);
s = prefix + query.Replace("'", "\\'") + suffix;

Of course, you should really use SQL parameters instead. 当然,您实际上应该使用SQL参数来代替。

so stupid me i finally looked over the method alot better 太愚蠢了,我终于更好地研究了该方法

i fixed it by string.Replace("'", "\\'") 我通过string.Replace("'", "\\'")修复了它

BEFORE i add a % & ' the to the beginning and % & ' at the end 在我将%'添加到开头并将%'到结尾之前

i learned alot from you guys, thanks so much 我从你们那里学到了很多东西,非常感谢

First use a trim function to cut single quotes from each side of the string. 首先使用修剪功能从字符串的每一边截去单引号。 Then replace "'" with "\\'" and add the single quotes back to each side of the string. 然后将“'”替换为“ \\'”,并将单引号添加回字符串的每一侧。 This would not work if ' was the first or last character in the string because it would be trimmed. 如果'是字符串中的第一个或最后一个字符,则此方法将不起作用,因为它将被修剪。

The way I have done this before is as follows: 我之前完成此操作的方式如下:

  1. Use IndexOf to get the index of the first occurrence of the character. 使用IndexOf可以获取字符首次出现的索引。
  2. Get a substring of the original string starting at the index found in Step 1 从步骤1中找到的索引处获取原始字符串的子字符串
  3. Reverse the substring found in Step 2 反转在步骤2中找到的子字符串
  4. Repeat Step 1 on the reversed string from Step 3, and you are now left with a substring containing the string from after the first ' character to before the last one. 在步骤3的反向字符串上重复步骤1,现在剩下的子字符串包含从第一个'字符之后到最后一个字符之前的字符串。
  5. Reverse the substring from Step 4 again (to get it back to original order) and replace the remaining ' characters with \\' 再次反转步骤4中的子字符串(以使其恢复原始顺序),然​​后将其余的'字符替换为\\'
  6. Rebuild the string using (pseudoCode) - 使用(pseudoCode)重建字符串-

    finalString = string.Concat( originalString.Substring(0, firstIndex), replacedSubstringFromStep5, originalString.Substring(secondIndex, originalStringLength - 1) finalString = string.Concat(originalString.Substring(0,firstIndex),replacedSubstringFromStep5,originalString.Substring(secondIndex,originalStringLength-1)

This will get you a string with anything before and including the first ' character and anything after the last one from the original string, and everything in between will be the replaced ' characters that are now escaped for your MySQL query. 这将使您得到一个字符串,字符串中包含第一个'字符之前和之后的内容,以及原始字符串中最后一个'之后的内容,并且介于两者之间的所有内容都将是替换的'字符,这些字符现在将为您的MySQL查询转义。

Hope this helps! 希望这可以帮助!

Edit: note when I did this, it was for string manipulation in an interlinking application, and not for database queries. 编辑:请注意,当我这样做时,它是用于互连应用程序中的字符串操作,而不是用于数据库查询。 I do agree it is better to use stored procs and parameters as others have stated; 我确实同意,最好像其他人所述使用存储的proc和参数。 however, the solution I have provided, without getting into best practices and such, will do what OP is seeking. 但是,我所提供的解决方案将不执行最佳实践,而是将执行OP所追求的目标。

Reffer bellow link 波纹管链接

Strip double quotes from a string in .NET 从.NET中的字符串中去除双引号

Replace all double quotes withing String 用String替换所有双引号

Hope this one is help you if not get solution than tell me i will give another solution 希望这对您有帮助,如果不能解决,请告诉我我会给您另一种解决方案

EDIT Okey so try string like this and give feed back 编辑 Okey,所以尝试这样的字符串并反馈

string query=@"%thi'stext%";

I hope this method helps you: Assuming raw matches the pattern "'%free text here with single quotes%'" 我希望这种方法对您有帮助:假设raw与模式“'%free text here with单引号%'”匹配

    private string ReplaceSpecial(string raw)
    {
        return String.Format("'%{0}%'", raw.Substring(2, raw.Length - 2).Replace("'", "\\'"));
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM