Look for char (13) or (10) in a field and replace it with a [closed] enter

0

Generate this variable that I bring from the database but in it there is char (13) or char (10) so what I want is to create a search engine where I find these two characters and convert it into another string so that it forms another my address line.

sAddress = ds.Tables[0].Rows[0]["STREET_INFO"].ToString();

    
asked by Cris Valdez 18.11.2016 в 18:18
source

2 answers

2

You can check if your chain is contained and make the replacement, the equivalent replacements are:

chr(13) => "\r"  ... retorno de carro.
chr(10) => "\n"  ... nueva línea.

therefore it would be like this:

            if (sAddress.Contains((char)13)) 
            {
                sAddress = sAddress.Replace("\r", "<texto_remplazo>");
            }
            if (sAddress.Contains((char)10))
            {
                sAddress = sAddress.Replace("\n", "<texto_remplazo>");
            }

This is an example:

 String sAddress = "Hola soy StackOverflow.com  " + ((char)13) + " buna " + ((char)10) + " StackOverflow.com";

            if (sAddress.Contains((char)13))
            {
                sAddress = sAddress.Replace("\r", "aquisecambio13");
            }
            if (sAddress.Contains((char)10))
            {
                sAddress = sAddress.Replace("\n", "aquisecambio10");
            }

Where you would have the result:

Hola soy StackOverflow.com  aquisecambio13 buna aquisecambio10 StackOverflow.com
    
answered by 18.11.2016 в 18:32
1

If you want to replace it with an "Enter", I assume it's a line break, this had already been answered in StackOverflow in English, the question How to replace part of string by position? (How to replace part of a string with the position?)

What it says is that you should use a StringBuilder .

sAddress = ds.Tables[0].Rows[0]["STREET_INFO"].ToString();
var aStringBuilder = new StringBuilder(sAddress);
aStringBuilder.Remove(10, 1); // (index, largo)
aStringBuilder.Insert(10, "\n"); //insertas
aStringBuilder.Remove(13, 1); // (index, largo)
aStringBuilder.Insert(13, "\n"); //insertas
theString = aStringBuilder.ToString();
    
answered by 18.11.2016 в 18:31