C# 字符串替换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16839401/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 07:55:24  来源:igfitidea点击:

C# String Replace

c#stringreplacestr-replace

提问by Bobcat88

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.

我一直在尝试,line.replace( ... , ... )但无法正常工作。

Any help would be appreciated.

任何帮助,将不胜感激。

采纳答案by DonBoitnott

Have you tried this:

你有没有试过这个:

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

回答by dasblinkenlight

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 reswill have the replacements.

请注意,替换不会“就地”发生,因为 .NET 字符串是不可变的。调用后原始字符串将保持不变;只有返回的字符串res才会有替换。

回答by Si-N

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

怎么样 line.Replace(@""",""", ";");

回答by I4V

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

回答by Prabakaran

回答by Rukshan Perera

Make sure you properly escape the quotes.

确保正确转义引号。

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

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

回答by Hassan Rahman

The simplest way is to do

最简单的方法是做

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

Output is shown as below:

输出如下所示:

enter image description here

在此处输入图片说明

回答by Thivan Mydeen

//Replace Method

Here I'm replace old value to new value


string actual = "Hello World";

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

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

回答by saktiprasad swain

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.replace ..因为一个字符串被分配你不能操作。为此我们使用字符串构建器。这是我的例子。在 html 页面中,我添加了 [Name],它被 Name 替换。确保 [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());

回答by MR Coder

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 :

注意:如果您的字符串中有“”,则必须在“”后面使用@,例如:

@"","";