在值之间分割和附加“与”

时间:2019-01-08 04:01:39

标签: c# split

如何在值以下分割并在值之间附加AND? 我无法与空格分开,因为单词之间有空格

"\"Mark John\" \"Tina Roy\""
as
"\"Mark John\" AND \"Tina Roy\""

最后应该看起来像-

"Mark John" AND "Tina Roy"

感谢您的帮助。

string operatorValue = " AND ";

if (!string.IsNullOrEmpty(operatorValue))
{
    foreach (string searchVal in SearchRequest.Text.Split(' '))
    {
        if (!string.IsNullOrEmpty(searchVal))
            searchValue += searchVal + operatorValue;
    }
}

int index = searchValue.LastIndexOf(operatorValue);

if (index != -1)
{
    outputSearchValue = searchValue.Substring(0, index);
}

3 个答案:

答案 0 :(得分:3)

尝试

var result = str.Replace("\" \"","\" And \"");

如果您有多个名称,或者两个名称之间可能有多个空格,则可以选择Regex。

var result = Regex.Replace(str,"\"\\s+\"","\" And \"");

示例

var str = "\"Mark John\"   \"Tina Roy\"   \"Anu Viswan\"";
var result = Regex.Replace(str,"\"\\s+\"","\" And \"");

输出

"Mark John" And "Tina Roy" And "Anu Viswan"

答案 1 :(得分:2)

或使用正则表达式:

var test = "\"John Smith\" \"Bill jones\" \"Bob Norman\"";      
Console.WriteLine(Regex.Replace(test, "\" \"", "\" AND \""));

答案 2 :(得分:0)

" "替换为" AND "

,而不是拆分
var test = "\"Mark John\" \"Tina Roy\"";
        var new_string= test.Replace("\" \"", " AND ");