Solved

Insert Character Into String At Specific Interval

Posted on 2011-03-08
6
716 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

932 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now