Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 如何在字符串中查找带引号的文本? 例子_String_Vba_Excel_Double Quotes - Fatal编程技术网

String 如何在字符串中查找带引号的文本? 例子

String 如何在字符串中查找带引号的文本? 例子,string,vba,excel,double-quotes,String,Vba,Excel,Double Quotes,假设我有一个字符串: “我说“你好,世界”,她说“对不起” VBA将此字符串解释为: 我说“你好,世界”,她说“对不起” 一个更复杂的例子: 我有一个字符串: “我说”“你知道她说”“你好吗!” VBA将此字符串解释为: 我说“你知道她说“你好吗!” 如果我们删除“我说” “你知道她说了声“你好吗!” 我们可以继续在vba中解析字符串: 你知道她说了声“你好吗!” 问题 最后,我需要一个函数sBasicQuote(quotedStringHierarchy as string),它返回一个字符串

假设我有一个字符串:

“我说“你好,世界”,她说“对不起”

VBA将此字符串解释为:

我说“你好,世界”,她说“对不起”

一个更复杂的例子: 我有一个字符串:

“我说”“你知道她说”“你好吗!”

VBA将此字符串解释为:

我说“你知道她说“你好吗!”

如果我们删除“我说”

“你知道她说了声“你好吗!”

我们可以继续在vba中解析字符串:

你知道她说了声“你好吗!”

问题 最后,我需要一个函数sBasicQuote(quotedStringHierarchy as string),它返回一个字符串,该字符串包含字符串层次结构中的下一级

例如

我只是想不出一个算法能解决这个问题。。。您几乎需要替换所有双引号,但是当您替换第n个双引号时,您必须跳到第n+1个双引号

如何在VBA中实现这一点?

我的解决方案 我花了更多的时间思考并想出了这个解决方案

Function sMineDoubleQuoteHierarchy(s As String) As String
    'Check the number of quotes in the string are even - sanity check
    If (Len(s) - Len(Replace(s, """", ""))) Mod 2 <> 0 Then sMineDoubleQuoteHierarchy = "Error - Odd number of quotes found in sMineDoubleQuoteHierarchy() function": Exit Function

    'First thing to do is find the first and last *single* quote in the string
    Dim lStart, lEnd, i As Long, fs As String
    lStart = InStr(1, s, """")
    lEnd = InStrRev(s, """")

    'After these have been found we need to remove them.
    s = Mid(s, lStart + 1, lEnd - lStart - 1)

    'Start at the first character
    i = 1

    Do While True
        'Find where the next double quote is
        i = InStr(1, s, """""")

        'if no double quote is found then concatenate with fs with the remainder of s
        If i = 0 Then Exit Do

        'Else add on the string up to the char before the ith quote
        fs = fs & Left(s, i - 1)

        'Replace the ith double quote with a single quote
        s = Left(s, i - 1) & Replace(s, """""", """", i, 1)

        'Increment by 1 (ensuring the recently converted double quote is no longer a single quote
        i = i + 1
    Loop

    'Return fs
    sMineDoubleQuoteHierarchy = s
End Function

你可以这样做

Public Sub test()

Dim s As String
s = "I say ""Did you know that she said """"Hi there!"""""""

Debug.Print DoubleQuote(s, 0)
Debug.Print DoubleQuote(s, 1)
Debug.Print DoubleQuote(s, 2)

End Sub

Public Function DoubleQuote(strInput As String, intElement As Integer) As String

Dim a() As String

strInput = Replace(strInput, String(2, Chr(34)), String(1, Chr(34)))

a = Split(strInput, chr(34))

DoubleQuote = a(intElement)


End Function
另一个稍加修改的版本更精确一些

`Public Function DoubleQuote(strInput As String, intElement As Integer) As String

Dim a() As String
Dim b() As String
Dim i As Integer

ReDim b(0)

a = Split(strInput, Chr(34))
'   ***** See comments re using -1 *******
For i = 0 To UBound(a) - 1

    If Len(a(i)) = 0 Then
        b(UBound(b)) = Chr(34) & a(i + 1) & Chr(34)
        i = i + 1
    Else
        b(UBound(b)) = a(i)

    End If

    ReDim Preserve b(UBound(b) + 1)

Next i

DoubleQuote = b(intElement)

End Function`

我认为下面将返回您在嵌套报价示例中寻找的内容。您的第一个示例实际上不是嵌套引号的情况

Option Explicit
Sub NestedQuotes()
    Const s As String = "I say ""Did you know that she said """"Hi there!"""""""
    Dim COL As Collection
    Dim Start As Long, Length As Long, sTemp As String, V As Variant

Set COL = New Collection
sTemp = s
COL.Add sTemp
Do Until InStr(sTemp, Chr(34)) = 0
    sTemp = COL(COL.Count)
    sTemp = Replace(sTemp, String(2, Chr(34)), String(1, Chr(34)))
        Start = InStr(sTemp, Chr(34)) + 1
        Length = InStrRev(sTemp, Chr(34)) - Start
    sTemp = Mid(sTemp, Start, Length)
    COL.Add sTemp
Loop

For Each V In COL
    Debug.Print V
Next V

End Sub

是否尝试调用
Replace()
?@SLaks使用Replace不是“无限可扩展的”。从某种意义上说,
将被替换为
而不是
”@Sancarn:你确定吗?好吧,
替换
不会在替换的子字符串中替换。(虽然我没有检查VBA)@SLaks你是对的。这简化了事情……我喜欢你使用字符串(2,Chr(34))的方式事实上,由于这些字符串中引号的数量变得非常混乱!我很惊讶它如此简单!我没有意识到,正如SLaks指出的,“替换不会在替换的子字符串中替换”。这大大简化了问题!@Nathan_Sav your
Ubound(a)-1
忽略了数组的最后一项。我想你的意思只是
Ubound(a)
@VictorMoraes:是的,但是需要在“替换行”中添加错误捕获。执行此操作时,
a(I+1)
行需要检查
I+1=Ubound(a)
`Public Function DoubleQuote(strInput As String, intElement As Integer) As String

Dim a() As String
Dim b() As String
Dim i As Integer

ReDim b(0)

a = Split(strInput, Chr(34))
'   ***** See comments re using -1 *******
For i = 0 To UBound(a) - 1

    If Len(a(i)) = 0 Then
        b(UBound(b)) = Chr(34) & a(i + 1) & Chr(34)
        i = i + 1
    Else
        b(UBound(b)) = a(i)

    End If

    ReDim Preserve b(UBound(b) + 1)

Next i

DoubleQuote = b(intElement)

End Function`
Option Explicit
Sub NestedQuotes()
    Const s As String = "I say ""Did you know that she said """"Hi there!"""""""
    Dim COL As Collection
    Dim Start As Long, Length As Long, sTemp As String, V As Variant

Set COL = New Collection
sTemp = s
COL.Add sTemp
Do Until InStr(sTemp, Chr(34)) = 0
    sTemp = COL(COL.Count)
    sTemp = Replace(sTemp, String(2, Chr(34)), String(1, Chr(34)))
        Start = InStr(sTemp, Chr(34)) + 1
        Length = InStrRev(sTemp, Chr(34)) - Start
    sTemp = Mid(sTemp, Start, Length)
    COL.Add sTemp
Loop

For Each V In COL
    Debug.Print V
Next V

End Sub