代码之家  ›  专栏  ›  技术社区  ›  Bokambo

在值之间拆分和附加“和”

  •  -1
  • Bokambo  · 技术社区  · 6 年前

    如何拆分以下值和附加值以及值之间? 我不能用空格分开,因为单词之间有空格

    "\"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 回复  |  直到 6 年前
        1
  •  2
  •   Sean Murphy    6 年前

    或使用正则表达式:

    var test = "\"John Smith\" \"Bill jones\" \"Bob Norman\"";      
    Console.WriteLine(Regex.Replace(test, "\" \"", "\" AND \""));
    
        2
  •  3
  •   Anu Viswan    6 年前

    尝试

    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"
    
        3
  •  0
  •   Don A.R.B.N    6 年前

    替换而不是拆分 " " 具有 " AND "

    var test = "\"Mark John\" \"Tina Roy\"";
            var new_string= test.Replace("\" \"", " AND ");