4

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?

Mathieu Guindon
  • 69,817
  • 8
  • 107
  • 235
napi15
  • 2,354
  • 2
  • 31
  • 55
  • 1
    The exact replacement would be: `return string.Replace("\n","").Replace("\r","").Substring(0, 9);` – Gusman Nov 23 '17 at 18:24
  • @Gusman Hey Gusman I always appreciate your help , so if I undestand I should replace : `return pString.Replace("\r\n", string.Empty).Replace("\n", string.Empty).Replace("\r", string.Empty).Replace(lineSep, string.Empty).Replace(paragraphSep, string.Empty);` and replace it by : `return string.Replace("\n","").Replace("\r","").Substring(0, 9);` – napi15 Nov 23 '17 at 18:26
  • Yes, also you can remove the strings. – Gusman Nov 23 '17 at 18:27
  • Wait, also, the null result is different, let me add this as an answer. – Gusman Nov 23 '17 at 18:28
  • @Gusman Thank you , take all the time you need . Your help is very appreciated – napi15 Nov 23 '17 at 18:28

2 Answers2

7

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;
}
Gusman
  • 14,905
  • 2
  • 34
  • 50
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.

Mathieu Guindon
  • 69,817
  • 8
  • 107
  • 235
Heinzi
  • 167,459
  • 57
  • 363
  • 519
  • 2
    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 – napi15 Nov 23 '17 at 18:36
  • @napi15: Thanks. Well, talking about "old code that should be updated"... ;-) – Heinzi Nov 23 '17 at 18:45