Read and edit a specific word in a string (c #) [duplicate]

0

I have a text file:

link

I put this piece of code in a string, and I need to replace the name inside (in the Mateus case) with another name of another string.

    
asked by anonymous 06.10.2017 / 20:04

2 answers

0

You can use the String.Replace.

Ex:

 public static void Main()
   {
      String s = new String('a', 3);
      Console.WriteLine("The initial string: '{0}'", s);
      s = s.Replace('a', 'b').Replace('b', 'c').Replace('c', 'd');
      Console.WriteLine("The final string: '{0}'", s);
   }

Reference: link

    
06.10.2017 / 20:06
0

What I understood from your question was: You want to change what's inside the <name>..esse texto aqui..</name> being that this text belongs to a String

Then do the following:

 // adicione a referência
    using System.Xml;

    //carregando o texto que você mencionou em uma string
    String textoXml = "<Farmer><name>nome</name><isEmoting>false</isEmoting><isCharging>false</isCharging></Farmer>";

   // Criando um novo xmlDocument
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.InnerXml = textoXml;// pasando para o Xml o conteúdo da string

    //Pegando o elemento pelo nome da TAG
    XmlNodeList xNodeList = xmlDoc.GetElementsByTagName("name");

    foreach (XmlNode xNode in xNodeList)
    {
       xNode["name"].InnerText = "outroNome";// alterando o texto desejado
    }

    String textoAlterado = xmlDoc.InnerXml; //texto alterado 

The result will be:

<Farmer><name>outroNome</name><isEmoting>false</isEmoting><isCharging>false</isCharging></Farmer>
    
06.10.2017 / 22:33