Answer the question
In order to leave comments, you need to log in
How to replace elements in a C# string?
Hey!
There is a line
string s = "<div>Здесь нам нужно заменить некоторый текст, текст, текст</div>";
s = s.Replace("текст", "ТЕКСТ");
Answer the question
In order to leave comments, you need to log in
You need to find all the places where there is the right word.
string s = "<div>Здесь нам нужно заменить некоторый текст, текст, текст</div>";
var word = "текст";
var replaceTo = "ТЕКСТ";
var words = new List<int>();
int lastPos = 0;
int pos = 0;
do
{
pos = s.IndexOf(word, lastPos);
if (pos >= 0)
{
words.Add(pos);
lastPos = pos + word.Length;
}
}
while (pos >= 0);
string s = "<div>Здесь нам нужно заменить некоторый текст, текст, текст</div>";
var word = "текст";
var replaceTo = "ТЕКСТ";
var words = new Regex(word).Matches(s).OfType<Match>().Select(match => match.Index).ToList();
var result = s.Substring(0, words[0]) + replaceTo + s.Substring(words[0] + word.Length);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question