【问题标题】:Insert a symbol ">" between the characters in a list of strings在字符串列表中的字符之间插入符号“>”
【发布时间】:2017-07-05 16:34:58
【问题描述】:

我在 Excel 中有这样的列:

ABC
BCD
BA

如何将符号“>”放在字符串中的每个字母之间?预期的输出应该是这样的:

A>B>C
B>C>D
B>A

或者我们可以在 Matlab 中做到这一点吗?

【问题讨论】:

    标签: string matlab excel insert vba


    【解决方案1】:

    单行 matlab 解决方案是:

    strjoin(cellstr(str(:)), '>')
    

    说明:

    • cellstr(str(:)):将 char 数组转换为元胞数组
    • strjoin:用给定的分隔符连接所有单元格,即>

    【讨论】:

      【解决方案2】:

      如果你只有 ASCII 字符,你可以试试这个简单的 UDF:

      Function insertChar(s As String, c As String) As String
        insertChar = Join(Split(StrConv(s, vbUnicode), vbNullChar), c)
      End Function
      

      测试:

      A1: abcd;'.
      B1: =insertChar(A1, ">")   ----->   a>b>c>d>;>'>.>
      

      【讨论】:

        【解决方案3】:

        这是工作宏代码

        Sub gt()
        
        Dim a As Integer, b As String, U(100) As String, J
        
        b = Selection
        a = Len(Selection)
        Selection.Offset(0, 1).Select
        
        
        For i = 1 To a
        
        If i <> a Then
        J = J & Mid(b, i, 1) & ">"
        Else
        J = J & Mid(b, i, 1)
        End If
        
        Next i
        
        ActiveCell = J
        
        End Sub
        

        【讨论】:

        • 这是工作代码,因为我之前的回答引起了混乱。
        【解决方案4】:

        这是一个有趣的问题!试试这个方法。

        Sub InsertCharacter()
        
        Dim Rng As Range
        Dim InputRng As Range, OutRng As Range
        Dim xRow As Integer
        Dim xChar As String
        Dim index As Integer
        Dim arr As Variant
        Dim xValue As String
        Dim outValue As String
        Dim xNum As Integer
        
        Set InputRng = Application.Selection
        Set InputRng = Application.InputBox("Range :", xTitleId, InputRng.Address, Type:=8)
        xRow = Application.InputBox("Number of characters :", xTitleId, Type:=1)
        xChar = Application.InputBox("Specify a character :", xTitleId, Type:=2)
        Set OutRng = Application.InputBox("Out put to (select range):", xTitleId, Type:=8)
        Set OutRng = OutRng.Range("A1")
        xNum = 1
        For Each Rng In InputRng
            xValue = Rng.Value
            outValue = ""
            For index = 1 To VBA.Len(xValue)
                If index Mod xRow = 0 And index <> VBA.Len(xValue) Then
                    outValue = outValue + VBA.Mid(xValue, index, 1) + xChar
                Else
                    outValue = outValue + VBA.Mid(xValue, index, 1)
                End If
            Next
            OutRng.Cells(xNum, 1).Value = outValue
            xNum = xNum + 1
        Next
        End Sub
        

        【讨论】:

          【解决方案5】:

          在其他答案的完整情况下,您可以在matlab中加载文件并通过strjoin更改字符串:

          [~,~,raw] = xlsread(fileName);
          num = length(raw{:,1});
          for rawNum = 1:num
              str = raw{rawNum,1};
              raw{rawNum,1} = strjoin(cellstr(str(:)), '>');
          end 
          xlswrite(fileName, raw);
          

          【讨论】:

            【解决方案6】:

            如果单词长度相同,可以直接使用公式:-

            MID(A1,1,1)&">"&MID(A1,2,2)&">".....

            这将提取字符并在它们之间插入“>”。

            如果单词的长度不同,您可以编写一个简单的宏。伪代码是:-

            对于 i = 1 到 len(string)

            范围("B1")=mid(A1,i,i)&">"

            下一个

            然后使用 left(txt,len(txt)-1) 删除最后一个 > 符号。

            【讨论】:

            • 欢迎来到 SO!很好的第一个答案!使用大写的 I 作为变量名可能会造成混淆,而且看起来您使用的不是这个变量。你能解释一下如何删除最后一个 > 符号吗?
            • 大写或小写在 vba 中无关紧要。你可以写 left(txt,len(txt)-1) 来删除最后一个符号。
            • 我的意思是“我”可能会被混淆为小写 L 或数字一。您的伪代码仍然不正确,因为您没有在任何地方使用该变量。请编辑您的答案。
            • 谢谢。我使用了小写的 I。我自己在 1 和 I 之间感到困惑 :)
            【解决方案7】:

            您需要创建一个自定义函数,该函数接收值、循环并放置字符,从外观上看,当字符为空格时您想跳过...我还添加了更改的功能您即时插入的字符,而不是硬编码。比如:

            Public Function AddGTSigns(strIn As String, strCharToAdd As String) As String
                Dim strOut As String
                Dim lngCount As Integer
                Dim lngLength As Integer
                Dim strNextChar As String
                lngLength = Len(strIn)
            
                For lngCount = 1 To lngLength
                    strOut = strOut & Mid(strIn, lngCount, 1)
                    If lngCount < lngLength Then
                        'Check next character'
                        If Mid(strIn, lngCount, 1) <> " " Then
                            strOut = strOut & strCharToAdd
                        End If
                    End If
                Next lngCount
            
                AddGTSigns = strOut
            
            End Function
            
            Private Sub RunIt()
                Dim strTest As String
            
                strTest = AddGTSigns("ABC CDE GHE", ">")
            
                MsgBox strTest
            End Sub
            

            【讨论】:

              【解决方案8】:

              在 MATLAB 中(双引号需要 17a,剥离/替换需要 16b)

              >> str = ["ABC"; "BCD"; "BA"];
              >> str = strip(replace(str,'','>'),'>')
              
              str = 
              
                3×1 string array
              
                  "A>B>C"
                  "B>C>D"
                  "B>A"
              

              Regexprep 也适用于旧版本的 MATLAB

              >> str = {'ABC';'BCD';'BA'};
              >> str = regexprep(str,'(.)(?=.)','$1>')
              
              str =
              
                3×1 cell array
              
                  {'A>B>C'}
                  {'B>C>D'}
                  {'B>A'  }
              

              【讨论】:

              • 如果 str 是一个元胞数组呢?
              • 第一行出现错误:“Error: The input character is not valid in MATLAB statements or expressions”
              • 剥离和替换将在 cellstrs 上工作。在 17a 中添加了用于创建字符串的双引号,在 16b 中添加了剥离/替换。如果您使用的是 16b 之前的版本,OmG 似乎有正确的解决方案。
              • 也添加了正则表达式示例。
              最近更新 更多