对于14个字符,我需要这样的格式:
10.257.938 / 0001.45
但是当我执行这段代码时,我得到了
### ### / #### - 。。
在txtcnpj.text
上。
我真的不喜欢使用maskedbox。 这是我的代码(失去焦点。):
Private Sub txtcnpj_LostFocus(sender As Object, e As EventArgs) Handles txtcnpj.LostFocus
If Len(txtcnpj.Text) > 0 Then
Select Case Len(txtcnpj.Text)
Case Is = 11
txtcnpj.Text = Format(txtcnpj.Text, "###.###.###-##")
Case Is = 14
txtcnpj.Text = Format(txtcnpj.Text, "##.###.###/####-##")
End Select
End If
End Sub
OR WHEN I USED ##.###.###/####-## RETURN
Andrew Morton解决, TY ANDREW。 UHUL。
在我的情况下,解决方案是:
Private Sub txtcnpj_LostFocus(sender As Object, e As EventArgs) Handles txtcnpj.LostFocus
If Len(txtcnpj.Text) > 0 Then
Select Case Len(txtcnpj.Text)
Case Is = 14
Dim A As String
A = txtcnpj.Text.Replace("."c, "").Replace("/"c, "")
txtcnpj.Text = String.Concat(A.Substring(0, 2), ".", A.Substring(2, 3), ".", A.Substring(5, 3), "/", A.Substring(8, 4), "-", A.Substring(12, 2))
End Select
End If
End Sub
答案 0 :(得分:1)
格式化就像是数字而不是字符串。
编写自己的方法最简单,例如:
Option Strict On
Module Module1
Function FormatWithSlash(s As String) As String
s = s.Replace("."c, "").Replace("/"c, "")
Return String.Concat(s.Substring(0, 2), ".", s.Substring(2, 3), ".", s.Substring(5, 3), "/", s.Substring(8, 4), ".", s.Substring(12, 2))
End Function
Sub Main()
Console.WriteLine(FormatWithSlash("12345678901234"))
Console.WriteLine(FormatWithSlash("10.257.938/0001.45"))
Console.ReadLine()
End Sub
End Module
输出:
12.345.678 / 9012.34
10.257.938 / 0001.45