469,358 Members | 1,599 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,358 developers. It's quick & easy.

format string

Just a little question...

How do I split 12345678 into 12 34 56 78???????
Christopher Brandsdal
Jul 19 '05 #1
9 4263
You could try this, although it's a little funky-
counter = 2
myString = "12345678"
WHILE counter <= 8
response.write right(left("12345678",counter),2)&"<br>"
counter=counter+2
WEND

"Christopher Brandsdal" <ch***********@c2i.net> wrote in message
news:u4**************@TK2MSFTNGP11.phx.gbl...
Just a little question...

How do I split 12345678 into 12 34 56 78???????
Christopher Brandsdal

Jul 19 '05 #2
Thanks!
But is there not a more easy way to do this?

:)
"Yarn" <no**@noaddress.com> skrev i melding
news:e4*************@TK2MSFTNGP12.phx.gbl...
You could try this, although it's a little funky-
counter = 2
myString = "12345678"
WHILE counter <= 8
response.write right(left("12345678",counter),2)&"<br>"
counter=counter+2
WEND

"Christopher Brandsdal" <ch***********@c2i.net> wrote in message
news:u4**************@TK2MSFTNGP11.phx.gbl...
Just a little question...

How do I split 12345678 into 12 34 56 78???????
Christopher Brandsdal


Jul 19 '05 #3

Probably. This really all there is to it though-

response.write right(left("12345678",2),2)&"<br>"
response.write right(left("12345678",4),2)&"<br>"
response.write right(left("12345678",6),2)&"<br>"
response.write right(left("12345678",8),2)&"<br>"

First we grab the two over from the left, than two over from the right.
Than we grab the fourth over from the left, than two over from the right.
and so on..

"Christopher Brandsdal" <ch***********@c2i.net> wrote in message
news:uD**************@tk2msftngp13.phx.gbl...
Thanks!
But is there not a more easy way to do this?

:)
"Yarn" <no**@noaddress.com> skrev i melding
news:e4*************@TK2MSFTNGP12.phx.gbl...
You could try this, although it's a little funky-
counter = 2
myString = "12345678"
WHILE counter <= 8
response.write right(left("12345678",counter),2)&"<br>"
counter=counter+2
WEND

"Christopher Brandsdal" <ch***********@c2i.net> wrote in message
news:u4**************@TK2MSFTNGP11.phx.gbl...
Just a little question...

How do I split 12345678 into 12 34 56 78???????
Christopher Brandsdal



Jul 19 '05 #4
Christopher Brandsdal wrote on 19 sep 2003 in
"Christopher Brandsdal" <ch***********@c2i.net> wrote in message
> How do I split 12345678 into 12 34 56 78???????
>

"Yarn" <no**@noaddress.com> skrev i melding
You could try this, although it's a little funky-

counter = 2
myString = "12345678"
WHILE counter <= 8
response.write right(left("12345678",counter),2)&"<br>"
counter=counter+2
WEND


But is there not a more easy way to do this?


<%
myString = "12345678"
Set regEx = New RegExp
regEx.Pattern = "(..)(?=.)"
regEx.Global = True
newString = regEx.Replace(myString, "$1 ")

response.write newString & "<br>"
%>

============================================

Works for any length any char.

The (..) selects to char's,
that are replaced "$1 " by the same plus a space.

The (?=.) looks ahead for another char,
so that an end space is suppressed.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 19 '05 #5
myString="12345678"
Dim iLoop
for iLoop = 1 to len(myString) Step 2
Response.write mid(myString,iLoop,2)
next

'Untested....but it should work
"Yarn" <no**@noaddress.com> wrote in message
news:e4*************@TK2MSFTNGP12.phx.gbl...
You could try this, although it's a little funky-
counter = 2
myString = "12345678"
WHILE counter <= 8
response.write right(left("12345678",counter),2)&"<br>"
counter=counter+2
WEND

"Christopher Brandsdal" <ch***********@c2i.net> wrote in message
news:u4**************@TK2MSFTNGP11.phx.gbl...
Just a little question...

How do I split 12345678 into 12 34 56 78???????
Christopher Brandsdal


Jul 19 '05 #6
"Yarn" <no**@noaddress.com> wrote in message
news:eG*************@TK2MSFTNGP12.phx.gbl...
.. . .
response.write right(left("12345678",2),2)&"<br>"

.. . .

