在string列表中的字符之间插入符号“>”

我在Excel中有一个列,像这样:

ABC BCD BA 

我怎样才能把符号“>”之间的string中的每个字母? 预期的输出应该是这样的:

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

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

单线matlab解决scheme将是:

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

说明:

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

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

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

testing:

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

这是工作的macros代码

 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 

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

 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 

在其他答案完整的情况下,你可以加载文件在MATLAB中通过strjoin更改string:

 [~,~,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); 

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

MID(A1,1,1) “>” &MID(A1,2,2)& “>” …..

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

如果单词长度不一样,可以写一个简单的macros。 伪代码将是: –

对于我= 1到len(string)

范围( “B1”)= MID(A1,I,I)& “>”

接下来我

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

你将需要创build一个自定义函数,获取值,循环和放置的字符,从它的外观,你想跳过,当字符是一个空格…我还添加了能力,改变你的字符正在插入,而不是硬编码。 就像是:

 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 

在MATLAB(需要17a双引号和16b带/replace)

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