local txt1 = " a bcde fg " local txt2 = "a bcde fg " local txt3 = " a bcde fg" txt1 = string.gsub(txt1, "^%s*(.-)%s*$", "%1") txt2 = string.gsub(txt2, "^%s*(.-)%s*$", "%1") txt3 = string.gsub(txt3, "^%s*(.-)%s*$", "%1") print("============") txt1 = "[start]".. txt1 .."[end]" txt2 = "[start]".. txt2 .."[end]" txt3 = "[start]".. txt3 .."[end]" print(txt1) print(txt2) print(txt3) print("==============")
结果如下