Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Appending a Char to each element in a array

Tags:

c#

vb.net

Input in my case is a string with a list of elements separated by Comma

Input:

var input = "123,456,789";

Expected Output (a string):

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

I'm looking for a solution in VB.net but i'm not so familiar with it. So, I tried it in c#. Not sure what i'm 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 funding a solution in vb.net is much appreciated :)

like image 985
BetterLateThanNever Avatar asked Dec 13 '17 19:12

BetterLateThanNever


People also ask

How do you append to a char array?

append(char[] str) method appends the string representation of the char array argument to this sequence. The characters of the array argument are appended, in order, to the contents of this sequence. The length of this sequence increases by the length of the argument.

How do you add items to each element in an array?

If you are using array module, you can use the concatenation using the + operator, append(), insert(), and extend() functions to add elements to the array. If you are using NumPy arrays, use the append() and insert() function.

How do you store values in a char array?

To store the words, a 2-D char array is required. In this 2-D array, each row will contain a word each. Hence the rows will denote the index number of the words and the column number will denote the particular character in that word.

What is a char * in C?

In C, char* means a pointer to a character. Strings are an array of characters eliminated by the null character in C.


1 Answers

You could use LINQ:

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

This splits the string at the , delimiter, then adds the quotes around the pieces using Select(), and then reassembles the array using string.Join()

Edit: Here is the equivalent VB.NET solution:

Dim result As String
result = String.Join(",", input.Split(",").Select(Function (x) ("'" & x & "'" )))
like image 84
adjan Avatar answered Sep 28 '22 00:09

adjan