[英]Insert a symbol “>” between the characters in a list of strings
我在Excel中有一个列,如下所示:
ABC
BCD
BA
如何在字符串中的每个字母之间放置符号“>”? 预期的输出应该是这样的:
A>B>C
B>C>D
B>A
或者我们可以在Matlab中这样做吗?
单线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
你想要创建一个自定义函数,它接收值,循环并放置字符,当你想要在空格中跳过时,你想要跳过它...我还添加了改变角色的能力你正在插入,而不是硬编码。 就像是:
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中加载文件,并通过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(字符串)
范围( “B1”)= MID(A1,I,I)& “>”
下一个我
然后使用left(txt,len(txt)-1)删除last>符号。
在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' }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.