Replacing the nth Occurrence of a Character in a String Homepage › Clovertech Forums › Read Only Archives › Cloverleaf › Cloverleaf › Replacing the nth Occurrence of a Character in a String Search for: This topic has 8 replies, 4 voices, and was last updated 14 years, 10 months ago by David Harrison. Creator Topic December 3, 2009 at 10:13 am #51392 David HarrisonParticipant I Creator Topic Viewing 7 reply threads Author Replies December 3, 2009 at 1:44 pm #70081 Jim KosloskeyParticipant Take a look at the string replace Tcl command. sting replace $some_string 6 6 “~” Might work for you. email: jim.kosloskey@jim-kosloskey.com 29+ years Cloverleaf, 59 years IT - old fart. December 3, 2009 at 1:58 pm #70082 David HarrisonParticipant Jim, That just replaces the 6th character, not the 6th occurrence of a character. This works but it’s a bit long winded. I wondered if there was a more concise method. Code: set imsg {10930,1234567890,123456,PATIENT,FNAME,1011945,TST1,Result 1,TST2,Result 2} set omsg [split $imsg “,”] set omsg [linsert $omsg 6 ~] set omsg [join $omsg ,] set omsg [string map -nocase {,~, ~} $omsg] puts $omsg Thanks, Dave December 3, 2009 at 3:26 pm #70083 Jim KosloskeyParticipant Sorry – I misunderstood what you wanted to accomplish. email: jim.kosloskey@jim-kosloskey.com 29+ years Cloverleaf, 59 years IT - old fart. December 3, 2009 at 3:28 pm #70084 Jim KosloskeyParticipant Does the inbound message have to have the tilde before Xlate or is it the outbound (from Xlate) message that needs the tilde? email: jim.kosloskey@jim-kosloskey.com 29+ years Cloverleaf, 59 years IT - old fart. December 3, 2009 at 4:12 pm #70085 Max Drown (Infor)Keymaster Try this: Code: set var “10930,1234567890,123456,PATIENT,FNAME,1011945,TST1,Result 1,TST2,Result 2” set res “10930,1234567890,123456,PATIENT,FNAME,1011945~TST1,Result 1,TST2,Result 2” regsub — {(.*?,.*?,.*?,.*?,.*?,.*?),(.*?,.*?,.*?,.*?)} $var {1-2} newVar puts $newVar #returns: 10930,1234567890,123456,PATIENT,FNAME,1011945-TST1,Result 1,TST2,Result 2 -- Max Drown (Infor) December 3, 2009 at 4:34 pm #70086 David HarrisonParticipant Jim, This is for pre-Xlate. The tilde is the terminator for the first VRL in my HRL. Max’s code works – thanks Max December 3, 2009 at 4:55 pm #70087 Tom RiouxParticipant Just thought I’d put out another way to skin the cat: set var “10930,1234567890,123456,PATIENT,FNAME,1011945,TST1,Result 1,TST2,Result 2” set ridx [lindex [regexp -all -indices -inline , $var] 5] set varout [string replace $var [lindex $ridx 0] [lindex $ridx 1] ~] RETURNS: 10930,1234567890,123456,PATIENT,FNAME,1011945~TST1,Result 1,TST2,Result 2 Hope this helps… Tom Rioux December 4, 2009 at 11:18 am #70088 David HarrisonParticipant Tom, That’s more elegant a solution than I could come up with, plus I can re-use the code for other manipulation I need to do. Many thanks, Dave Author Replies Viewing 7 reply threads The forum ‘Cloverleaf’ is closed to new topics and replies.