如何检查票号是否使用函数格式化



我希望我的函数遵循一些约定来检查我的票号是否需要格式化。

如果不能满足惯例,那么我想更改一下票号。

票证编号19K3072216需要格式化为此19-K3-07-002216

  1. 检查前两位数字是否为0-9(数字)
  2. 检查第三个数字的值是否为a到Z
  3. 检查第4位的值是否为0-9(数字)
  4. 检查第5位和第6位是否有日期值(例如,2位年份-17、90、15等)
  5. 检查接下来的6位数字,即第7位至第12位数字是否为数字

由于票号19K3072216不符合上述条件,我希望我的功能将其格式化为如下19-K3-07-002216

字符串strTicketNumber应返回格式化的票号19-K3-07-002216

我的vb.net功能

Public Class Ticket_Code
Public Shared Sub main()
Dim strTicketNumber As String = FixTicketNumber("19K3072216")
End Sub
Public Shared Function FixCaseNumber(ByVal astrCaseNumber As String) As String
Dim strCaseNumber As String = Replace(astrCaseNumber, "-", "")
'Determine if ticket number is formatted
How do I do this?

'If ticket number is formatted add 2 zeros
'How do I do this?
'Else return unchanged
'If ticket number is already formatted, just returned the number (original number)
Return strCaseNumber
End Function

End Class

这将真正取决于您的输入以及它与示例的不同程度。例如,无效输入的格式是否总是相同的19K3072216,或者是否有可能是所有数字、所有字母、长度小于/大于10个字符等。所有这些规则都需要根据需要进行考虑和处理。

如果输入来自用户,请永远不要相信它,并始终认为它离有效性最远。如果应用程序可以处理这种情况,它可以处理的其他一切

这样的东西应该让你开始:

Public Sub Main()
Dim strTicketNumber As String = FixTicketNumber("19-K3-07-002216") ' or 19K3072216
Console.WriteLine(strTicketNumber)
Console.ReadKey()
End Sub

Private Function FixTicketNumber(p1 As String) As String
Dim fixed As String = ''
Dim valid As Boolean = checkTicketNumber(p1)
If valid Then
Return p1 ' Ticket number is valid, no transformation needed
Else
'Assume invalid input will always be 10 characters (e.g. 19K3072216)
'Split the input and Step through each rule one at a time
'returning the necessary result/format string as you go
'#1 Check if the 1st 2 digits has a value 0 - 9 (numeric)
Dim ruleOne As String = p1.Substring(0, 2)
'perform isNumeric, concatenate to fixed if everything is ok
'fixed += ruleOne+"-"
'#2 Check if the 3rd digit has a value of A to Z
Dim ruleTwo As String = p1.Substring(3, 1)
'check if its a letter, concatenate to fixed if everything is ok
'... same for all the rules
End If
End Function
Private Function checkTicketNumber(p1 As String) As Boolean
'See if the input matches the rules
'Check if the 1st 2 digits has a value 0 - 9 (numeric)
'Check if the 3rd digit has a value of A to Z
'Check if the 4th digit has a value 0 - 9 (numeric)
'Check if the 5th and 6th digits has a date value (e.g.2 digit year - 17, 90, 15 etc)
'Check if the next 6 digits i.e. 7th - 12th digits are numeric.
Dim pattern As String = "d{2}-[A-Z]d-d{2}-d{6}"
Dim match As Match = Regex.Match(p1, pattern)
Return match.Success
End Function

很难产生一个完全有效的解决方案,因为作为局外人,输入有太多未知因素。

最新更新