String Manipulations

Clovertech Forums Read Only Archives Cloverleaf Tcl Library String Manipulations

  • Creator
    Topic
  • #51365
    Ganesan Murali
    Participant

      What is the best method to replace the second character in a variable string to a last character?

      Example

      From : XZ94363459445

      To: X94363459445Z

    Viewing 4 reply threads
    • Author
      Replies
      • #69895
        Robert Kersemakers
        Participant

          This would be my way of doing this:

          Code:

          set test “XZ94363459445”
          set result “[string range $test 0 0][string range $test 2 end][string range $test 1 1]”

          Zuyderland Medisch Centrum; Heerlen/Sittard; The Netherlands

        • #69896

          Agreed.

          -- Max Drown (Infor)

        • #69897
          Charlie Bursell
          Participant

            C’mon Max, how about a regular expression:   😀

            regsub — {(w)(w)(.*$)} test {132} result

            or (two commands instead of three)

            set result [string replace “$test[string index $test 1]” 1 1]

            Just making the point that there are many ways to accomplish the same thing.  I can’t really say one way is better than the other   😉

          • #69898

            Charlie Bursell wrote:

            C’mon Max, how about a regular expression:

            -- Max Drown (Infor)

          • #69899
            Ganesan Murali
            Participant

              Thanks a lot guys for your quick response.

          Viewing 4 reply threads
          • The forum ‘Tcl Library’ is closed to new topics and replies.