在我的情况下,输入是一个字符串,其中包含以逗号分隔的元素列表
输入:
var input = "123,456,789";
Run Code Online (Sandbox Code Playgroud)
预期输出(字符串):
"'123','456','789'"
Run Code Online (Sandbox Code Playgroud)
我正在 VB.net 中寻找解决方案,但我对它不太熟悉。所以,我在 c# 中尝试过。不知道我错过了什么。
我的尝试:
var input = "123,456,789";
var temp = input.Split(new Char[] { ',' });
Array.ForEach(temp, a => a = "'" + a + "'");
Console.WriteLine(String.Join(",",temp));
Run Code Online (Sandbox Code Playgroud)
实际输出:
"123,456,789"
Run Code Online (Sandbox Code Playgroud)
非常感谢为 vb.net 中的解决方案提供资金的任何帮助:)
您可以使用 LINQ:
var result = string.Join(",", input.Split(',').Select(x => "'" + x + "'"))
Run Code Online (Sandbox Code Playgroud)
这会在分隔符处分割字符串,,然后使用 为各部分添加引号 Select(),然后使用 重新组装数组string.Join()
编辑:这是等效的 VB.NET 解决方案:
Dim result As String
result = String.Join(",", input.Split(",").Select(Function (x) ("'" & x & "'" )))
Run Code Online (Sandbox Code Playgroud)