Adding Char to each element of the array

The input in my case is a line with a list of items separated by a comma

Input:

var input = "123,456,789";

Expected Result (row):

"'123','456','789'"

I am looking for a solution on VB.net, but I am not very familiar with it. So, I tried this in C #. Not sure what I am missing.

My attempt:

var input = "123,456,789";
var temp = input.Split(new Char[] { ',' });
Array.ForEach(temp, a => a = "'" + a + "'");
Console.WriteLine(String.Join(",",temp));

Actual output:

"123,456,789"

Any help in financing the solution at vb.net is greatly appreciated :)

+4
source share
4 answers

You can use LINQ:

var result = string.Join(",", input.Split(',').Select(x => "'" + x + "'"))

This splits the string on the delimiter ,, then adds quotes around the fragments with Select(), and then reassembles the array withstring.Join()

Change . This is the equivalent VB.NET solution:

Dim result As String
result = String.Join(",", input.Split(",").Select(Function (x) ("'" & x & "'" )))
+3
source

input = Regex.Replace(input, "\d+", "'$0'");

+2
source
var input = "123,456,789";
var temp = input.Split(new Char[] { ',' });
temp = Array.ConvertAll(temp, a => a = "'" + a + "'");

Console.WriteLine(String.Join(",", temp));

: fooobar.com/questions/393948/...

0

There are many solutions here, but I think it will be faster:

var newStr = "'" + string.Replace(str, ",", "','") + "'";

in vb.net

Private newStr = "'" & String.Replace(str, ",", "','") & "'"
0
source

Source: https://habr.com/ru/post/1690733/


All Articles