【问题标题】:Wrap string without cutting words for thermal printer为热敏打印机绕线不切字
【发布时间】:2020-08-13 07:14:59
【问题描述】:

我正在尝试通过热敏打印机打印字符串,但问题是纸张只接受 32 一行中的字符将剩余文本换成另一行,最后一个字符总是分成两部分,字符串很难理解。

例子:

string PrintStr = "01-(200),02-(200),03-(200),04-(200),05-(200)";

当前输出:

01-(200),02-(200),03-(200),04-(20            # <- 200 is broken into 20 and 0
0),05-(200)

更好的输出:

01-(200),02-(200),03-(200),                  # Split on comma, numbers are preserved 
04-(200),05-(200)

我还使用 Linq 方法在 32th 字符后换行,但 最后一个字符 正在切割。我只想在最后一个逗号“,”之后的前 32 个字符中添加一个新行,这样我的字符串将在可读文本中中断。我正在分享我的代码。提前感谢您的帮助..

输入:

var PrintStr = "01-(200),02-(200),03-(200),04-(200),05-(200),06-(200),07-(200),08-(200),09- 
  (200),10-(200),11-(200),12-(200),13-(200),14-(200),15- 
 (200),16-(200),17-(200),18-(200),19-(200),20-(200),21-(200),22-(200),23-(200),24-(200),25-(200),26- 
 (200),27-(200),28-(200),29-(200),30-(200),31-( 
 200),32-(200),33-(200),34-(200),35-(200),36-(200),37-(200),38-(200),39-(200),40-(200),41-(200),42- 
 (200),43-(200),44-(200),45-(200),46-(200),47-(200),48- 
 (200),49-(200),50-(200),51-(200),52-(200),53-(200),54-(200),55-(200),56-(200),57-(200),58-(200),59- 
 (200),60-(200),61-(200),62-(200),63-(200),64- 
 (200),65-(200),66-(200),67-(200),A1111-(200)"

代码(我的尝试):

var AdjustPrintStr = string.Join(Environment.NewLine, PrintStr
  .ToLookup(c => k++ / 32) 
  .Select(e => new String(e.ToArray())));

输出(当前,不需要):

01-(200),02-(200
),03-(200),04-(200),05-(200),06-
(200),07-(200),08-(200),09-(200)
,10-(200),11-(200),12-(200),13-(
200),14-(200),15-(200),16-(200),
17-(200),18-(200),19-(200),20-(2
00),21-(200),22-(200),23-(200),2
4-(200),25-(200),26-(200),27-(20
0),28-(200),29-(200),30-(200),31
-(200),32-(200),33-(200),34-(200
),35-(200),36-(200),37-(200),38-
(200),39-(200),40-(200),41-(200)
,42-(200),43-(200),44-(200),45-(
200),46-(200),47-(200),48-(200),
49-(200),50-(200),51-(200),52-(2
00),53-(200),54-(200),55-(200),5
6-(200),57-(200),58-(200),59-(20
0),60-(200),61-(200),62-(200),63
-(200),64-(200),65-(200),66-(200

注意,最后一个"),A1111-(200)" 片段丢失了

【问题讨论】:

    标签: c# xamarin.forms split


    【解决方案1】:

    好吧,你必须实现这样的例程 (将text 拆分为at 字符,确保每行最多maxWidth 个字符)手动; 没那么难:

    public static IEnumerable<string> MySplit(string text, 
        int maxWidth, params char[] at) {
      if (null == text)
        throw new ArgumentNullException(nameof(text));
      else if (maxWidth <= 0)
        throw new ArgumentOutOfRangeException(nameof(maxWidth));
      else if (null == at)
        throw new ArgumentNullException(nameof(at));
    
      int startIndex = 0;
      int bestIndex = -1;
    
      for (int i = 0; i < text.Length; ++ i) {
        if ((i - startIndex) > maxWidth) {
          if (bestIndex < 0)
            bestIndex = i - 1;
    
          yield return text.Substring(startIndex, bestIndex - startIndex + 1);
    
          startIndex = bestIndex += 1;
          bestIndex = -1;
        }
        
        if (at.Contains(text[i]))
          bestIndex = i;
      }
    
      yield return text.Substring(startIndex);
    }
    

    现在,让我们打印出source 字符串:

    string PrintStr = @"01-(200),02-(200),03-(200),04-(200),05-(200),06-(200),07-(200),08-(200),09- 
      (200),10-(200),11-(200),12-(200),13-(200),14-(200),15- 
     (200),16-(200),17-(200),18-(200),19-(200),20-(200),21-(200),22-(200),23-(200),24-(200),25-(200),26- 
     (200),27-(200),28-(200),29-(200),30-(200),31-( 
     200),32-(200),33-(200),34-(200),35-(200),36-(200),37-(200),38-(200),39-(200),40-(200),41-(200),42- 
     (200),43-(200),44-(200),45-(200),46-(200),47-(200),48- 
     (200),49-(200),50-(200),51-(200),52-(200),53-(200),54-(200),55-(200),56-(200),57-(200),58-(200),59- 
     (200),60-(200),61-(200),62-(200),63-(200),64- 
     (200),65-(200),66-(200),67-(200),A1111-(200)";
    

    看来,您应该预处理它以删除所有新行、制表符、空格等,并且 然后才拆分:

    // Let's remove all white spaces (new lines, tabulations, spaces)
    PrintStr = Regex.Replace(PrintStr, @"\s+", "");
    
    // split on comma ',' while ensuring each lines <= 32 characters 
    var result = string.Join(Environment.NewLine, MySplit(PrintStr, 32, ','));
    
    Console.Write(result);
    

    结果:

    01-(200),02-(200),03-(200),
    04-(200),05-(200),06-(200),
    07-(200),08-(200),09-(200),
    10-(200),11-(200),12-(200),
    13-(200),14-(200),15-(200),
    16-(200),17-(200),18-(200),
    19-(200),20-(200),21-(200),
    22-(200),23-(200),24-(200),
    25-(200),26-(200),27-(200),
    28-(200),29-(200),30-(200),
    31-(200),32-(200),33-(200),
    34-(200),35-(200),36-(200),
    37-(200),38-(200),39-(200),
    40-(200),41-(200),42-(200),
    43-(200),44-(200),45-(200),
    46-(200),47-(200),48-(200),
    49-(200),50-(200),51-(200),
    52-(200),53-(200),54-(200),
    55-(200),56-(200),57-(200),
    58-(200),59-(200),60-(200),
    61-(200),62-(200),63-(200),
    64-(200),65-(200),66-(200),
    67-(200),A1111-(200)
    

    希望,这正是您打印后想要看到的图片

    【讨论】:

    • 非常感谢先生,这段代码工作得很好,但这是通过使用循环,所以这将花费太多时间来处理,因为我正在使用这种方法来移动应用程序?有没有使用 Linq 的选项?
    • @Prashant Sharma:Linq 也使用循环,所以它不会更快PrintStr 是一个 tiny 字符串,将在微秒内处理完毕,它是 termal printer 将成为瓶颈。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-05
    • 2012-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-03
    相关资源
    最近更新 更多