×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Change String - Add and Replace Characters
2

Change String - Add and Replace Characters

Change String - Add and Replace Characters

(OP)
Hi, I am hoping the experts can point me in the right direction on this one. I have never attempted this and do not know where to begin. Is this a regular expression function? I don't know.... so here is what I need to do:

Change this:

CODE

CR4-7,CR10,CR40-42 

To This:

CODE

CR4,CR5,CR6,CR7,CR10,CR40,CR41,CR42 

How to add the missing numbers where there is a "-" along with adding the "CR" to the prefix. If someone can lead me a little that would be great.

RE: Change String - Add and Replace Characters

Of course regular expressions are great tool to test if the element is a compound element i.e. CR<from_num>-<to_num>

For example you can do something like this:

CODE

my_line_in = "CR4-7,CR10,CR40-42"
my_line_out = ""
' create regexp object
set re = new regexp
' regexp pattern
re.pattern = "CR\d+\-\d+"
' split string into array
my_array = split(my_line_in,",")

' process array elements
for each array_element in my_array
  if re.test(array_element) then
    ' if the element match the pattern, i.e. compound element
    my_string = replace(array_element,"CR","")
    my_range=split(my_string,"-")
    from_num = my_range(0)
    to_num=my_range(1)
    for i=from_num to to_num
      my_element = "CR" & i
      call add_to_line_out
    next
  else
    ' if it's simple element
    my_element = array_element
    call add_to_line_out
  end if
next

' write input
WScript.StdOut.writeline("my_line_in  = " & my_line_in)
' write output
WScript.StdOut.writeline("my_line_out = " & my_line_out)

sub add_to_line_out
  ' add my_element to string
  if my_line_out = "" then
    my_line_out = my_element
  else 
    my_line_out = my_line_out & "," & my_element
  end if
end sub 

Output:

CODE

C:\Work>cscript /NoLogo nu2java2.vbs
my_line_in  = CR4-7,CR10,CR40-42
my_line_out = CR4,CR5,CR6,CR7,CR10,CR40,CR41,CR42 

RE: Change String - Add and Replace Characters

And a non-regex solution. Same idea though:

CODE

sStart = "CR4-7,CR10,CR40-42"

arr1 = Split(sStart, ",")
For x = LBound(arr1) To UBound(arr1)

   If InStr(arr1(x), "-") > 0 Then
      sExpand = ""
      arr1(x) = Replace(arr1(x), "CR", "")
      arr2 = Split(arr1(x), "-")

      For i = CInt(arr2(0)) to CInt(arr2(1))
         sExpand = sExpand & ",CR" & i
      Next
      sExpand = Mid(sExpand, 2) 

      arr1(x) = sExpand
   End If
Next

sEnd = Join(arr1, ",") 

RE: Change String - Add and Replace Characters

Hi guitarzan,
But, by the way, it looks like this:
if you do not get correct data in your script (what could happen), for example instead of
sStart = "CR4-7,CR10,CR40-42"
you will get
sStart = "CRAB-7,CR10,CRXY-42"
then it seems that your script crashes on the conversion using CInt().

RE: Change String - Add and Replace Characters

Correct, what I posted is not production-ready code, it's just an illustration.

RE: Change String - Add and Replace Characters

(OP)
Thanks mikrom & guitarzan... this is more than enough to get me started. I appreciate the help.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close