[첫화면으로]Perl/문자열

마지막으로 [b]

Perl의 문자열 처리 관련

1. index

스트링 내에서 최초로 나타나는 서브스트링의 위치1
    my $stuff  = "Howdy world!";
    my $where1 = index($stuff, "w");               # $where1 gets 2
    my $where2 = index($stuff, "w", $where1 + 1);  # $where2 gets 6 - 세번째 인자는 index의 리턴값의 최소값을 명시
    my $where3 = index($stuff, "w", $where2 + 1);  # $where3 gets -1 (not found)

2. rindex

스트링 내에서 최후로 나타나는 서브스트링의 위치2
    my $fred = "Yabba dabba doo!";
    my $where1 = rindex($fred, "abba");  # $where1 gets 7
    my $where2 = rindex($fred, "abba", $where1 - 1);  # $where2 gets 1 - 세번째 인자는 rindex의 리턴값의 최대값을 명시
    my $where3 = rindex($fred, "abba", $where2 - 1);  # $where3 gets -1

3. substr

특정 위치의 서브스트링 추출3
    $part = substr($string, $initial_position, $length);

    my $mineral = substr("Fred J. Flintstone", 8, 5);  # gets "Flint"
    my $rock = substr "Fred J. Flintstone", 13, 1000;  # gets "stone"
    my $pebble = substr "Fred J. Flintstone", 13;  # gets "stone"        # 길이 생략

스트링이 lvalue로 주어질 경우는 치환도 가능4
    my $string = "Hello, world!";
    substr($string, 0, 5) = "Goodbye";  # $string is now "Goodbye, world!"
# 정규 표현식 바인드 연산도 가능
    substr($string, -20) =~ s/fred/barney/g;

# 이런 형태로도 사용가능
    my $previous_value = substr($string, 0, 5, "Goodbye");  # 리턴값은 치환되기 전의 해당 위치의 서브스트링

4. sprintf

sprintf FORMAT, LIST5

5. 기타

이름:  
Homepage:
내용:
 


컴퓨터분류
각주:
1. 2. 3. 4. Learning Perl, Chapter 13
5. perldoc -f sprintf

마지막 편집일: 2013-5-23 12:52 am (변경사항 [d])
4514 hits | Permalink | 변경내역 보기 [h] | 페이지 소스 보기