format string

Discussion in 'ASP General' started by Christopher Brandsdal, Sep 19, 2003.

  1. Just a little question...

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


    Christopher Brandsdal
     
    Christopher Brandsdal, Sep 19, 2003
    #1
    1. Advertisements

  2. Christopher Brandsdal

    Yarn Guest

    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" <> wrote in message
    news:...
    > Just a little question...
    >
    > How do I split 12345678 into 12 34 56 78???????
    >
    >
    > Christopher Brandsdal
    >
    >
     
    Yarn, Sep 19, 2003
    #2
    1. Advertisements

  3. Thanks!
    But is there not a more easy way to do this?

    :)


    "Yarn" <> skrev i melding
    news:...
    > 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" <> wrote in message
    > news:...
    > > Just a little question...
    > >
    > > How do I split 12345678 into 12 34 56 78???????
    > >
    > >
    > > Christopher Brandsdal
    > >
    > >

    >
    >
     
    Christopher Brandsdal, Sep 19, 2003
    #3
  4. Christopher Brandsdal

    Yarn Guest

    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" <> wrote in message
    news:...
    > Thanks!
    > But is there not a more easy way to do this?
    >
    > :)
    >
    >
    > "Yarn" <> skrev i melding
    > news:...
    > > 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" <> wrote in message
    > > news:...
    > > > Just a little question...
    > > >
    > > > How do I split 12345678 into 12 34 56 78???????
    > > >
    > > >
    > > > Christopher Brandsdal
    > > >
    > > >

    > >
    > >

    >
    >
     
    Yarn, Sep 19, 2003
    #4
  5. Christopher Brandsdal

    Evertjan. Guest

    Christopher Brandsdal wrote on 19 sep 2003 in
    >> "Christopher Brandsdal" <> wrote in message
    >> > How do I split 12345678 into 12 34 56 78???????
    >> >

    > "Yarn" <> 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)
     
    Evertjan., Sep 19, 2003
    #5
  6. Christopher Brandsdal

    Phill. W Guest

    "Yarn" <> wrote in message
    news:...
    .. . .
    > 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.
     
    Phill. W, Sep 19, 2003
    #6
  7. Christopher Brandsdal

    Tom B Guest

    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" <> wrote in message
    news:...
    > 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" <> wrote in message
    > news:...
    > > Just a little question...
    > >
    > > How do I split 12345678 into 12 34 56 78???????
    > >
    > >
    > > Christopher Brandsdal
    > >
    > >

    >
    >
     
    Tom B, Sep 19, 2003
    #7
  8. Christopher Brandsdal

    dlbjr Guest

    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
     
    dlbjr, Sep 21, 2003
    #8
  9. Christopher Brandsdal

    Yarn Guest

    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" <> wrote in message
    news:bkes24$l44$...
    > "Yarn" <> wrote in message
    > news:...
    > . . .
    > > 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.
    >
    >
     
    Yarn, Sep 21, 2003
    #9
  10. Christopher Brandsdal

    dlbjr Guest

    '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
     
    dlbjr, Sep 21, 2003
    #10
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gaurav
    Replies:
    0
    Views:
    16,486
    Gaurav
    Nov 7, 2003
  2. Brian Candy
    Replies:
    2
    Views:
    1,448
    Janaka
    Feb 18, 2004
  3. chongo
    Replies:
    2
    Views:
    8,363
    Edward
    Jul 8, 2004
  4. ankur
    Replies:
    1
    Views:
    14,431
    Jan =?UTF-8?B?VGhvbcOk?=
    Aug 27, 2007
  5. R. David Murray
    Replies:
    8
    Views:
    797
    Tim Chase
    Mar 27, 2009
  6. Andre Alexander Bell

    String formatting with the format string syntax

    Andre Alexander Bell, Sep 14, 2010, in forum: Python
    Replies:
    9
    Views:
    745
    Andre Alexander Bell
    Sep 15, 2010
  7. Chris Angelico
    Replies:
    3
    Views:
    406
    Mark Lawrence
    Mar 1, 2013
  8. Victor Hooi
    Replies:
    3
    Views:
    289
    Chris Kaynor
    Nov 27, 2013
Loading...