我正在编写一个格式化字符串的函数。我收到一串数字,有时带破折号,有时没有。我需要生成一个14个字符的输出字符串,所以如果输入字符串包含少于14个字符,我需要用零填充它。然后我需要通过在适当的位置插入破折号来掩盖数字串。这是我到目前为止所得到的:
strTemp = strTemp.Replace("-", "")
If IsNumeric(strTemp) Then
If strTemp.Length < 14 Then
strTemp = strTemp.PadRight(14 - strTemp.Length)
End If
output = String.Format(strTemp, "{00-000-0-0000-00-00}")
End If
以上工作正常,除了它只返回一串数字而不放入破折号。我知道我在使用String.Format做错了,但到目前为止我只使用了预定义的格式。有人可以帮忙吗?在这种情况下,如何使用正则表达式进行字符串格式化?
答案 0 :(得分:1)
这个功能可以解决问题:
Public Function MaskFormat(input As String) As String
input = input.Replace("-", String.Empty)
If IsNumeric(input) Then
If input.Length < 14 Then
input = input.PadRight(14 - input.Length)
End If
Return String.Format("{0:00-000-0-0000-00-00}", CLng(input))
Else
Return String.Empty
End If
End Function
您可以在字符串格式here上找到更多信息。