Solved

Insert Character Into String At Specific Interval

Posted on 2011-03-08
6
714 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

746 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

10 Experts available now in Live!

Get 1:1 Help Now