2017-07-05 113 views
2

的名单我有这样的Excel中的列:插入符号“>”字符之间的字符串

ABC 
BCD 
BA 

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

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

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

回答

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>;>'>.> 
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 
3

单线MATLAB的解决办法是:

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

说明:

  • cellstr(str(:)):char数组到单元阵列
  • strjoin转换:加入所有的具有给定分隔符的单元格,即>
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 
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); 
1

如果字是相同的长度,你可以直接使用以下公式: -

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

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

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

对于i = 1至LEN(字符串)

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

下一个I

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

+0

欢迎来到SO!好的第一个答案!使用大写字母I作为变量名称可能会造成混淆,它看起来并不像您正在使用此变量。你能澄清如何删除最后>符号吗? –

+0

大写或小写在vba中无关紧要。你可以写左(txt,len(txt)-1)去除最后一个符号。 – user8260627

+0

我的意思是“I”可能会混淆为小写字母L或数字1。您的psuedocode仍然不正确,因为您没有在任何地方使用该变量。请编辑你的答案。 –

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 
+0

这是工作代码,因为我之前的回答引起了混淆。 – user8260627

1

在MATLAB(需要双引号和16b的条17A /替换)

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

如果str是一个单元阵列呢?第一行为 – kgk

+0

,出现错误:“错误:输入字符在MATLAB语句或表达式中无效” – kgk

+0

strip和replace将在cellstrs上工作。在17a中添加了用于字符串创建的双引号,并且在16b中添加了条/替换。如果您使用的是16b之前的版本,OmG看起来有正确的解决方案。 – matlabbit