关于字符串操作的简单问题?

iedge 2004-10-17 10:12:42
现在要实现以下功能:
输入一个字符串,程序要把它拆分成一个一个的字符,并在每两个字符之间加入一个“&”分隔符,重新生成一个新的字符串。
例如:
从ComboBox输入:"csdn技术论坛"
输出: "&c&s&d&n&技&术&论&坛&"

输入可以是英文也可以是中文,字符串长度可以变化,求一代码,先谢谢各位大侠了^_^
...全文
193 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
KiteGirl 2004-10-18
  • 打赏
  • 举报
回复
下面代码的好处是速度特别快。

Public Function StringReMoveCode(ByVal pString As String, Optional ByVal pReMoveCode = &H26) As String
'去码函数
Dim tOutString As String

Dim tReMoveString As String

tReMoveString = Chr(pReMoveCode)

tOutString = Replace(pString, tReMoveString, "")

StringReMoveCode = tOutString
End Function

Public Function StringAddCode(ByVal pString As String, Optional ByVal pAddCode = &H26) As String
'加码函数
Dim tOutString As String

Dim tSurBytes() As Byte
Dim tSurBytes_Length As Byte
Dim tSurBytes_Count As Byte

Dim tDesBytes() As Byte
Dim tDesBytes_Length As Byte
Dim tDesBytes_Count As Byte

tSurBytes() = pString
tSurBytes_Length = UBound(tSurBytes())
tSurBytes_Count = tSurBytes_Length + 1

tDesBytes_Count = tSurBytes_Count * 2
tDesBytes_Length = tDesBytes_Count - 1

ReDim tDesBytes(tDesBytes_Length)

Dim tSurIndex_Base As Long
Dim tSurIndex As Long

Dim tDesIndex As Long
Dim tDesIndex_Base As Long
Dim tDesIndex_AddCode As Long

Dim tPatchIndex As Long

For tSurIndex_Base = 0 To tSurBytes_Length Step 2

tDesIndex_AddCode = tSurIndex_Base * 2
tDesIndex_Base = tDesIndex_AddCode + 2

For tPatchIndex = 0 To 1
tSurIndex = tSurIndex_Base + tPatchIndex
tDesIndex = tDesIndex_Base + tPatchIndex

tDesBytes(tDesIndex) = tSurBytes(tSurIndex)

Next

tDesBytes(tDesIndex_AddCode) = pAddCode

Next

tOutString = tDesBytes()

StringAddCode = tOutString
End Function

Private Sub Form_Load()
'测试代码
Dim tString_Sur As String
Dim tString_AddCode As String
Dim tString_ReMoveCode As String

tString_Sur = "KiteGirl小仙妹是个好孩子!"

tString_AddCode = StringAddCode(tString_Sur)
tString_ReMoveCode = StringReMoveCode(tString_AddCode)

Text1.Text = tString_Sur
Text2.Text = tString_AddCode
Text3.Text = tString_ReMoveCode

End Sub
KiteGirl 2004-10-18
  • 打赏
  • 举报
回复
少等!
cso 2004-10-17
  • 打赏
  • 举报
回复
chr(38) 或者 "&" 都可以代替
futongdl 2004-10-17
  • 打赏
  • 举报
回复

For i = 1 To Len(字符串)
xx = xx & "&" & Mid(Me.Text1, i, 1)
Next i
字符串= xx & "&"
victorycyz 2004-10-17
  • 打赏
  • 举报
回复

Private Function GetAndS(s As String) As String

Dim ts As String, i As Integer
For i = 1 To Len(s)
ts = ts & Mid(s, i, 1) & "&"
Next i
GetAndS = Left(ts, Len(ts) - 1)

End Function

Private Sub Command1_Click()

Print GetAndS("CS中文")

End Sub
fredlin 2004-10-17
  • 打赏
  • 举报
回复
Private Sub Command1_Click()
Dim str As String
Dim str1 As String
Dim i As Integer
str = Text1

For i = 1 To Len(str)
str1 = str1 & "&" & Mid(str, i, 1)
Next

str1 = str1 & "&" '最后个 &

Debug.Print str1
End Sub
helanshan 2004-10-17
  • 打赏
  • 举报
回复
支持
BlueBeer 2004-10-17
  • 打赏
  • 举报
回复
Private Sub Command1_Click()
Dim s As String, n As String, i As Long
s = "csdn技术论坛"
For i = 1 To Len(s)
n = n & "&" & Mid(s, i, 1)
Next i
n = n & "&"
MsgBox n
End Sub
northwolves 2004-10-17
  • 打赏
  • 举报
回复
Private Sub Command1_Click()
MsgBox outstring("csdn技术论坛")
End Sub
Function outstring(ByVal instring As String) As String
Dim i As Integer, temp() As String
ReDim temp(Len(instring) + 1)
For i = 1 To Len(instring)
temp(i) = Mid(instring, i, 1)
Next
outstring = Join(temp, "&")
Erase temp
End Function

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