从VBA中的字符串中删除子字符串(remove a sub-string from a string in VBA)
我在VBA中有两个字符串可以说,
Dim str1 as String Dim str2 as String str = "I.3) Haupttätigkeit(en)"和
str2= "I.3)"我想从str1中删除str2。 在VBA有可能吗?
这就是我在做的事情:
Dim strResult As String ' Position of str1 intPos = InStr(1, str1, str2) If intPos > 0 Then ' str2 found, strResult = Left(str1, intPos - 1) Else ' str2 not found, so take whole string strResult = str1 End IfI have two strings in VBA lets say,
Dim str1 as String Dim str2 as String str = "I.3) Haupttätigkeit(en)"and
str2= "I.3)"I want to remove str2 from str1. Is it possible in VBA ?
This is what I am doing:
Dim strResult As String ' Position of str1 intPos = InStr(1, str1, str2) If intPos > 0 Then ' str2 found, strResult = Left(str1, intPos - 1) Else ' str2 not found, so take whole string strResult = str1 End If最满意答案
你需要的是使用Replace function很简单:
str1 = Replace(str1, str2, "") 'result: Haupttätigkeit(en)但是,它会在更换后保留前导空间,因此您可以尝试这种方式(删除前导或结束空格):
str1 = Trim(Replace(str1, str2, "")) 'result:Haupttätigkeit(en)或者,您可以使用此处所述的 Mid Statement 。
What you need is quite simple to do with Replace function:
str1 = Replace(str1, str2, "") 'result: Haupttätigkeit(en)However, it will keep leading space after replacement, therefore you could try in this way (removing leading or ending spaces):
str1 = Trim(Replace(str1, str2, "")) 'result:Haupttätigkeit(en)Alternatively you could use Mid Statement as described here.
更多推荐
发布评论