【发布时间】:2017-07-05 16:34:58
【问题描述】:
我在 Excel 中有这样的列:
ABC
BCD
BA
如何将符号“>”放在字符串中的每个字母之间?预期的输出应该是这样的:
A>B>C
B>C>D
B>A
或者我们可以在 Matlab 中做到这一点吗?
【问题讨论】:
标签: string matlab excel insert vba
我在 Excel 中有这样的列:
ABC
BCD
BA
如何将符号“>”放在字符串中的每个字母之间?预期的输出应该是这样的:
A>B>C
B>C>D
B>A
或者我们可以在 Matlab 中做到这一点吗?
【问题讨论】:
标签: string matlab excel insert vba
单行 matlab 解决方案是:
strjoin(cellstr(str(:)), '>')
说明:
cellstr(str(:)):将 char 数组转换为元胞数组strjoin:用给定的分隔符连接所有单元格,即>
【讨论】:
如果你只有 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>;>'>.>
【讨论】:
这是工作宏代码
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更改字符串:
[~,~,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)&">".....
这将提取字符并在它们之间插入“>”。
如果单词的长度不同,您可以编写一个简单的宏。伪代码是:-
对于 i = 1 到 len(string)
范围("B1")=mid(A1,i,i)&">"
下一个
然后使用 left(txt,len(txt)-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
【讨论】:
在 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' }
【讨论】: