【问题标题】:Concatenate one string to another with removing duplicated part通过删除重复部分将一个字符串连接到另一个字符串
【发布时间】:2020-03-17 08:11:30
【问题描述】:

这听起来很熟悉,如果我重复一遍,我很抱歉,但我还没有找到答案。 具体来说,我遇到了这种情况,有两个字符串:

示例:

案例 1 互联网站点 URL "https://mainwebsite.com:8080/specialpage/" 来自该站点某处 HTML 元素的 href 的文件 URL "/specialpage/files/videos/excel-vba/Concatenate%20one%20string%20to%20another%20with%20removing%20duplicated%20part.zip"

案例 2 YYYYMMDD 格式的全名和日期“John Smith 20191121” 和 YYYYMMDD格式的日期和工作描述“20191121解决问题”

第一个字符串的结尾与第二个字符串的开头相同,只是长度未知。

在这两种情况下,我都需要加入字符串,同时从其中一个中删除相同的部分 案例 1 结果“https://mainwebsite.com:8080/specialpage/files/videos/excel-vba/Concatenate%20one%20string%20to%20another%20with%20removing%20duplicated%20part.zip 案例 2 结果“John Smith 20191121 解决问题”

【问题讨论】:

    标签: excel vba string concatenation


    【解决方案1】:

    多亏了 IIF 功能,这种情况有一个解决方案:

    Dim String1, String2, String3 as String
    Dim I as Long
    
    ' comparing parts of both strings, counting down from the length of the shorter one (IIF function)
    For i = IIf(Len(String1) < Len(String2), Len(String1), Len(String2)) To 0 Step -1
    
    ' considering right side of String1 and left side of String2...
        If Right(String1, i) = Left(String2, i) Then
    
            ' for example, overlapping was in "/specialpage/"; we took String1 without this part and joined it with String2
            String3 = Left(String1, Len(String1) - i) & String2
            Exit For
    
        End If
    
    ' the length of both strings' parts we compare is gradually shortened...
    Next i
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-13
      • 2017-06-06
      • 1970-01-01
      • 1970-01-01
      • 2018-10-15
      • 1970-01-01
      • 2015-02-05
      相关资源
      最近更新 更多