nemo_87
nemo_87

Reputation: 4781

Split string by character and separate it with comma in C#

How can I separate string by character and separate each of them by comma?

I have for example value = '1234'; and I want to transform it to: value = '1,2,3,4'. So it should still be string (not an array of numbers).

I've tried this out:

var result = "1234";
var fResult = string.Join(",", result.Split());

But I did not have any success. I've got again result = "1234"; Where I'm making a mistake?

Upvotes: 2

Views: 2521

Answers (3)

Linh Tuan
Linh Tuan

Reputation: 448

this demo for you:

var result = "1234";
var data = result.ToCharArray();
var fResult = string.Join(",", data);

Upvotes: 1

Mostafiz
Mostafiz

Reputation: 7352

Take string as char array then Join again with comma

var result = "1234";
var fResult = string.Join(",", result.ToCharArray());

Upvotes: 12

fubo
fubo

Reputation: 45947

Join your string as IEnumerable<char>

string input = "1234";  
string result = string.Join(",",input.AsEnumerable());

Upvotes: 3

Related Questions