简体   繁体   English

如何在 string.replace 上使用字符串插值

[英]How to use string interpolation on string.replace

I created a method and would like to know how to use string interpolation with string.replace inside my method.我创建了一个方法,想知道如何在我的方法中使用带有 string.replace 的字符串插值。

I looked through several how to's on Stack Overflow on its use but not with replace.我查看了 Stack Overflow 上的几个使用方法,但没有替换。 I would like some guidance on how to accomplish this.我想要一些关于如何实现这一点的指导。

 private string CheckForEscapeStringInQuotesAndTrim(string csvDataValue)
 {
     if (Regex.IsMatch(csvDataValue, "[,\"\\r\\n]"))
     {
         return "\"" + csvDataValue.Replace("\"", "\"\"") + "\"";
     }
     //trim any unnecessary whitespace
     return csvDataValue.Trim();
 }

You could simply use csvDataValue.Replace("\\"", "\\"\\"") as an interpolated expression it will be resolved.您可以简单地使用csvDataValue.Replace("\\"", "\\"\\"")作为内插表达式,它将被解析。

private string CheckForEscapeStringInQuotesAndTrim(string csvDataValue)
{
    if (Regex.IsMatch(csvDataValue, "[,\"\\r\\n]"))
    {
        return $"\"{csvDataValue.Replace("\"", "\"\"")}\"";
    }
    //trim any unnecessary whitespace
    return csvDataValue.Trim();
}

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

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