VBA#修剪字符串以删除字符

时间:2019-02-18 15:09:54

标签: excel vba ms-access access-vba

我有一个文件名,我需要删除以下一些字符,即文件名和修剪文件名后的目标。

我当前的字符串=“ text_12_12_19.pdl”

新的字符串目标=“ Text.pdl”

3 个答案:

答案 0 :(得分:1)

您可以使用 Split

MyStringGoal = Split(MyCurrentString, "_")(0) & "." & Split(MyCurrentString, ".")(1)

答案 1 :(得分:0)

'另一种解决方案(也可以左右使用):

Dim my_current_string As String
Dim New_String_Goal As String
Dim r As String, l As String

my_current_string = "text_12_12_19.pdl"
l = Left(my_current_string, 4)
r = Right(my_current_string, 4)
New_String_Goal = l & r
Debug.Print New_String_Goal

答案 2 :(得分:0)

假设您要获取第一个下划线之前的所有字符,我建议如下:

Function TrimFilename(fnm As String) As String
    Dim i As Long, j As Long
    i = InStr(fnm, "_")
    j = InStrRev(fnm, ".")
    If 0 < i And i < j Then
        TrimFilename = Mid(fnm, 1, i - 1) & Mid(fnm, j)
    Else
        TrimFilename = fnm
    End If
End Function
?TrimFilename("text_12_12_19.pdl")
text.pdl