rtrim net example ejemplos c# asp.net string carriage-return

net - ¿Cómo puedo eliminar "/ r / n" de una cadena en c#? ¿Puedo usar un regEx?



trim c# ejemplos (7)

Estoy intentando persistir la cadena de un textarea de ASP.NET. Necesito quitar las alimentaciones de la línea de retorno del carro y luego dividir lo que quede en una matriz de 50 caracteres.

Tengo esto hasta ahora

var commentTxt = new string[] { }; var cmtTb = GridView1.Rows[rowIndex].FindControl("txtComments") as TextBox; if (cmtTb != null) commentTxt = cmtTb.Text.Length > 50 ? new[] {cmtTb.Text.Substring(0, 50), cmtTb.Text.Substring(51)} : new[] {cmtTb.Text};

Funciona bien, pero no estoy eliminando los caracteres CrLf. ¿Cómo hago esto correctamente?

Gracias, ~ ck en San Diego


Aquí está el método perfecto

Tenga en cuenta que Environment.NewLine funciona en plataformas de Microsoft .

¡Además de lo anterior, necesita agregar / ry / n en una función separada !

Aquí está el código que apoyará si escribe en Linux, Windows o Mac

var stringTest = "/r Test/nThe Quick/r/n brown fox"; Console.WriteLine("Original is:"); Console.WriteLine(stringTest); Console.WriteLine("-------------"); stringTest = stringTest.Trim().Replace("/r", string.Empty); stringTest = stringTest.Trim().Replace("/n", string.Empty); stringTest = stringTest.Replace(Environment.NewLine, string.Empty); Console.WriteLine("Output is : "); Console.WriteLine(stringTest); Console.ReadLine();


Esto divide la cadena en cualquier combinación de nuevos caracteres de línea y los une con un espacio, suponiendo que realmente desea el espacio donde habrían estado las nuevas líneas.

var oldString = "the quick brown/rfox jumped over/nthe box/r/nand landed on some rocks."; var newString = string.Join(" ", Regex.Split(oldString, @"(?:/r/n|/n|/r)")); Console.Write(newString); // prints: // the quick brown fox jumped over the box and landed on some rocks.


La función .Trim () hará todo el trabajo por ti.

Estaba intentando el código anterior pero después de la función "recorte", y noté que todo está "limpio" incluso antes de que llegue al código de reemplazo.

String input: "This is an example string./r/n/r/n" Trim method result: "This is an example string."

Fuente: http://www.dotnetperls.com/trim


Nicer código para esto:

yourstring = yourstring.Replace(System.Environment.NewLine, string.Empty);


Prueba esto:

private void txtEntry_KeyUp(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { string trimText; trimText = this.txtEntry.Text.Replace("/r/n", "").ToString(); this.txtEntry.Text = trimText; btnEnter.PerformClick(); } }


Puede usar una expresión regular, sí, pero una cadena simple. Reemplazar () probablemente sea suficiente.

myString = myString.Replace("/r/n", string.Empty);


Suponiendo que desea reemplazar las líneas nuevas con algo para que algo como esto:

the quick brown fox/r/n jumped over the lazy dog/r/n

no termina así:

the quick brown foxjumped over the lazy dog

Haría algo como esto:

string[] SplitIntoChunks(string text, int size) { string[] chunk = new string[(text.Length / size) + 1]; int chunkIdx = 0; for (int offset = 0; offset < text.Length; offset += size) { chunk[chunkIdx++] = text.Substring(offset, size); } return chunk; } string[] GetComments() { var cmtTb = GridView1.Rows[rowIndex].FindControl("txtComments") as TextBox; if (cmtTb == null) { return new string[] {}; } // I assume you don''t want to run the text of the two lines together? var text = cmtTb.Text.Replace(Environment.Newline, " "); return SplitIntoChunks(text, 50); }

Me disculpo si la sintaxis no es perfecta; No estoy en una máquina con C # disponible en este momento.