替换“”(双引号)内的定界符(|)

Replace delimiter( | ) inside ""(Double Quotes)

我必须删除双引号 (") 内的 | 并替换为 -

示例:

实际:

("Some Txt | www.google.com")

预期:

("Some Txt - www.google.com")

您可以使用字符串替换方法。

https://docs.microsoft.com/en-us/dotnet/api/system.string.replace?view=net-5.0

string str = "Some Txt | www.google.com";
string result = str.Replace('|', '-');
 var newString = ("Some Txt | www.google.com").Replace("|", "-");

最好的办法是使用内置替换方法 (Microsoft documentation)。

string result = "Some Txt | www.google.com".Replace('|', '-');

编码愉快 :)

如果您只想替换 |与-,如果字符在双引号内,你可以使用这种方法:

private static string ReplaceWithinQuotes(string input, char from, char to)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append(input);
        bool withinQuotes = false;
        for(int i = 0; i < input.Length; i++)
        {
            if(input[i] == '\"')
            {
                withinQuotes = !withinQuotes;
            }
            if(withinQuotes && input[i] == from)
            {
                sb[i] = to;
            }
        }
        return sb.ToString();
    }

该方法首先将字符串复制到字符串生成器中。然后它遍历字符串并检查我们是否在引号内。状态存储在 bool 中。如果我们在引号内,则在字符串生成器中替换字符。

在线演示:https://dotnetfiddle.net/hdlu4U

您可以简单地使用派生列和函数 REPLACE() :

REPLACE(YourColumn, "|", "-")
REPLACE("Some Txt | www.google.com", "|", "-")