插入符号">"字符串列表中的字符之间

时间:2017-07-05 16:34:58

标签: excel string matlab excel-vba insert vba

我在Excel中有一个列,如下所示:

ABC
BCD
BA

如何设置符号">"字符串中的每个字母之间?预期的输出应该是这样的:

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

或者我们可以在Matlab中这样做吗?

8 个答案:

答案 0 :(得分:3)

单行matlab解决方案将是:

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

<强>解释

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

答案 1 :(得分: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>;>'>.>

答案 2 :(得分:2)

这是工作宏代码

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

答案 3 :(得分:1)

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

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

答案 4 :(得分:1)

你想要创建一个自定义函数,它接收值,循环并放置字符,当你想要在空格中跳过时,你想要跳过它...我还添加了这个能力更改您正在插入的字符,而不是硬编码。类似的东西:

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

答案 5 :(得分:1)

在其他答案的完整情况下,您可以在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 :(得分:1)

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

MID(A1,1,1)及;&#34;&GT;&#34;&安培; MID(A1,2,2)及;&#34;&GT;&#34; .....

这将提取字符并插入&#34;&gt;&#34;他们之间。

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

表示i = 1到len(字符串)

范围(&#34; B1&#34;)= MID(A1,I,I)及;&#34;&GT;&#34;

下一个

然后删除最后一个&gt;使用left(txt,len(txt)-1)签名。

答案 7 :(得分:1)

在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'  }