Delete or trim carriage return at end of line

Possible duplicate:
Removing carriage return and newline from end of line in C #

How to remove a carriage return at the end of a line. I already have Google, but I can’t find the code I need.

This is my current code:

return s.Replace ("\ n", ") .Replace (" \ r ",") .Replace ("\ r \ n", "") .Trim ();

but, of course, all replacements of the old character will be replaced.

I tried these:

trimming a public line (line s) {

string[] whiteSpace = {"\r", "\n", "\r\n"}; foreach(string ws in whiteSpace) { if (s.EndsWith(ws)) { s = s.Substring(0, s.Length - 1); } } return s; 

}

but it doesn’t work for me, maybe I missed something or there is something wrong with my code

I am also trying to use regex, but I cannot get what I need.

I get a string from a text document and then wrap it to other documents.

I will be grateful for any help. Tnx

+4
source share
2 answers

Hi, I found a solution in the following link. So the loan should go to this author.

Example

However, I tuned it to increase the demonstration ability.

 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication4 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { String s = "ABCDE\r\n" + "FGHIJ"; MessageBox.Show(s); MessageBox.Show(RemoveAndNewlineLineFeed(s)); } string RemoveAndNewlineLineFeed(string s) { String[] lf = { "\r", "\n" }; return String.Join("",s.Split(lf,StringSplitOptions.RemoveEmptyEntries)); } } } 

Check this.

+2
source

Source: https://habr.com/ru/post/1332059/


All Articles