format string

  • Thread starter Christopher Brandsdal
  • Start date
C

Christopher Brandsdal

Just a little question...

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


Christopher Brandsdal
 
Y

Yarn

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
 
Y

Yarn

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..
 
E

Evertjan.

Christopher Brandsdal wrote on 19 sep 2003 in
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.
 
P

Phill. W

.. . .
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.
 
T

Tom B

myString="12345678"
Dim iLoop
for iLoop = 1 to len(myString) Step 2
Response.write mid(myString,iLoop,2)
next

'Untested....but it should work
 
D

dlbjr

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
%>
 
Y

Yarn

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
 
D

dlbjr

'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
%>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top