2012-09-07 95 views
5

में किसी स्ट्रिंग को सरणी में विभाजित करने के लिए किसी दिए गए वर्ण द्वारा स्ट्रिंग को सरणी में विभाजित करने का सबसे आसान तरीका क्या है? उदाहरण के लिए, अंतरिक्ष पर विभाजित करके शब्दों की एक सरणी बनाना; या स्ट्रिंग के सभी पात्रों की सरणी भी बनाते हैं।पोस्टस्क्रिप्ट

एकमात्र तरीका जिसे मैं सोच सकता हूं वह है search एक लूप में उपयोग करना। चूंकि, सभी भाषाओं में इस उद्देश्य के लिए एक कार्य है, मुझे डर है कि ऐसा करने के लिए मुझे PostScript में कोई फ़ंक्शन याद नहीं है।

+1

मैं यहाँ मेरा उत्तर में एक पाश में खोज का उपयोग करने का एक उदाहरण है:: यहाँ search ऑपरेटर सारांश PostScript Language Reference Manual में पाया जाता है http://stackoverflow.com/a/5846955/733077 –

उत्तर

6

आप 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 
5
%! 

%(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)]