TCL基礎(chǔ)教程——(4)字符串處理_第1頁
TCL基礎(chǔ)教程——(4)字符串處理_第2頁
免費預(yù)覽已結(jié)束,剩余2頁可下載查看

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)

文檔簡介

1、TCL 基礎(chǔ)教程 一一(4)字符串處理對于任何一種腳本語言來說, 強大的字符串處理功能都是為人們所津津樂道的, TCL 也不例 外,那么究竟 TCL 的字符串處理有什么功能呢?下面將介紹簡單模式匹配, 在日后的文章中, 還 將介紹正則表達式。Stri ng 命令Stri ng 命令實際上是一組操作字符串的命令,它的第一個變元決定了進行什么樣子的操作,所有 String 的命令如下:命令說明stri ng bytele ngth str返回用于存儲字符串的字節(jié)數(shù),由于 UTF8 編碼的原因,這個長度可能與 str ing len gth返回長度不一樣stri ng compare ?-no ca

2、se? ?-le ngth len?Stri str2根據(jù)詞典順序比較兩個字符串,nocase 表示忽略大小與,length 表示比較前 n 個字符,如果相冋 返回值為 0,如果 stri 靠前就返回-1,對于其他 情況返回 1stri ng equal ?- no case? Stri str2比較字符串,如果相同返回1,否則-1,使用nocase 來表示忽略大小與string first stri str2返回 str2 中 str1 第一次岀現(xiàn)的位置,如果沒有 的話,就返回-1。string is class ?-strict? ?-failindex varn ame? stri ng

3、如果 string 屬于某個 class 就返回,如果指定了 strict,那么就不匹配空字符串,否則總是要匹配,如果指定了failindex,就會將在 string中阻止其稱為 class 一員的字符串索引賦給var name,str ing last stri str2返回 str2 中 str1 最后一次岀現(xiàn)的位置,如果沒 有岀現(xiàn)就返回-1str ing len gth str返回 string中的字符個數(shù)string map ?-nocase? charMap string返回一個根據(jù) charmap 中輸入輸岀列表將 string 中的字符進行映射后產(chǎn)生的字符串。str ing ma

4、tch pattern str如果 str 匹配 pattern 就返回 1,否則返回 0,str ing ran ger str i j返回字符串中從 i 到 j 的部分。str ing repeat str cou nt返回將 str 重復(fù) count 次的字符串string replace str first last ?newstr?返回一個通過把從first 到 last 字符串替換為newstr 的新字符串,或是返回空string tolower string ?first? ?last?返回 string的小寫形式,first 和 last 決定了字符串位置stri ng tot

5、itle stri ng ?first? ?last?將第一個字符替換為大與,其他為小與,first和 last 決定了字符串位置string toupper string ?first? ?last?返回 string的大寫格式,first 和 last 決定了字符串位置string trim string ?chars?從 string 兩端除去 chars 中指定的字符,chars默認(rèn)空string trimleft string ?chars?從 string 的左端除去 chars 中指定的字符,chars默認(rèn)為空string trimright string ?chars?從 st

6、ring 的右端除去 chars 指定的字符, chars默認(rèn)為空string wordend str ix返回 str 中在索引 ix 位置包含的字符的單詞之后的字符的索引位置string wordstart str ix返回str中在索引ix位置包含字符串的單詞中第 一個字符的索引位置。對于我來說,常用的有如下幾個方法,length , equal , match , range , first 。請看下面的程序ppcor n localhost ppcor n$ cat strtest.tcl#!/usr/bi n/tclshset str1 str1set str2 str1set s

7、tr3 isstr1?set str4 the index of strl# print the length of str1,the value should be 4puts string length $str1# print the str1 equal str2, the value should be 1puts stri ng equal $str1 $str2# print the str2 match str3, the value should be 1puts stri ng match *$str1* $str3# print the 4 to 9 of str4, the value should be in dexputs stri ng range $str4 4 9# print the postion of first str1 in str4, the value should be 13puts stri ng first $str1 $str4ppcor n localhost ppcor n$ ./strtest.tcl411in dex13請注意一下 string match 的用法,中間使用了 *$str1*的用法,這里使用了模糊匹配。共有三種進行匹配的方式*匹配任意數(shù)量字符?確切的匹配一個字符chars匹配 char

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論