locked
How do I remove Hex 0a / Linefeed?

    Question

  • I have hex  0a  line-feeds in a text file I need to remove.

    Where  s   is a string, none of these work:

    s = s.Replace("\r\n", "");
    s = s.Replace(
    "\n\r", ""
    );
    s = s.Replace(
    "\n", ""
    );
    s = s.Replace("\r", ""
    );


    Can someone please give a few lines of code to remove the hex 0a characters?

    Thanks,

    Dennis
    Tuesday, December 02, 2008 4:10 PM

Answers

  • Use the other overload.  You seem to already know the char value for carriage return and line feed.  Your strings are not evaluating quite the way you think they should be.  Use the debugger to observe what is actually happening. 

    String s1 = "\r\n";
    String s2 "";
    s = s.Replace("\r", "");


    public string Replace(char oldChar, char newChar)  
        Member of System.String  
     
    Summary:  
    Replaces all occurrences of a specified Unicode character in this instance with another specified Unicode character.  
     
    Parameters:  
    newChar: A Unicode character to replace all occurrences of oldChar.  
    oldChar: A Unicode character to be replaced.  
     
    Return Values:  
    A System.String equivalent to this instance but   
    with all instances of oldChar replaced with newChar. 

    Rudedog   =8^D
    Mark the best replies as answers. "Fooling computers since 1971."
    Tuesday, December 02, 2008 5:58 PM
  • Okay, got it working. Thanks.
    Tuesday, December 02, 2008 8:12 PM

All replies

  • Use the other overload.  You seem to already know the char value for carriage return and line feed.  Your strings are not evaluating quite the way you think they should be.  Use the debugger to observe what is actually happening. 

    String s1 = "\r\n";
    String s2 "";
    s = s.Replace("\r", "");


    public string Replace(char oldChar, char newChar)  
        Member of System.String  
     
    Summary:  
    Replaces all occurrences of a specified Unicode character in this instance with another specified Unicode character.  
     
    Parameters:  
    newChar: A Unicode character to replace all occurrences of oldChar.  
    oldChar: A Unicode character to be replaced.  
     
    Return Values:  
    A System.String equivalent to this instance but   
    with all instances of oldChar replaced with newChar. 

    Rudedog   =8^D
    Mark the best replies as answers. "Fooling computers since 1971."
    Tuesday, December 02, 2008 5:58 PM
  • I tried using the other overload with s2 and got the same results.
    Tuesday, December 02, 2008 6:04 PM
  • NDennisV said:

    I tried using the other overload with s2 and got the same results.



    The overload that I posted requires that you furnish object of type Char as the parameters, not String.  The decimal value for Hex OA is decimal 10, which is a line feed.  By the way, Carriage Return is 13. 

    // define constants  
    char lineFeed = Convert.ToChar(10);  
    char carriageReturn = Convert.ToChar(13);  
    char nullString = Convert.ToChar(0); 



    Rudedog
    Mark the best replies as answers. "Fooling computers since 1971."
    Tuesday, December 02, 2008 6:17 PM
  • Sorry, I can't make it work. Still get the same results.

    A few lines of code using Replace please?
    Tuesday, December 02, 2008 6:38 PM
  • Okay, got it working. Thanks.
    Tuesday, December 02, 2008 8:12 PM
  • NDennisV said:

    Okay, got it working. Thanks.



    Post your solution so that others may benefirt.  Especially if the problem was a simple typo or something is easily overlooked.  Those errors are the ones that drive all of us nuts.  Once you hear about it, you'll be on the look out for it.

    Rudedog   =8^D
    Mark the best replies as answers. "Fooling computers since 1971."
    Tuesday, December 02, 2008 9:10 PM
  •  

    Hi NDennisV,

    I am glad to hear that you have made your codes work.

    I agree on Rudedog’s suggestion.  If you could share your solution here, I am sure it will benefit many other community members.   Let’s contribute more to MSDN Forums together and thank you in advance.  

     

    Best Regards,
    Lingzhi


    Please remember to mark the replies as answers if they help and unmark them if they provide no help. http://forums.msdn.microsoft.com/en-US/csharpide/thread/8e9ed0d7-11ff-402a-8489-9b5f05eeb706 http://forums.msdn.microsoft.com/en-US/vssetup/thread/60424309-bd78-4ca2-b618-03c4a16123b6
    Friday, December 05, 2008 7:50 AM
  • It was my fault. I had a routine that was not being called properly.

    Dennis
    Friday, December 05, 2008 5:57 PM