Solved

Insert Character Into String At Specific Interval

Posted on 2011-03-08
6
723 Views
Last Modified: 2012-05-11
Hello,

Using Classic ASP (not ASP.net), I wish to manipulate a string as follows:

Original String: ABCDEFGHIJKLMNOPQRSTUVWXYZ
New String: |AB|CD|EF|GH|IJ|KL|MN|OP|QR|ST|UV|WX|YZ

So basically, starting at the beginning by inserting the character, and after every two characters of the string, I wish to insert a character - in this case, the pipe symbol.

I am sure there is some sort of easy loop that can accomplish this, but I just can't think of it.

Any help someone can provide would be greatly appreciated.
0
Comment
Question by:nyxano
6 Comments
 
LVL 15

Accepted Solution

by:
pateljitu earned 125 total points
ID: 35073676
This code will give you the output:

dim strreplace, intllp
dim pipeString

strreplace = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
intllp = 1

for i = 1 to len(strreplace)
   
    strnewstr = mid(strreplace, intllp, 2)
    pipeString = pipeString &"|" & strnewstr
   
    intllp = intllp + 2
   
    if intllp >= len(strreplace) then exit for
   
   
next

response.Write(pipeString)
0
 
LVL 3

Expert Comment

by:kolakanuru1
ID: 35073782
       Dim s As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        Dim i As Integer = 0
        Dim interval As Integer = 2
        For i = 0 To s.Length + s.Length / interval - 1
            s = s.Insert(i, "|")
            i = i + interval
        Next


0
 
LVL 29

Expert Comment

by:Badotz
ID: 35074936
Just for grins, if you go to Tools -> References and set a reference to Microsoft VBScript Regular Expressions 5.5, then you can use the following:


Function Rexplace(str, ptrn, swap) As String
    
    Dim rex As RegExp
    
    ' Create regular expression.
    Set rex = New RegExp
    
    rex.Global = True
    rex.IgnoreCase = True
    rex.Pattern = ptrn
    
    ' Make replacement.
    Rexplace = rex.Replace(str, swap)
    
    Set rex = Nothing

End Function


Sub rex_test()
    
    Dim str As String
    str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    MsgBox Rexplace(str, "(..)", "$1|") '' Separate each pair of letters with a pipe char

End Sub

Open in new window

0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 29

Expert Comment

by:Badotz
ID: 35074963
Or, as you want the pipe before the pairs of letters:




MsgBox Rexplace(str, "(..)", "|$1") '' Separate each pair of letters with a pipe char

Open in new window

0
 

Author Closing Comment

by:nyxano
ID: 35090110
Accepted as this was the first solution provided, and it worked.
0
 
LVL 29

Expert Comment

by:Badotz
ID: 35090569
(sigh) Elegance counts for nothing, eh?

No worries.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SP to delete duplicates 15 73
ASP Button to clear text 4 62
Select only the top record in a left join 13 45
ASP exit 10 20
I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question