2014-06-06

TCL, string trim 字串剔除

剔除所需字串前後的字元.

[Syntax]
  • string  trim  string1  char  --> 刪除 string1左右char字元.
  •                                                    不指定, 則刪除空白無效字元 \t \r \n \s
  • string  trimleft  string1 char  --> 刪除 string1左邊含有char字元
  • string  trimright  string  chars  --> 刪除 string1右邊含有char字元

[ExampleCode 1]
  • set text2 "HelloWorldPapa 111 abc 222 PapaWorldHello"

  • puts $text2\n
  • puts [ string trim $text2 "Hello"]
  • puts [ string trimleft $text2 "Hello"]
  • puts [ string trimright $text2 "Hello"]
[Output]
  • HelloWorldPapa 111 abc 222 PapaWorldHello
  •  
  • WorldPapa 111 abc 222 PapaWorld
  • World Papa 111 abc 222 PapaWorldHello
  • HelloWorldPapa 111 abc 222 PapaWorld

[ExampleCode 2] 

Purpose: 取 Linux process ID後, 將之移除.
  • set uartRes {
  • 1 {ps | grep mke2fs

  •  3387 root       0:42 mke2fs -j /dev/sda1 

  •  3404 root       0:00 grep mke2fs 
  • v3.7.1.9 tango3[~]# }
  • }

  • puts $uartRes
  • puts "A##############"
  • puts [lindex $uartRes 1]
  • puts "B##############"
  • puts [string trimleft [lindex $uartRes 1] "ps | grep mke2fs\n"]
  • puts "C##############"
  • puts [string trimright [string trimleft [lindex $uartRes 1] "ps | grep mke2fs\n"] "\n"]
  • puts "D##############"
  • puts [string range [string trimright [string trimleft [lindex $uartRes 1] "ps | grep mke2fs\n"] "\n"] 0 3]
  • puts "F##############"
[Output]


1 {ps | grep mke2fs

 3387 root       0:42 mke2fs -j /dev/sda1 

 3404 root       0:00 grep mke2fs 

v3.7.1.9 tango3[~]# }

A##############
ps | grep mke2fs

 3387 root       0:42 mke2fs -j /dev/sda1 

 3404 root       0:00 grep mke2fs 

v3.7.1.9 tango3[~]# 
B##############
3387 root       0:42 mke2fs -j /dev/sda1 

 3404 root       0:00 grep mke2fs 

v3.7.1.9 tango3[~]# 
C##############
3387 root       0:42 mke2fs -j /dev/sda1 

 3404 root       0:00 grep mke2fs 

v3.7.1.9 tango3[~]# 
D##############
3387
F##############


[程式拆解]

沒有留言:

張貼留言