【发布时间】:2017-11-23 18:21:26
【问题描述】:
我正在尝试将这个从字符串中删除 CRLF 的 VBA 函数转换为必须执行相同结果的 C# 函数
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); -
@Gusman Hey Gusman 我一直很感谢你的帮助,所以如果我不明白我应该替换:
return pString.Replace("\r\n", string.Empty).Replace("\n", string.Empty).Replace("\r", string.Empty).Replace(lineSep, string.Empty).Replace(paragraphSep, string.Empty);并替换为:return string.Replace("\n","").Replace("\r","").Substring(0, 9); -
是的,你也可以删除字符串。
-
等一下,null 结果也不一样,让我添加这个作为答案。
-
@Gusman 谢谢你,花点时间。非常感谢您的帮助