Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Insert Character Into String At Specific Interval

Posted on 2011-03-08
6
Medium Priority
?
737 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 500 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

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…
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: …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

688 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