Solved

Insert Character Into String At Specific Interval

Posted on 2011-03-08
6
728 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

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…
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/…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

622 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