본문 바로가기

Linux/tcl

[Tcl] tcl 문자열 조작

  • String 명령어

문자열에 관해서 할 수 있는 많은 연산들을 한데 모아 놓은 명령어

 

string compare str1 str2 : str1과 str2 를 사전 순으로 비교하여 같으면 0 , str1이 str2 보다 앞서면 -1, 뒤지면 1을 리턴

string first str1 str2 : str1이 str2 에서 첫번쨰로 나타나는 index를 리턴

string index str i : str의 i번째 문자를 리턴

string last str1 str2 : str1이 str2에서 마지막으로 나타나는 index를 리턴

string length str : str의 길이를 리턴

string match pattern str : glob style 패턴이 str에 매칭되면 1을 아니면 0을 리턴

string range str i j : str의 i번째에서 j번째까지의 문자들을 리턴

string tolower str : str내의 대문자를 모두 소문자로 바꿈

string toupper str : str내의 소문자를 모두 대문자로 바꿈

string trim str ?chars? : str의 시작 부분과 끝 부분에 존재하는 chars를 제거. chars는 따로 지정하지 않으면 공백문자

string trimleft str ?chars? : str의 시작 부분에 존재하는 chars를 제거

string tirmright str ?chars? : str의 끝 부분에 존재하는 chars를 제거

string wordend str ix : str 내에서 ix번째 글자가 포함 된 단어가 끝나는 지점의 index를 리턴

                              단어 사이는 공백 문자로 구분

string wordstart str ix : str 내에서 ix번째 글자가 포함 된 단어가 시작하는 지점의 index를 리턴

 

% set str ":::::: won nara is genius :::: nara? really? ::::"
:::::: won nara is genius :::: nara? really? ::::
% string first "nara" $str
11
% string last "nara" $str
31
% string index $str 1
:
% string length $str
49
% string match ":*" $str
1
% string trim $str :
 won nara is genius :::: nara? really?
% string trimleft $str :
 won nara is genius :::: nara? really? ::::
% string trimright $str :
:::::: won nara is genius :::: nara? really?
% string toupper $str
:::::: WON NARA IS GENIUS :::: NARA? REALLY? ::::
% string range $str 11 14
nara
% string wordend $str 12
15
% string wordstart $str 12
11
%

 

 

- 문자열 매칭

string match 명령어에서 사용되는 패턴은 glob-style 패턴임 이는 UNIX shell에서 사용하는 wild card문자들을

사용하여 기술한 패턴임

 

- Wild card 

* : 아무 글자에나 매칭되고 0개 이상의 글자에 매칭

? : 아무 글자나 한 글자에만 매칭

[] : []사이에 열거된 문자에만 매칭, 한 글자만 적용

ex) string match

set str "about"
if { [string match { } $str] }\
{
	puts "True"
} else \
{
    	puts "False"
}

4행의 { }에 들어갈 예시
{ a* } > *은 아무 글자나 매칭이 되고 여러개의 글자에 매칭이므로 : 참
{ a? } > ?은 한 글자에만 매칭되므로 : 거짓
{ a???? } > 참
{ [aA] } a > 한 글자인 a와 매칭되므로 : 참
{ [aA] } aA > 한 글자 이상 매칭이 되지 않으므로 : 거짓
{ [aA] [aA] } aA > 각 각 한 글자씩 매칭되므로 : 참

 

- 문자열 비교

string compare 명령어 이용

ex) compare
set str1 " " ; set str2 " "
if { [string compare $str1 $str2] == 0 } \
{
	puts "equal"
} elseif { [string compare $str1 $str2] == 1 } \
{
	puts "lag" // lag = str1이 str2보다 사전적 순위가 뒤쳐짐
} else \
{
	puts "lead" // 앞섬
}

- format

어떤 형식으로 변수를 문자열로 바꾸어줄 것인지 명시하는 명령어

C 에서 printf()와 유사

ex)
//C언어//
printf( "Sum is %d\n", 10 );

//tcl//
puts [ format "Sum is %d" 10 ]

 

- append 

첫번째 Argument로 주어진 문자열 변수에 저장된 문자열과 나머지 Argument들을 연결하여 하나의 문자열로 만듬

set fruit "app"
append fruit le
puts $fruit
result : apple

 

- split

필드 분리자를 명시해주면 필드 구분자로 분리된 필드를 추출해서 리턴

set str "a:p:p:l:e"
puts [split $str :]

- join

주어진 리스트의 원소들 사이에 주어진 필드 분리자를 넣어 하나의 문자열로 병합

set str "a p p l e"
puts [join $str :]
result : a:p:p:l:e

 

'Linux > tcl' 카테고리의 다른 글

[tcl]  (0) 2022.08.02
[Tcl] tcl 정규표현식  (0) 2022.06.10
[Tcl] tcl 리스트 / 배열  (0) 2022.05.25
[Tcl] tcl 제어구조  (0) 2022.05.17
[Tcl] tcl 기본문법  (0) 2022.05.17