It is necessary to replace all the characters 'х' in the string with 'F' , tried this:

 String^ tempString = textBox1->Text; for (size_t i = 0; i < tempString->ToString()->Length; i++) { if (tempString[i] == L'x'); { tempString->SubString(i)->Replace(L'x', L'F'); } } 

The expected result is not received. What could be the problem?

  • The c ++ label seems superfluous here. All the same, C ++ CLI is a completely different language. - Vladimir Gamalyan

1 answer 1

In .NET, strings are immutable.

Therefore, Substring and Replace return you a new string, and the old one does not change. Therefore, your code does nothing.

On the other hand, you should not go through the string character by character, everything has already been done for you. The Replace function replaces all occurrences of one character with another at once.

Write just like that, without any cycle:

 tempString = tempString->Replace(L'x', L'F'); 
  • 3
    And yes, C ++ / CLI is a perversion. Write on .NET - go to C #. - VladD
  • she already realized that perversions) - Duracell
  • std::string::substr also returns a new string. - αλεχολυτ