我正在尝试将此VBA函数转换为C#函数,该函数可以从字符串中删除CRLF,并且新的函数需要实现相同的结果。
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
到目前为止,我已经想出了:
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);
}
但它没有达到相同的结果,有人能帮我调整我的C#函数,使其与VBA版本的结果相匹配吗?
return string.Replace("\n","").Replace("\r","").Substring(0, 9);
- Gusmanreturn string.Replace("\n","").Replace("\r","").Substring(0, 9);
,而不是使用return pString.Replace("\r\n", string.Empty).Replace("\n", string.Empty).Replace("\r", string.Empty).Replace(lineSep, string.Empty).Replace(paragraphSep, string.Empty);
。 - napi15