Name depends on the number of characters

时间:2019-05-31 11:36:06

标签: vb.net

How to write a conditional statement acting as follows:

  • If "% name%" has 1 character, use block in the name: 00 "% name%"
  • If "% name%" has 2, name: 0 "% name%"
  • If "% name%" has 3 , name: "% name%"

Probably at the beginning you need to check the number of characters

Dim array2() As String = array1.ToArray(GetType(String))

    action.SetDynamicParameter("FileName", ((((((("%version%" + "_")  _

                    + String.Join(", ", array2))  _

                    + "_")  _

                    + "%name%")  _

                    + ".jpg"))

I will be grateful for your help.

2 个答案:

答案 0 :(得分:2)

在我看来,您只是想将数字填充为以零开头为3的长度的字符串。对吗?如果是这样,请尝试以下操作:

Private Function FormatMyName(ByVal Name As String) As String
    Return Name.PadLeft(3, "0")
End Function

如果将其创建为函数,以便您更轻松地添加所需的其他格式(例如,添加.jpg扩展名)

答案 1 :(得分:1)

检查函数的返回值是否为零,或在将字符串传递给函数之前检查字符串的长度。

Private Function GetPaddedName(OriginalName As String) As String
    Dim PaddedString As String = ""
    Select Case OriginalName.Length
        Case 1
            PaddedString = "00" & OriginalName & ".jpg"
        Case 2
            PaddedString = "0" & OriginalName & ".jpg"
        Case 3
            PaddedString = OriginalName & ".jpg"
        Case Else
            PaddedString = Nothing
    End Select
    Return PaddedString
End Function