5

特定の文字で文字列を配列に分割する最も簡単な方法は何ですか? たとえば、スペースを分割して単語の配列を作成します。または、文字列のすべての文字の配列を作成することさえできます。

私が考えることができる唯一の方法はsearch、ループで使用することです。すべての言語にはこの目的のための関数があるため、そうするための関数が欠けているのではないかと心配してPostScriptいます。

4

2 に答える 2

8
%!

%(string) (delimiter)  split  [(s)(t)(r)(i)(n)(g)]
/split {              % str del
    [ 3 1 roll        % [ str del
    {                 % [ ... str del
        search {      % [ ... post match pre
            3 1 roll   % [ ... pre post match  %ie. [ ... pre str' del
        }{            % [ ... str
            exit       % [ ... str  %% break-from-loop
        }ifelse
    }loop             % [ ...
    ]                 % [ ... ]
} def

(string of words separated by spaces)( )split ==
%-> [(string) (of) (words) (separated) (by) (spaces)]

(string.of.words.separated.by.dots)(.)split ==
%-> [(string) (of) (words) (separated) (by) (dots)]
于 2012-12-23T16:22:35.053 に答える
6

あなたはsearchオペレーターと正しい軌道に乗っています。その目的は、テキスト文字列の検索と一致を実行することです。以下は、 PostScript 言語リファレンス マニュアルsearchにある演算子の概要です。

search   string seek search post match pre true (if found)  
         string false (if not found)  

         looks for the first occurrence of the string seek within string and  
         returns results of this search on the operand stack. The topmost   
         result is a boolean that indicates if the search succeeded.  

         If search finds a subsequence of string whose elements are equal   
         to the elements of seek, it splits string into three segments:   
         pre, the portion of string preceding the match; match, the portion  
         of string that matches seek; and post, the remainder of string. It  
         then pushes the string objects post, match, and pre on the operand   
         stack, followed by the boolean true. All three of these strings are  
         substrings sharing intervals of the value of the original string.  

         If search does not find a match, it pushes the original string  
         and the boolean false.  

         Example:  

             (abbc) (ab) search ==> (bc) (ab) ( ) true  
             (abbc) (bb) search ==> (c) (bb) (a) true  
             (abbc) (bc) search ==> () (bc) (ab) true  
             (abbc) (B) search ==> (abbc) false  
于 2012-09-08T03:11:28.457 に答える