替换字符串中间的空格

goosen 2009-03-20 03:42:46
66CN iv2 23.1238004649805113.3175715760020.01721744704992 :10.008385081103780.01530017937794
66CN iv1-1 23.1238004355084113.3175715884120.04448469541967 :10.008455782939740.01566210830967
66CN iv2-1 23.1238004297506113.3175714049510.04365696199238 :10.008452657868280.01562435592060
66CN iv2-2 23.1238004145708113.3175714767230.03714695852250 :10.008169433887990.01508523549694
66CN iv3-1 23.1237798393336113.3182735959170.64396097045392 :10.008215704972690.01275585622886
66CN iv3-2 23.1237797747149113.3182735375400.63551265280694 :10.007952053369160.01235192509699
66CN iv4-1 23.1259227218252113.3178053028630.50226401258260 :10.005654885642490.00992212988610
66CN iv4-2 23.1259226665865113.3178053048600.48789828084409 :10.004982893653600.00942217567364
66CN iv4-3 23.1259226618187113.3178053178360.48833942785859 :10.004869894276490.00910980800294


替换后

66CN,iv4-1,23.1259227218252113.3178053028630.50226401258260,:10.005654885642490.00992212988610
66CN,iv4-2,23.1259226665865113.3178053048600.48789828084409,:10.004982893653600.00942217567364
66CN,iv4-3,23.1259226618187113.3178053178360.48833942785859,:10.004869894276490.00910980800294
中间的空格是不定的 有什么好方法吗?
...全文
120 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
sonic_andy 2009-03-20
  • 打赏
  • 举报
回复
set reg = new regexp
reg.pattern = "[ ]+"
dst = reg.replace(src,",")

src 是替换前的
dst 是替换后的
penguinhzf 2009-03-20
  • 打赏
  • 举报
回复
好像不需要如此复杂吧?
我的理解是:

Dim a As String
a = "asdf asdf fdsfo wo nwo n asf o a asdf dsf wer we r"
a = Replace(a, Space(1), "")
MsgBox a
应该能满足你的意思
东方之珠 2009-03-20
  • 打赏
  • 举报
回复
Option Explicit

Private Sub Command1_Click()
Dim cRichText() As String, cText() As String, S As String
Dim i As Integer, j As Integer
cRichText = Split(RichTextBox1.Text, vbCrLf)
For i = 0 To UBound(cRichText)
cText = Split(cRichText(i), " ")
If Len(cText(0)) <> 0 Then S = cText(0)
For j = 1 To UBound(cText)
If Len(cText(j)) <> 0 Then S = S & "," & cText(j)
Next
cRichText(i) = S
Next

RichTextBox1.Text = ""

For i = 0 To UBound(cRichText)
RichTextBox1.Text = RichTextBox1.Text & cRichText(i) & vbCrLf
Next

End Sub

Private Sub Form_Load()
RichTextBox1.FileName = "C:\66cn.txt" '加载文本内容
End Sub
东方之珠 2009-03-20
  • 打赏
  • 举报
回复
Option Explicit

Private Sub Command1_Click()
Dim cRichText() As String, cText() As String, S As String
Dim i As Integer, j As Integer
cRichText = Split(RichTextBox1.Text, vbCrLf)
For i = 0 To UBound(cRichText)
cText = Split(cRichText(i), " ")
If Len(cText(0)) <> 0 Then S = cText(0)
For j = 1 To UBound(cText)
If Len(cText(j)) <> 0 Then S = S & "," & cText(j)
Next
cRichText(i) = S
Next

RichTextBox1.Text = ""

For i = 0 To UBound(cRichText)
RichTextBox1.Text = RichTextBox1.Text & cRichText(i) & vbCrLf
Next

End Sub

Private Sub Form_Load()
RichTextBox1.FileName = "C:\66cn.txt" '加载文本内容
End Sub
yachong 2009-03-20
  • 打赏
  • 举报
回复
Private Sub Command1_Click()
Dim s As String
s = "66CN iv2 23.1238004649805113.3175715760020.01721744704992 :10.008385081103780.01530017937794 "
s = Trim(s) '去除尾空格
Do While InStr(s, " ") <> 0 '不管有多少个连续的空格,统统替换成一个空格
s = Replace(s, " ", " ")
Loop
s = Replace(s, " ", ",") & " " '空格替换成逗号,并加上尾空格
Debug.Print s
End Sub


这是处理一个字符串的例子
说老实话我不知道你给出的数据末尾的空格是实际存在的还是CSDN自作主张加上的
如果本来就不存在末尾的空格,就没必要专门对尾空格做处理

7,762

社区成员

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

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