C# String Replace

人盡茶涼 提交于 2019-11-26 21:29:30

问题


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

For Example:

Change this

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

to this

"Text;Text;Text",

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

Any help would be appreciated.


回答1:


Have you tried this:

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



回答2:


The simplest way is to do

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

Output is shown as below:




回答3:


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.




回答4:


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



回答5:


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




回答6:


Please find from here for more help

http://msdn.microsoft.com/en-us/library/fk49wtc1.aspx




回答7:


Make sure you properly escape the quotes.

  string line = "\"Text\",\"Text\",\"Text\",";

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



回答8:


you cant use string.replace..as one string is assigned you cannot manipulate.for that we use string builder.here is my example.In 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());



回答9:


set your Textbox Value in a String Like :

string MySTring = textBox1.Text;

then replace your string , For Exam Replace Text To Hex :

MyString = MyString.Replace("Text","Hex");

Or For Your Problem (Replace "," With ;) :

MyString = MyString.Replace(@""",""",",");

Note : If You Have "" In Your String You Have to use @ In Back Of "" Like :

@"","";



回答10:


//Replace Method

Here I'm replace old value to new value


string actual = "Hello World";

string Result = actual.Replace("World", "stackoverflow");

----------------------
Output : "Hello stackoverflow"


来源:https://stackoverflow.com/questions/16839401/c-sharp-string-replace

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!