Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# string replace

I want to replace "," with a ; in my string.

For example:

Change

"Text","Text","Text",

to this:

"Text;Text;Text",

I've been trying the line.replace( ... , ... ), but can't get anything working properly.

like image 717
Bobcat88 Avatar asked May 30 '13 14:05

Bobcat88


3 Answers

Try this:

line.Replace("\",\"", ";") 
like image 52
DonBoitnott Avatar answered Oct 11 '22 13:10

DonBoitnott


The simplest way is to do

line.Replace(@",", @";");

Output is shown as below:

enter image description here

like image 36
Hassan Rahman Avatar answered Oct 11 '22 12:10

Hassan Rahman


You need to escape the double-quotes inside the search string, like this:

string orig = "\"Text\",\"Text\",\"Text\"";
string res = orig.Replace("\",\"", ";");

Note that the replacement does not occur "in place", because .NET strings are immutable. The original string will remain the same after the call; only the returned string res will have the replacements.

like image 43
Sergey Kalinichenko Avatar answered Oct 11 '22 12:10

Sergey Kalinichenko