Home > Blockchain >  tcl string or regex expression to join the substring at last
tcl string or regex expression to join the substring at last

Time:03-01

Currently, I'm removing the first position substring, but I wanted to join the same first position substring at the last position for all the strings mentioned in the file (Note: Here each substring is categorized by underscore)

file.txt
        A_Class 
        B_Class 
        A_Name_Student 
        B_Name_Student 
        Marks_Mid_Term_Student
        Marks_Student_Subject
    

so far I tried to remove the first substring but was unable to join it at last,

        set school [string range $school [expr {[string first "_" $school]   1}] end]

Basically, I wanted to print it as

        Class_A
        Class_B
        Name_Student_A
        Name_Student_B
        Mid_Term_Student_Marks
        Student_Subject_Marks
        
        

CodePudding user response:

There are lots of ways to do this. Here is one way, converting the input string to a list of the elements which were separated by "_", rearranging the list, then converting back to string form:

set schl [split $school _]
set schl [lassign $schl first]
lappend schl $first
set school [join $schl _]
  • Related