A
A
Alexander Alexandrovich2014-06-17 20:45:47
Microsoft
Alexander Alexandrovich, 2014-06-17 20:45:47

How to replace characters in a string using c#?

Guys, you need to change the characters (Words) in a string in c#
I use Replace :

string textFile = System.IO.File.ReadAllText(@"C:\Users\Александр\Desktop\good.htm");

textFile = textFile.Replace("datatimes", this.clientsItem.dateconnect.ToString());

File.AppendAllText(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\otchet.htm", textFile, System.Text.Encoding.GetEncoding("utf-8"));

There is no replacement at the output.
What could be the problem?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
F
Funbit, 2014-06-18
@Funbit

I suspect that the problem is a case mismatch, i.e. in the text "DataTimes" and is replaced by "datatimes". If you need a case-insensitive replacement, you can use Regex.Replace:

Regex.Replace(textFile, "datatimes", this.clientsItem.dateconnect.ToString(), RegexOptions.IgnoreCase)

A
aush, 2014-06-18
@aush

The code is correct, check the input data.

A
Alexander Alexandrovich, 2014-06-18
@tatu

The funny thing is that if you assign a new variable, then everything is fine

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question