通过添加空格来分隔子字符串

时间:2015-10-28 15:34:52

标签: string vbscript

我有一个文本文件(.txt),我需要在一行中添加许多空格。 我的输入字符串在一个连续的字符串中有4个日期(未分割)。例如:

20150612201506122015061220150612

必须通过添加空格yyyyMMdd yyyyMMdd yyyyMMdd yyyyMMdd

每8个字符进行分割
20150612 20150612 20150612 20150612

我尝试使用leftright函数,但我不知道如何使用它们剪切8个字符并插入空格" "

我该怎么做?

1 个答案:

答案 0 :(得分:0)

您可以通过快速For循环执行此操作:

strIn = "20150612201506122015061220150612"

'add a space every 8 characters
For intChar = 1 To Len(strIn) Step 8
    strOut = strOut & Mid(strIn, intChar, 8) & " "
Next

'remove last space
strOut = Left(strOut, Len(strOut) - 1)

从你的评论中,如果字符串中包含可变长度的字符串,这是分割字符串的另一种方法:

strIn = "020031000420150612201506122015061220150612"
'to transform in 02 003 1 0004 20150612 20150612 20150612 20150612"

intStart = 1
For Each charCount In Array(2, 3, 1, 4, 8, 8, 8, 8)
    strOut = strOut & Mid(strIn, intStart, charCount) & " "
    intStart = intStart + charCount
Next


'remove last space
intStr = Left(strOut, Len(strOut) - 1)

这里我们使用For each charCount in Array(),字段列表长度在数组中。

相关问题