首页 文章

后记:连接两个字符串?

提问于
浏览
5

如何在Postscript中连接两个字符串?

(foo) (bar) ??? -> (foobar)

3 回答

  • 6

    PostScript没有内置的字符串连接运算符 . 你需要为此编写一些代码 . 例如,请参阅http://en.wikibooks.org/wiki/PostScript_FAQ#How_to_concatenate_strings.3F .

    (由mh更新:复制到这里)

    /concatstrings % (a) (b) -> (ab)  
       { exch dup length    
         2 index length add string    
         dup dup 4 2 roll copy length
         4 -1 roll putinterval
       } bind def
    
  • 3

    同样的想法推广到任意数量的字符串 . 早期版本使用辅助函数 acat ,它接受一个字符串数组(以便于计数和迭代) . 此版本使用更高级的循环和堆栈操作来避免分配数组 . 此版本还可以通过将 string 运算符更改为 array 来连接数组 .

    % (s1) (s2) (s3) ... (sN) n  ncat  (s1s2s3...sN)
    /ncat {        % s1 s2 s3 .. sN n                   % first sum the lengths
        dup 1 add  % s1 s2 s3 .. sN n n+1 
        copy       % s1 s2 s3 .. sN n  s1 s2 s3 .. sN n
        0 exch     % s1 s2 s3 .. sN n  s1 s2 s3 .. sN 0 n 
        {   
            exch length add 
        } repeat             % s1 s2 s3 .. sN  n   len  % then allocate string
        string exch          % s1 s2 s3 .. sN str   n   
        0 exch               % s1 s2 s3 .. sN str  off  n
        -1 1 {               % s1 s2 s3 .. sN str  off  n  % copy each string
            2 add -1 roll       % s2 s3 .. sN str  off s1  % bottom to top
            3 copy putinterval  % s2 s3 .. sN str' off s1
            length add          % s2 s3 .. sN str' off+len(s1)
                                % s2 s3 .. sN str' off'
        } for                               % str' off'
        pop  % str'
    } def 
    
    (abc) (def) (ghi) (jkl) 4 ncat == %(abcdefghijkl)
    
  • 4

    有很多有用的子程序

    http://www.jdawiseman.com/papers/placemat/placemat.ps

    包括 Concatenate (接受两个字符串)和 ConcatenateToMark (标记string0 string1 ...) .

相关问题