Tcl / Tk language learning ------ split string

Split string

Foreword

  Working with strings of each language is a problem often encountered, tcl as a scripting language is no exception, split on a string, usually there are two issues: 1. The use of a single character split, 2. split string.

1. split a single character string

  

 

  As shown, a single character string can be easily divided.

2. string into string

  

  When we use the string into a string, tcl will use each character in the string to split, can not achieve our objective.

  Then we can define a quick way to use a string split string, the following is what I split.tcl a new file:

1 proc OnePiece {str sep} {
2     split [string map [list $sep \0] $str ] \0
3 }
4 set a {RookieSky}
5 set b {ok}
6 set c [OnePiece $a $b]
7 puts $c

  Next we look at the implementation of this file:

  

  We can see from the results, we have successfully used the string 'ok' to split 'RookieSky'

  NICE!!!

principle

  1.split string ?splitChars?

  The splitChars exploded argument characters String , returns a list. Each list element by a string in splitChars characters between characters, if string contains two consecutive characters splitChars characters or the same as the first or last character splitChars characters returns an empty list element, if splitChars is an empty string then string each character elements as a separate list, the default splitChars space character.

  2.list ?value value …?

  This command generates a list, that is, all elements of the value, TCL in the list is an ordered set of elements of a set, defined list can be nested, each element of the list can be any string may be a list.

  3.string map ?-nocase? charMap str

  The new string charmap return input, the output list str characters generated after mapping, charmap values ​​in the list can be seen that we {key value key value ...} of the map, has been mapped through str the string will not be repeated mapping.

  Thus we can understand our method is used to write a string and split \ 0 after forming a mapping alternatives, use \ 0 to split.

 

 

 

  

 

Guess you like

Origin www.cnblogs.com/RookieSky/p/10991394.html
TCL