Remove CRLF from string C#

12,567

Solution 1

You are missing the null check (the original function returns an empty string in that case) and also you forgot the Left$ which trims the string size.

public static string RemoveCRLFFromString(string pString)
{
    //Return empty string if null passed
    if(pString == null)
        return ""; 

    //Remove carriage returns
    var str = pString.Replace("\n","").Replace("\r",""); 

    //If len is more than 9 chars trim it
    return str.Length > 9 ? str.Substring(0, 9) : str;
}

Solution 2

The VBA function is unnecessarily complicated. It can be simplified to:

Private Function RemoveCRLFFromString(ByVal pString As Variant) As String
    Dim s As String

    s = Nz(pString)  ' Available in Access VBA, in Excel you'd need a null check
    s = Replace(s, vbCr, "")
    s = Replace(s, vbLf, "")

    RemoveCRLFFromString = Left(s, 9)
End Function

Once the useless complexity is gone, the translation to C# is straightforward and left as an exercise to the reader. Note:

  • Nz(...) becomes ... ?? ""
  • Replace(...) becomes ....Replace(...)
  • Left becomes Truncate, which isn't a built-in method but can be implemented easily with a length check.

Oh, and since your method does more than removing CR and LF (it also truncates the string), its name should be changed.

Share:
12,567
napi15
Author by

napi15

To me, programming is a way of looking at things. Software Engineering isn't only my job, it's my passion My main job is developing solutions for SAP :) usign SAPUI5, JS, nodeJS, OpenUI5 etc.... I mostly love C# and .NET framework, I believe JAVA is made for n00bs.

Updated on June 07, 2022

Comments

  • napi15
    napi15 almost 2 years

    I'm trying to convert this VBA fonction that remove CRLF from string to a C# function that must do the same result

    Private Function RemoveCRLFFromString(ByVal pString As Variant) As String
    Dim i As Integer
    Dim c As String * 1
    
     If IsNull(pString) Then
        RemoveCRLFFromString = ""
     Else
        For i = 1 To Len(pString)
            c = Mid$(pString, i, 1)
            If Asc(c) <> 10 And _
               Asc(c) <> 13 Then
               RemoveCRLFFromString = RemoveCRLFFromString & c
            End If
        Next i
     End If
    
    
     RemoveCRLFFromString = Left$(RemoveCRLFFromString, 9)
    
    End Function
    

    So far I have come up with:

    public static string RemoveCRLFFromString(string pString )
    {
        if(String.IsNullOrEmpty(pString))
        {
            return pString ;
        }
        string lineSep = ((char) 0x2028).ToString();
        string paragraphSep = ((char)0x2029).ToString();
    
        return pString.Replace("\r\n", string.Empty).Replace("\n", string.Empty).Replace("\r", string.Empty).Replace(lineSep, string.Empty).Replace(paragraphSep, string.Empty);
    }
    

    But it's not achieving the same result, can someone help me adjust my C# function to match the same result as the VBA version?

  • napi15
    napi15 over 6 years
    Hello Heinzi , yes of course it's complicated , I'm working on a access application that was build in 1998 and trying to write it in a c# 2017 app :) .....thank you for your time optimizing the vba code
  • Can Sahin
    Can Sahin over 6 years
    @napi15: Thanks. Well, talking about "old code that should be updated"... ;-)