C # string replacement

I want to replace "," with; in my line.

Example:

Change it

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

to that

"Text; Text; Text",

I am trying to line.replace( ... , ... ) but cannot work properly.

Any help would be appreciated.

+6
source share
9 answers

Have you tried this:

 line.Replace("\",\"", ";") 
+22
source

You need to avoid double quotes inside the search string, for example:

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

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

+3
source
 var str = "Text\",\"Text\",\"Text"; var newstr = str.Replace("\",\"",";"); 
+3
source

The easiest way is to do

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

The result is shown below:

enter image description here

+3
source

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

0
source

Make sure you choose the right quotation marks.

  string line = "\"Text\",\"Text\",\"Text\","; string result = line.Replace("\",\"", ";"); 
0
source

You cannot use string.replace..and assign one line, you cannot manipulate. To do this, we use the string builder.here - my example. In the html page I add [Name], which is replaced by Name.make sure [Name] is unique, or u can give any unique name

  string Name = txtname.Text; string contents = File.ReadAllText(Server.MapPath("~/Admin/invoice.html")); StringBuilder builder = new StringBuilder(contents); builder.Replace("[Name]", Name); StringReader sr = new StringReader(builder.ToString()); 
-1
source
 //Replace Method Here I'm replace old value to new value string actual = "Hello World"; string Result = actual.Replace("World", "stackoverflow"); ---------------------- Output : "Hello stackoverflow" 
-3
source

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


All Articles