RE: Adding Characters to a string

From: Tom Lavedas (tlavedas_at_hotmail.remove.com)
Date: 07/01/04


Date: Thu, 1 Jul 2004 06:13:01 -0700

Maybe ...

CheckString = "565495423564898562124165"

If Len(CheckString) <> 29 then
  If Len(CheckString) = 24 then
    MyVar = Mid(CheckString,1,4)
    for i = 5 to 21 Step 4
      MyVar = MyVar & "-" & Mid(CheckString,i,4)
    Next
    Msgbox MyVar & vbcr & "5654-9542-3564-8985-6212-4165"
  Else
    'Something here
  End If
Else
  'Something here
End if

Or maybe this, which may be a few microsecconds faster ;-) ...

CheckString = "565495423564898562124165"

If Len(CheckString) <> 29 then
  If Len(CheckString) = 24 then
    Dim aTemp(5)
    n = 0
    for i = 1 to 21 Step 4
      aTemp(n) = Mid(CheckString,i,4)
      n = n + 1
    Next
    MyVar = Join(aTemp, "-")
    Msgbox MyVar & vbcr & "5654-9542-3564-8985-6212-4165"
  Else
    'Something here
  End If
Else
  'Something here
End if

Though your approach is okay, too.

Tom Lavedas
===========

"Michael Bouchard" wrote:

> I am prompting technicians for a serial number at the begining of an install
> and wanted to try to make sure that the serial number was formated
> correctly.
>
> The script below works but I am just wondering if there is a better way to
> add the - to the string
>
> Thanks,
>
> Mike
>
> CheckString = "565495423564898562124165"
>
> If Len(CheckString) <> 29 then
> If Len(CheckString) = 24 then
> MyVar = Mid(CheckString,1,4)
> MyVar = MyVar & "-" & Mid(CheckString,5,4)
> MyVar = MyVar & "-" & Mid(CheckString,9,4)
> MyVar = MyVar & "-" & Mid(CheckString,13,4)
> MyVar = MyVar & "-" & Mid(CheckString,17,4)
> MyVar = MyVar & "-" & Mid(CheckString,21,4)
> Msgbox MyVar & vbcr & "5654-9542-3564-8985-6212-4165"
> Else
> 'Something here
> End If
> Else
> 'Something here
> End if
>
>
>



Relevant Pages

  • Re: can someone please help correct this script, thank you.
    ... Mike -- FrontPage MVP '97-'02 ... > below is a script i have made up and i can not seem to get the number to add ... > should add the numbers together, not add then as a string. ...
    (microsoft.public.frontpage.programming)
  • Re: How to rewrite with awk?
    ... > I'm unfamiliar with tools such as sed & awk. ... Extract the string that matches a RE. ... This script will not only expand all the lines that say "include ... file) and not resetting ARGV(the tmp file), it then lets awk do any ...
    (comp.unix.shell)
  • Re: Slow string
    ... I see virtually no difference in execution time for the code you posted when using cscript versus using wscript; to confirm, I bracketed the code with an initial ... Are you actually entering the script name into a console window? ... You can use the exact same line for every wrapper script - and that includes wsf and js scripts; all you need to do is change the final "vbs" to "wsf" or "js" as appropriate. ... If you build a large string by small concatenations, the performance degrades geometrically - so does array resizing, because they both use the same nasty technique inherited from VB1 of copying the entire data structure to a new structure with the added element. ...
    (microsoft.public.scripting.vbscript)
  • Re: Function that returns date of file.
    ... string after the date/time when it is used by itself. ... Is that your entire script? ... I make an IF statement that required the 'equals equals'. ... designed database your job will be all that much harder. ...
    (alt.php)
  • Re: Script: Remote shutdown of all domain computers
    ... When trying to run the script from a XP workstation, where i am logged in as ... Dim strBase, strFilter, strAttributes, strQuery, objRecordSet ... 2000 or above and allow shutdown, ... Public Function AllComputersAs String() ...
    (microsoft.public.windows.server.scripting)