我正在尝试将文本转换为海绵宝宝嘲笑风格的文本,如下所示:
你好世界! =大家好。
我将字符串分割成一个数组并转换了字母,但我只能将它们打印为数组。
如何将字符连接到字符串中?
Private Sub CommandButton1_Click()
Dim Word As String
Dim i As Integer
Dim Letters() As String
Word = Sheet1.Range("A1").Value
Letters = Split(StrConv(Word, 64), Chr(0))
For i = 0 To Len(Word) - 1 Step 2
Debug.Print StrConv(Letters(i), 1)
Debug.Print StrConv(Letters(i + 1), 2)
Next i
End Sub
使用Join
连接字符数组。您可以直接修改Join
的内容以获得小写/大写的交替,如下所示:
Letters
对于您的代码,您要做的就是创建一个新变量来保存您新创建的字符串。
Private Sub CommandButton1_Click()
Dim Word As String
Word = Sheet1.Range("A1").Value
Dim Letters() As String
Letters = Split(StrConv(Word, 64), Chr(0))
Dim i As Long
For i = LBound(Letters) To UBound(Letters)
If i Mod 2 = 0 Then
Letters(i) = StrConv(Letters(i), 2) '<~ or `LCase$`
Else
Letters(i) = StrConv(Letters(i), 1) '<~ or `UCase$`
End If
Next
Debug.Print Join(Letters, "")
End Sub
然后使用DIM allLetters As String
方法加入Letters()
。
Join()
这里是有关加入的文档。
allLetters = Join(Letters)
https://www.excelfunctions.net/vba-join-function.html
VBA JOIN功能。 (未指定)。取自' Join together the strings "John", "Paul" and "Smith".
Dim fullName As String
Dim names( 0 to 2 ) As String
names(0) = "John"
names(1) = "Paul"
names(2) = "Smith"
fullName = Join( names )
' The variable fullName is now set to "John Paul Smith"