在路径中间查找字符串 [英] Finding a string in the middle of a path
问题描述
我尝试在路径中间找到一个字符串.在两个反斜杠之后,我遇到了查找"反斜杠的问题.
I try to find a string in the middle of a path. I am encountering issues with 'finding' backslashes after two backslashes.
以下两个公式都使我获得相同的数字.我相信是因为在第二个反斜杠之后它缺少'+1',所以第三个反斜杠开始在第二个反斜杠处重新计数.
The below two formula both get me the same number. I believe it is because after the second backslash it is missing the '+1', so the third starts counting again at the second one.
如何增加以下反斜杠计数"以访问更远的路径?
\path1\path2\path3\path4\path5
=FIND("\";A2;FIND("\";A2; FIND("\"; A2; FIND("\"; A2)+1)))
这从"path2"开始.
This starts with 'path2'.
=FIND("\";A2;FIND("\";A2; FIND("\"; A2; FIND("\"; A2; FIND("\";A2)+1))))
这也让我获得了"path2".
This gets me also the 'path2'.
是否可以使用FIND()
根据反斜杠选择路径的一部分?
Is there a way to select parts of the path based on the backslashes with FIND()
?
推荐答案
您的公式(均为两者)简化为
Your formulas (both of them) reduce to
2nd \ =FIND("\",A2,FIND("\",A2)+1)
如果要转到下一个"反斜杠,则必须+1
循环中的每个起始位置.
If you want to go to the "next" backslash, you have to +1
each starting postion in the loop.
如果使用的是FIND
,则使用SUBSTITUTE
将所需的\
替换为唯一字符,然后将FIND
替换为该字符要简单得多.
If you are using FIND
, much simpler to use SUBSTITUTE
to replace the desired \
with a unique character and then FIND
that character.
但是更容易的是将字符串拆分成一个数组,然后返回适当的数组元素.
But even easier would be to split the string into an array, and then return the appropriate array element.
使用Excel 2016,您可以使用FILTERXML
函数轻松完成此操作:
With Excel 2016 you have the FILTERXML
function that can easily do this:
=INDEX(FILTERXML("<t><s>" & SUBSTITUTE(A2,"\","</s><s>") & "</s></t>","//s"),3)
请注意,因为您的字符串以\
开头,所以所需元素的索引号将为n+1
而不是n
(即3
将返回path2
)
Note that, because your string starts with a \
, the index number for the desired element will be n+1
and not n
(ie. 3
will return path2
)
对于早期的excel版本,还有其他方法可以做到这一点.
And there are other ways to do this for earlier excel versions.
这篇关于在路径中间查找字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!