2011年12月15日 星期四

Crystal Report 9 以後適用的“拆解長字串成多行”函數

//
// 將長字串拆解成數行,中間用 CR+LF 分開
// Crystal Report 9 以後適用
//
StringVar s := {要拆解的欄位};                // 要拆解的字串
StringVar output := '';                     // 輸出字串
NumberVar CharsInALine := 20;               // 一行幾個字
NumberVar currLength := 0;
while Length(s) > 0 do
(
  // 這只是很粗略的判斷,假定使用者只會輸入半形英數字跟中文字,
  //
不會使用半形的特殊符號,如半形日文假名
  if AscW(s) > 256 then
  (   
    currLength := currLength + 2;           // 中文字,長度 + 2
  )
  else
  (
    currLength := currLength + 1;           // 文數字,長度 + 1
  );
  output := output + s[1];
  if currLength >= CharsInALine then
  (
    output := output + Chr(13) + Chr(10);
    currLength := 0;
  );
  s := Mid(s, 2);
);
output;