format string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Christopher Brandsdal

    format string

    Just a little question...

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


    Christopher Brandsdal


  • Yarn

    #2
    Re: format string

    You could try this, although it's a little funky-


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



    "Christophe r Brandsdal" <christopherwb@ c2i.net> wrote in message
    news:u4muyvnfDH A.3700@TK2MSFTN GP11.phx.gbl...[color=blue]
    > Just a little question...
    >
    > How do I split 12345678 into 12 34 56 78???????
    >
    >
    > Christopher Brandsdal
    >
    >[/color]


    Comment

    • Christopher Brandsdal

      #3
      Re: format string

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

      :)


      "Yarn" <nope@noaddress .com> skrev i melding
      news:e4cUN8nfDH A.392@TK2MSFTNG P12.phx.gbl...[color=blue]
      > You could try this, although it's a little funky-
      >
      >
      > counter = 2
      > myString = "12345678"
      > WHILE counter <= 8
      > response.write right(left("123 45678",counter) ,2)&"<br>"
      > counter=counter +2
      > WEND
      >
      >
      >
      > "Christophe r Brandsdal" <christopherwb@ c2i.net> wrote in message
      > news:u4muyvnfDH A.3700@TK2MSFTN GP11.phx.gbl...[color=green]
      > > Just a little question...
      > >
      > > How do I split 12345678 into 12 34 56 78???????
      > >
      > >
      > > Christopher Brandsdal
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • Yarn

        #4
        Re: format string


        Probably. This really all there is to it though-

        response.write right(left("123 45678",2),2)&"< br>"
        response.write right(left("123 45678",4),2)&"< br>"
        response.write right(left("123 45678",6),2)&"< br>"
        response.write right(left("123 45678",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..





        "Christophe r Brandsdal" <christopherwb@ c2i.net> wrote in message
        news:uDhrvGofDH A.3200@tk2msftn gp13.phx.gbl...[color=blue]
        > Thanks!
        > But is there not a more easy way to do this?
        >
        > :)
        >
        >
        > "Yarn" <nope@noaddress .com> skrev i melding
        > news:e4cUN8nfDH A.392@TK2MSFTNG P12.phx.gbl...[color=green]
        > > You could try this, although it's a little funky-
        > >
        > >
        > > counter = 2
        > > myString = "12345678"
        > > WHILE counter <= 8
        > > response.write right(left("123 45678",counter) ,2)&"<br>"
        > > counter=counter +2
        > > WEND
        > >
        > >
        > >
        > > "Christophe r Brandsdal" <christopherwb@ c2i.net> wrote in message
        > > news:u4muyvnfDH A.3700@TK2MSFTN GP11.phx.gbl...[color=darkred]
        > > > Just a little question...
        > > >
        > > > How do I split 12345678 into 12 34 56 78???????
        > > >
        > > >
        > > > Christopher Brandsdal
        > > >
        > > >[/color]
        > >
        > >[/color]
        >
        >[/color]


        Comment

        • Evertjan.

          #5
          Re: format string

          Christopher Brandsdal wrote on 19 sep 2003 in[color=blue][color=green]
          >> "Christophe r Brandsdal" <christopherwb@ c2i.net> wrote in message[color=darkred]
          >> > How do I split 12345678 into 12 34 56 78???????
          >> >[/color][/color]
          > "Yarn" <nope@noaddress .com> skrev i melding[color=green]
          >> You could try this, although it's a little funky-
          >>
          >> counter = 2
          >> myString = "12345678"
          >> WHILE counter <= 8
          >> response.write right(left("123 45678",counter) ,2)&"<br>"
          >> counter=counter +2
          >> WEND[/color]
          >
          > But is there not a more easy way to do this?[/color]

          <%
          myString = "12345678"
          Set regEx = New RegExp
          regEx.Pattern = "(..)(?=.)"
          regEx.Global = True
          newString = regEx.Replace(m yString, "$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)

          Comment

          • Tom B

            #6
            Re: format string

            myString="12345 678"
            Dim iLoop
            for iLoop = 1 to len(myString) Step 2
            Response.write mid(myString,iL oop,2)
            next

            'Untested....bu t it should work


            "Yarn" <nope@noaddress .com> wrote in message
            news:e4cUN8nfDH A.392@TK2MSFTNG P12.phx.gbl...[color=blue]
            > You could try this, although it's a little funky-
            >
            >
            > counter = 2
            > myString = "12345678"
            > WHILE counter <= 8
            > response.write right(left("123 45678",counter) ,2)&"<br>"
            > counter=counter +2
            > WEND
            >
            >
            >
            > "Christophe r Brandsdal" <christopherwb@ c2i.net> wrote in message
            > news:u4muyvnfDH A.3700@TK2MSFTN GP11.phx.gbl...[color=green]
            > > Just a little question...
            > >
            > > How do I split 12345678 into 12 34 56 78???????
            > >
            > >
            > > Christopher Brandsdal
            > >
            > >[/color]
            >
            >[/color]


            Comment

            • Phill.  W

              #7
              Re: format string

              "Yarn" <nope@noaddress .com> wrote in message
              news:eGqfnPofDH A.460@TK2MSFTNG P12.phx.gbl...
              .. . .[color=blue]
              > response.write right(left("123 45678",2),2)&"< br>"[/color]
              .. . .

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


              Comment

              • dlbjr

                #8
                Re: format string

                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 "RegularExpersi on = " & GetTime(start) & "<br>"
                start = Timer()
                For i = 1 To 1000
                MidLoop(strStri ng)
                Next
                response.write "MidLoop = " & GetTime(start) & "<br>"
                start = Timer()
                For i = 1 To 1000
                MidHardCode(str String)
                Next
                response.write "MidHardCod e = " & GetTime(start) & "<br>"
                start = Timer()
                For i = 1 To 1000
                ArrayBreak(strS tring)
                Next
                response.write "ArrayBreak = " & GetTime(start) & "<br>"

                Function GetTime(start)
                GetTime = FormatNumber(Ti mer() - start,4)
                End Function

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

                Function MidHardCode(str Data)
                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(s trData, "$1 ")
                End Function

                Function ArrayBreak(strD ata)
                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(strDat a,iLoop,2))
                If Len(strItem) > 0 Then
                aryData(intCoun t) = strItem
                intCount = intCount + 1
                End If
                next
                ArrayBreak = Trim(Join(aryDa ta," "))
                End If
                End Function
                %>


                --
                -dlbjr

                Discerning resolutions for the alms


                Comment

                • Yarn

                  #9
                  Re: format string

                  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("123 45678",counter) ,2)&"<br>"
                  counter=counter +2
                  WEND








                  "Phill. W" <P.A.Ward@open. ac.uk> wrote in message
                  news:bkes24$l44 $1@yarrow.open. ac.uk...[color=blue]
                  > "Yarn" <nope@noaddress .com> wrote in message
                  > news:eGqfnPofDH A.460@TK2MSFTNG P12.phx.gbl...
                  > . . .[color=green]
                  > > response.write right(left("123 45678",2),2)&"< br>"[/color]
                  > . . .
                  >
                  > (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.
                  >
                  >[/color]


                  Comment

                  • dlbjr

                    #10
                    Re: format string

                    'Here is a timing test

                    <%
                    strString = "12345678"
                    start = Timer()
                    For i = 1 To 1000
                    RexEx(strString )
                    Next
                    response.write "RegularExpersi on = " & GetTime(start) & "<br>"
                    start = Timer()
                    For i = 1 To 1000
                    MidLoop(strStri ng)
                    Next
                    response.write "MidLoop = " & GetTime(start) & "<br>"
                    start = Timer()
                    For i = 1 To 1000
                    MidHardCode(str String)
                    Next
                    response.write "MidHardCod e = " & GetTime(start) & "<br>"
                    start = Timer()
                    For i = 1 To 1000
                    ArrayBreak(strS tring)
                    Next
                    response.write "ArrayBreak = " & GetTime(start) & "<br>"

                    Function GetTime(start)
                    GetTime = FormatNumber(Ti mer() - start,4)
                    End Function

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

                    Function MidHardCode(str Data)
                    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(s trData, "$1 ")
                    End Function

                    Function ArrayBreak(strD ata)
                    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(strDat a,iLoop,2))
                    If Len(strItem) > 0 Then
                    aryData(intCoun t) = strItem
                    intCount = intCount + 1
                    End If
                    next
                    ArrayBreak = Trim(Join(aryDa ta," "))
                    End If
                    End Function
                    %>


                    --
                    -dlbjr

                    Discerning resolutions for the alms


                    Comment

                    Working...