5

Possible Duplicate:
Removing carriage return and new-line from the end of a string in c#

How do I remove the carriage return at the end of string. I have already google out this but I cannot find the code I need.

This is my current code:

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

but of course all the occurrence of the old character will be replaced.

I have tried these:

public string trim(string 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 is not working for me, maybe I miss something or there's a wrong with my code

I have also try using regular expression but I can't get what I need.

I get the string from word document then will be transfered to another docs.

I'll appreciate any help.Tnx

Community
  • 1
  • 1
ace
  • 6,775
  • 7
  • 38
  • 47
  • @paxdiablo I've checked for the possible duplicate, but I didn't find any duplication. And about the links I've checked it out but was not worked for me. Maybe I'll try experiment using regular expression as what I've read in that link. – ace Dec 14 '10 at 05:02
  • Looks like a dupe to me, both questions are C#, both seek to remove CR/LF from the end of a string. Unless I'm missing something, that's pretty well an exact match. – paxdiablo Dec 14 '10 at 06:06
  • I also wondering why I get that kind of result. I’m not sure if somewhere in my code cause the unexpected result so I try to print it in console right after I trim the string. But then again I get the same result. Note: I’m using C# 2010 with office 2003 You may check this link to see the actual output: http://b.imagehost.org/0707/carriage_return.jpg – ace Dec 14 '10 at 07:00

2 Answers2

2

Hi I found a solution in following link. So the credit should go to that author.

Example

However I have customized it to boost the demonstrative capability.

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 out.

Greg
  • 23,155
  • 11
  • 57
  • 79
JCTLK
  • 2,145
  • 8
  • 29
  • 37
  • I've already checked and used it in my code. Though it removes the carriage return but it removes all the occurrence of carriage return. I'll try to experiment from that code.tnx – ace Dec 14 '10 at 04:43
1

See this answer: Removing carriage return and new-line from the end of a string in c#

Community
  • 1
  • 1
zsalzbank
  • 9,685
  • 1
  • 26
  • 39