(Yes, it's /definitely/ VBScript, so...)

What, pray tell, is wrong with using Mid() ???

sData = "12345678"
Response.Write Mid( sData, 1, 2 ) _
& " " & Mid( sData, 3, 2 ) _
& " " & Mid( sData, 5, 2 ) _
& " " & Mid( sData, 7, 2 )

Regards,
Phill W.
Jul 19 '05 #7
Try this test to make your decision on what to do.
<%
strString = "12345678"
start = Timer()
For i = 1 To 1000
RexEx(strString)
Next
response.write "RegularExpersion = " & GetTime(start) & "<br>"
start = Timer()
For i = 1 To 1000
MidLoop(strString)
Next
response.write "MidLoop = " & GetTime(start) & "<br>"
start = Timer()
For i = 1 To 1000
MidHardCode(strString)
Next
response.write "MidHardCode = " & GetTime(start) & "<br>"
start = Timer()
For i = 1 To 1000
ArrayBreak(strString)
Next
response.write "ArrayBreak = " & GetTime(start) & "<br>"

Function GetTime(start)
GetTime = FormatNumber(Timer() - start,4)
End Function

Function MidLoop(strData)
for iLoop = 1 to len(strData) Step 2
MidLoop = mid(strData,iLoop,2) & " "
next
End Function

Function MidHardCode(strData)
MidHardCode = Mid( strData, 1, 2 ) _
& " " & Mid( strData, 3, 2 ) _
& " " & Mid( strData, 5, 2 ) _
& " " & Mid( strData, 7, 2 )
End Function

Function RexEx(strData)
Set regEx = New RegExp
regEx.Pattern = "(..)(?=.)"
regEx.Global = True
RexEx = regEx.Replace(strData, "$1 ")
End Function

Function ArrayBreak(strData)
Dim aryData()
intLen = Len(strData)
If intLen > 0 Then
ReDim aryData((intLen \ 2) + 1)
intCount = 0
for iLoop = 1 to len(strData) Step 2
strItem = Trim(mid(strData,iLoop,2))
If Len(strItem) > 0 Then
aryData(intCount) = strItem
intCount = intCount + 1
End If
next
ArrayBreak = Trim(Join(aryData," "))
End If
End Function
%>
--
-dlbjr

Discerning resolutions for the alms
Jul 19 '05 #8
I liked my first one better.
Keeps my clients from trying to hack my code themselves.
Obsfucating is the key to long term clients!

(what a weasel, I know..)

counter = 2
myString = "12345678"
WHILE counter <= 8
response.write right(left("12345678",counter),2)&"<br>"
counter=counter+2
WEND


"Phill. W" <P.******@open.ac.uk> wrote in message
news:bk**********@yarrow.open.ac.uk...
"Yarn" <no**@noaddress.com> wrote in message
news:eG*************@TK2MSFTNGP12.phx.gbl...
. . .
response.write right(left("12345678",2),2)&"<br>"

. . .

(Yes, it's /definitely/ VBScript, so...)

What, pray tell, is wrong with using Mid() ???

sData = "12345678"
Response.Write Mid( sData, 1, 2 ) _
& " " & Mid( sData, 3, 2 ) _
& " " & Mid( sData, 5, 2 ) _
& " " & Mid( sData, 7, 2 )

Regards,
Phill W.

Jul 19 '05 #9
'Here is a timing test

<%
strString = "12345678"
start = Timer()
For i = 1 To 1000
RexEx(strString)
Next
response.write "RegularExpersion = " & GetTime(start) & "<br>"
start = Timer()
For i = 1 To 1000
MidLoop(strString)
Next
response.write "MidLoop = " & GetTime(start) & "<br>"
start = Timer()
For i = 1 To 1000
MidHardCode(strString)
Next
response.write "MidHardCode = " & GetTime(start) & "<br>"
start = Timer()
For i = 1 To 1000
ArrayBreak(strString)
Next
response.write "ArrayBreak = " & GetTime(start) & "<br>"

Function GetTime(start)
GetTime = FormatNumber(Timer() - start,4)
End Function

Function MidLoop(strData)
for iLoop = 1 to len(strData) Step 2
MidLoop = mid(strData,iLoop,2) & " "
next
End Function

Function MidHardCode(strData)
MidHardCode = Mid( strData, 1, 2 ) _
& " " & Mid( strData, 3, 2 ) _
& " " & Mid( strData, 5, 2 ) _
& " " & Mid( strData, 7, 2 )
End Function

Function RexEx(strData)
Set regEx = New RegExp
regEx.Pattern = "(..)(?=.)"
regEx.Global = True
RexEx = regEx.Replace(strData, "$1 ")
End Function

Function ArrayBreak(strData)
Dim aryData()
intLen = Len(strData)
If intLen > 0 Then
ReDim aryData((intLen \ 2) + 1)
intCount = 0
for iLoop = 1 to len(strData) Step 2
strItem = Trim(mid(strData,iLoop,2))
If Len(strItem) > 0 Then
aryData(intCount) = strItem
intCount = intCount + 1
End If
next
ArrayBreak = Trim(Join(aryData," "))
End If
End Function
%>
--
-dlbjr

Discerning resolutions for the alms
Jul 19 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by san | last post: by
6 posts views Thread by Stuart McGraw | last post: by
11 posts views Thread by Grumble | last post: by
2 posts views Thread by Bob | last post: by
7 posts views Thread by Alpha | last post: by
4 posts views Thread by David Morris | last post: by
6 posts views Thread by Scewbedew | last post: by
8 posts views Thread by Lucky | last post: by
8 posts views Thread by Armando Rocha | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.