2014-10-30 143 views
0

我正在写一个函数来格式化字符串。我收到一串数字,有时候是破折号,有时候不是。我需要产生一个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错误,但到目前为止我只使用预定义的格式。谁能帮忙?在这种情况下,我如何使用正则表达式来进行字符串格式化?

回答

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

相关问题