excel 给定文本中的第一个和最后一个单词,如何从字符串中提取文本

2g32fytz  于 2023-03-20  发布在  其他
关注(0)|答案(3)|浏览(158)

我在一个单元格中有以下文本。
我正在使用堆栈溢出。
Stackoverflow是一个问答论坛。
假设我还有一句台词。
我已经将它拆分成一个数组。

Arr[0] = I am using stackoverflow.   
Arr[1] = Stackoverflow is a question and answer forum.   
Arr[2] = Let’s say I have one more line.

如何检索从Arr[1]forum的文本?
输出应为-answer forum

aurhwmvo

aurhwmvo1#

我假设您不知道单词“answer”出现在Arr(1)中的确切位置,并且单词“forum”没有直接出现在它后面,即变量Arr(1)可能包含字符串"Stack Overflow is a question and answer site and is not a forum like so many other sites"(这实际上是对Stack Overflow的更好描述!)

Dim temp As String
Dim result As String
arr(1) = "Stack Overflow is a question and answer site and is not a forum like so many other sites"

'Get everything after the first occurrence of "answer"
temp = Mid(arr(1), InStr(arr(1), "answer"))

'Get everything before the first occurrence of "forum"
result = Left(temp, InStr(temp, "forum") + 4)  ' + 4 because the InStr will point to the "f" of "forum"

'result will contain "answer site and is not a forum"
jxct1oxe

jxct1oxe2#

使用子字符串。

Dim substring As String = RIGHT(arr[1], 13)

其中13可以替换为子字符串的长度。确保长度不会导致索引越界异常。

nvbavucw

nvbavucw3#

根据您的用例,这可能不是最可靠的答案,但是right()函数可以工作。

answerString = Right(Arr(1), 13)
'answer forum.

相关问题