Vba 从下划线分隔的字符串中提取PascalCase字符串和数字

Vba 从下划线分隔的字符串中提取PascalCase字符串和数字,vba,outlook,Vba,Outlook,给定字符串的格式为FirstnameLastname\u ABC\u11111\u 500 我需要将Firstname、Lastname和500提取成一个字符串,格式为:“Firstname Lastname/500”,而忽略其余部分(ABC、随机数和下划线) 不确定在VBA脚本中实现这一点的最佳方法是什么。我想说这将非常合适。我已经添加了一些安全网,但这还可以进一步改进。我已经将其编码为函数,但是,当然,您可以根据需要将块添加到代码中 此代码使用作为分隔符将(VBA.Split)输入拆分为多个

给定字符串的格式为FirstnameLastname\u ABC\u11111\u 500 我需要将Firstname、Lastname和500提取成一个字符串,格式为:“Firstname Lastname/500”,而忽略其余部分(ABC、随机数和下划线)


不确定在VBA脚本中实现这一点的最佳方法是什么。

我想说这将非常合适。我已经添加了一些安全网,但这还可以进一步改进。我已经将其编码为函数,但是,当然,您可以根据需要将块添加到代码中

此代码使用
作为分隔符将(
VBA.Split
)输入拆分为多个部分,然后逐个字符解析第一部分字符,检测大写字母并在其前面添加空格。我的全名由6个单词组成,因此我这样做是为了使此代码与所需的任意多个单词兼容。;)

最后,代码检测输入字符串中是否有更多的段,并将最后一个段添加到结果字符串中

Option Explicit

Public Function stringExtractor(givenString As String) As String
    
    If VBA.Len(givenString) > 0 Then    ' If the user enters an empty string, this prevents execution errors
    
        Dim stringParts() As String
        Dim finalResult As String
        Dim fullName As String
        Dim i As Integer
        Dim currentChar As String
        Dim lastPart As String
        Dim partCount As Integer
        
        stringParts = VBA.Split(givenString, "_")   ' Gets the original string split into its components, and loaded into stringParts
        
        fullName = stringParts(0)   ' Gets the name part into a variable of its own
        finalResult = ""
        i = 1
        
        Do Until i > VBA.Len(fullName)  ' Loops through all the name, to start loading it into the finalResult
        
            currentChar = VBA.Mid(fullName, i, 1)
            
            If currentChar = VBA.UCase(currentChar) And VBA.Len(finalResult) <> 0 Then ' If we find an uppercase character and finalResult is not empty (this is not the first word)
                finalResult = finalResult & " " & currentChar                           ' we are entering a new word, and we have to enter both the found character and a blank space
            Else
                finalResult = finalResult & currentChar                                ' Else, we're in the same word as before, and we only have to add the found character.
            End If
            
            i = i + 1
        Loop
        
        partCount = UBound(stringParts) ' Get how many parts has the string (zero-based)
        
        If partCount > 0 Then   ' If there was something else than just the name
            lastPart = stringParts(partCount)   ' Get the last bit of the string
            
            finalResult = finalResult & " / " & lastPart    ' And attach it to the end result
        End If
        
    End If
    
    stringExtractor = finalResult   ' This will return the full name, separating the words with spaces and (if there is one) the number at the end of the original string.
        
End Function

希望这足以回答您的问题,请不要犹豫,向我询问更多详细信息。

对于
拆分功能来说似乎是一项不错的工作-它在PascalCase中是不相关的-哦,是否还要将名字和姓氏分开?我想您可以逐个字符解析第一个拆分结果字符,并检查其大小写。如果它是Keo-BiRANN怎么办?BRAX使用拆分函数逐字解析字符吗?不,首先用下划线来分割它,这给了你一个数组,并且数组中的第一个项目将有第一个/最后一个名字,然后你可以通过字符解析字符,试图找出中间的大写字母在哪里,假设只有一个。
Option Explicit

Public Function stringExtractor(givenString As String) As String
    
    If VBA.Len(givenString) > 0 Then    ' If the user enters an empty string, this prevents execution errors
    
        Dim stringParts() As String
        Dim finalResult As String
        Dim fullName As String
        Dim i As Integer
        Dim currentChar As String
        Dim lastPart As String
        Dim partCount As Integer
        
        stringParts = VBA.Split(givenString, "_")   ' Gets the original string split into its components, and loaded into stringParts
        
        If partCount > 0 Then   ' If there was something else than just the name
            fullName = stringParts(0)   ' Gets the name part into a variable of its own
            finalResult = ""
            i = 1
            
            Do Until i > VBA.Len(fullName)  ' Loops through all the name, to start loading it into the finalResult
            
                currentChar = VBA.Mid(fullName, i, 1)
                
                If currentChar = VBA.UCase(currentChar) And VBA.Len(finalResult) <> 0 Then ' If we find an uppercase character and finalResult is not empty (this is not the first word)
                    finalResult = finalResult & " " & currentChar                           ' we are entering a new word, and we have to enter both the found character and a blank space
                Else
                    finalResult = finalResult & currentChar                                ' Else, we're in the same word as before, and we only have to add the found character.
                End If
                
                i = i + 1
            Loop
            
            partCount = UBound(stringParts) ' Get how many parts has the string (zero-based)
            lastPart = stringParts(partCount)   ' Get the last bit of the string
            
            finalResult = finalResult & " / " & lastPart    ' And attach it to the end result
            
        End If
        
    End If
    
    stringExtractor = finalResult   ' This will return the full name, separating the words with spaces and (if there is one) the number at the end of the original string.
        
End Function