我想在字符串中找到子字符串的位置但面临一些问题。这是代码
Function findPos( Searchval As String, Output As String) As Long
Dim pos, i, count As Long
pos = InStr(1, content, searchVal, 0)
If pos = 0 Then
count = 0
Else
count = 1
End If
If pos > 0 Then
For i = 1 To pos
If Mid(content, i, 1) = "/" Then count = count + 1
Next i
End If
findPos=count
End Function
例如:如果输出是“AA / AE_ABC / AE / CD”,如果我的searchVal是“AE”那么我输出的位置为2,这是错误的因为我应该得到3.我知道代码中的 pos 必须以某种方式进行修改,但无法弄清楚。
答案 0 :(得分:2)
如果您只是想找到字符串的位置,请使用此
Sub Sample()
Debug.Print findPos("AE", "AA/AE_ABC/AE/CD")
End Sub
Function findPos(Searchval As String, Output As String) As Long
findPos = InStr(1, Output, Searchval, 0)
End Function
顺便说一句,该职位是4
而不是3
编辑:如果您在" /"之后寻找位置然后尝试这个
Sub Sample()
Debug.Print findPos("AE", "AA/AE_ABC/AE/CD")
End Sub
Function findPos(Searchval As String, Output As String) As Long
Dim MyAr
Dim i As Long
'~~> Check if output has "/"
If InStr(1, Output, "/", 0) Then
'~~> Split it and store it in an array
MyAr = Split(Output, "/")
'~~> Loop through the array to find an exact match
For i = LBound(MyAr) To UBound(MyAr)
If MyAr(i) = Searchval Then
findPos = i + 1
Exit Function
End If
Next i
Else
'~~> Check if both Searchval and Output are same
If Output = Searchval Then findPos = 1
End If
End Function
答案 1 :(得分:1)
这样的事情应该对你有用,为了清晰起见:
NodeRef file = getFirstPackageItem(execution)
Object filenameprefix = task.getVariable("mymodel_regnumber");
Object filenamesuffix = task.getVariable("mymodel_sender");
nodeService.setProperty(file, ContentModel.PROP_NAME, filenameprefix+"/"+filenamesuffix );