0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

文字列追加

Last updated at Posted at 2025-04-21

文字列追加(引数情報指定)

指定した文字を追加する。
区切り文字指定、除外文字列指定、重複チェック指定、追加位置指定あり。

'------------------------------------------------------------------------------
' 文字列追加
'------------------------------------------------------------------------------
' 引数情報初期化
Public Property Get G_String_ClearArgAddInf() As T_STRING_ARG_ADD_INF
    With G_String_ClearArgAddInf
        .Str = ""
        
        .Dlmt = ""
        .DlmtChkFlg = True
        
        .Add = ""
        .AddChkFlg = False
        .AddSpec = E_STRING_SPEC_POS_END
        
        .Excluded = ""
    End With
End Property

' 文字列追加(引数情報指定)
Public Function F_String_ReturnAdd_Inf( _
        ByRef aArgInf As T_STRING_ARG_ADD_INF) As String
    Dim wkRtn As String
    
    Dim wkAddChkFlg As Boolean
    Dim wkTmpStr As String
    
    With aArgInf
        '初期化
        wkRtn = .Str
        wkAddChkFlg = .AddChkFlg
        
        '追加ありの場合
        If .Add <> "" Then
            '区切り文字接続
            If wkRtn = "" Then
                '追加元が無い場合は無視
            ElseIf PF_String_GetAdd_Sub(wkRtn, .Dlmt, .AddSpec, .DlmtChkFlg, .Excluded) = True Then
                '区切り追加した場合は追加チェックを有効化
                wkAddChkFlg = True
            End If
            
            '追加文字接続
            PF_String_GetAdd_Sub wkRtn, .Add, .AddSpec, wkAddChkFlg, .Excluded
        End If
    End With
    
    F_String_ReturnAdd_Inf = wkRtn
End Function

' サブルーチン
Private Function PF_String_GetAdd_Sub( _
        ByRef aRtn As String, _
        ByVal aAdd As String, ByVal aAddSpec As E_STRING_SPEC, _
        ByVal aAddChkFlg As Boolean, ByVal aExcluded As String) As Boolean
    Dim wkRet As Boolean: wkRet = True
    
    Dim wkChkStr As String
    Dim wkExcStr As String
    
    If aAdd <> "" Then
        If aAddChkFlg = True Then
            '追加位置が後方指定の場合
            If aAddSpec = E_STRING_SPEC_POS_END Then
                wkChkStr = Right(aRtn, Len(aAdd))
                If aExcluded <> "" Then
                    wkExcStr = Right(aRtn, Len(aExcluded))
                End If
            '追加位置が前方指定の場合
            Else
                wkChkStr = Left(aRtn, Len(aAdd))
                If aExcluded <> "" Then
                    wkExcStr = Left(aRtn, Len(aExcluded))
                End If
            End If
            
            '接続位置に文字がある場合は不採用
            If StrComp(wkChkStr, aAdd, vbBinaryCompare) = 0 Then
                '対象外文字が無い場合は不採用
                If aExcluded = "" Then
                    wkRet = False
                '対象外文字と不一致の場合は不採用
                ElseIf StrComp(wkExcStr, aExcluded, vbBinaryCompare) <> 0 Then
                    wkRet = False
                End If
            End If
        End If
        
        '追加ありの場合
        If wkRet = True Then
            '追加位置指定に従って文字列追加
            If aAddSpec = E_STRING_SPEC_POS_END Then
                aRtn = aRtn & aAdd
            Else
                aRtn = aAdd & aRtn
            End If
        End If
    End If
    
    PF_String_GetAdd_Sub = wkRet
End Function

文字列追加(引数指定)

' 文字列追加(引数指定)
'------------------------------------------------------------------------------
Public Function F_String_ReturnAdd( _
        ByVal aStr As String, ByVal aAdd As String, _
        Optional ByVal aDlmt As String = "", _
        Optional ByVal aAddSpec As E_STRING_SPEC = E_STRING_SPEC_POS_END, _
        Optional ByVal aExcluded As String = "") As String
    Dim wkArgInf As T_STRING_ARG_ADD_INF: wkArgInf = G_String_ClearArgAddInf()
    
    With wkArgInf
        .Str = aStr
        .Dlmt = aDlmt
        .Add = aAdd
        .AddSpec = aAddSpec
        .Excluded = aExcluded
    End With
    
    F_String_ReturnAdd = F_String_ReturnAdd_Inf(wkArgInf)
End Function
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?