【问题标题】:Postscript: concatenate two strings?后记:连接两个字符串?
【发布时间】:2021-12-28 22:54:28
【问题描述】:

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

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

【问题讨论】:

  • 有关基本字符串操作的列表,请参阅以下文档。包括字符串连接。stringconv

标签: postscript


【解决方案1】:

PostScript 没有内置的字符串连接运算符。您需要为此编写一些代码。比如

 /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  

(来自https://en.wikibooks.org/wiki/PostScript_FAQ/Programming_PostScript#How_to_concatenate_strings%3F.的代码)

【讨论】:

  • 你打赌...感谢指点,我的程序现在运行良好!
  • Link rot 已将上述内容取出。见:How to concatenate strings?
  • @scruss,已修复,谢谢。
【解决方案2】:

同样的想法可以推广到任意数量的字符串。较早的版本使用了一个辅助函数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)

【讨论】:

  • 此代码使用 bottom-to-top 堆栈技术,详细描述here
【解决方案3】:

里面有一些有用的子程序

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

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

【讨论】:

    猜你喜欢
    • 2013-11-05
    • 1970-01-01
    • 2011-08-29
    • 2020-08-19
    • 1970-01-01
    • 2016-03-01
    • 2012-10-05
    相关资源
    最近更新 更多