VBA#trim字符串删除字符

问题描述 投票:-2回答:3

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

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

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

excel vba ms-access access-vba
3个回答
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

1
投票

你可以使用Split:

MyStringGoal = Split(MyCurrentString, "_")(0) & "." & Split(MyCurrentString, ".")(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
© www.soinside.com 2019 - 2024. All rights reserved.