从左或右将一个阵列移入/移入另一个固定大小的阵列

时间:2015-06-16 12:58:36

标签: c# arrays lcd .net-micro-framework

我已经考虑了一段时间并且继续回到嵌套的For Loops和一堆If / Thens ......

我正在尝试在单行上为LCD字符显示创建文本滚动/滑入效果。我希望能够将输入起点和终点设置到数组中。例如,我的基本字节数组大小为16,并且想要开始将数组移入/移入它。

输出将是这样的,每行是数组的迭代,发送到显示器:


_______________< -start with blank array [16]
_____________H_< -start在指定的起始位置移入,例如[14]
____________He_
___________Hel_
__________Hell_
_________Hello_
________Hello__
_______Hello___
______Hello____
_____Hello_____< -end按指定位置移动,例如[5]

相反,我希望能够像这样改变它:
_____Hello_____< -Begining Array< -This需要创建
____Hello______
___Hello_______
__Hello________
_Hello_________
_ello__________
_llo___________
_lo____________
_o_____________
_______________

有没有一种有效/本地的方式来做到这一点?我使用的是NetMF,因此框架存在一些限制。

脚注:我想这可以通过直接操作要显示的字符串然后将其转换为字节数组发送到显示器来完成,但我认为这可能会更慢。

1 个答案:

答案 0 :(得分:1)

  class Program2
    {
        static void AnimateString(int leftEdge, int RightEdge, int length, char fillWith, string text, int delay_ms)
        {
            int x = RightEdge;
            string line = "";
            int count = 0;
            string leftBorder = "";            

        while (true)
        {
            Console.CursorLeft = 0;
            Console.Write(new String(fillWith, length));
            Console.CursorLeft = 0;

            if (x < leftEdge) ++count;                

            leftBorder = new String(fillWith, x - 1 > leftEdge ? x - 1 : leftEdge);                

            line = leftBorder + text.Substring(
                    x > leftEdge - 1? 0 : count, 
                    x > leftEdge - 1 ? (x + text.Length > RightEdge ? RightEdge - x : text.Length) : text.Length - count);
            Console.Write(line);
            Thread.Sleep(delay_ms);
            --x;
            if (count >= text.Length) { x = RightEdge; count = 0; line = ""; }
        }       
    }

    static void Main()
    {
        string blank = new String('-', 32);
        string text = "Hello world!";
        AnimateString(4, 20, 24, '-', "Hello world", 100);

        Console.ReadKey();
    }
}

检查以下代码行:

line = leftBorder + text.Substring(
        x > leftEdge - 1? 0 : count, 
        x > leftEdge - 1 ? (x + text.Length > RightEdge ? RightEdge - x : text.Length) : text.Length - count);

我们已经创建了左侧部分&#39;我们的字符串 - 文本行以0开头,以x或left border的位置结束(如果x-leftEdge小于零。现在是时候计算:1)文本中的索引(& #39;你好世界&#39;)我们应该采用SubString方法和2)我们想要提取多少个字符。

1)取决于:我们的x位置是否到达左边界?    不)我们从索引0开始    是的)我们从计数值开始。计数 - 是我们用来计算的字段 我们的文本字符串中的左移

2)这取决于x对右边界的位置。如果我们的x位置+文本string.length没有超出边界,我们就取这个字符串。在其他情况下,我们应该计算限制内有多少个字符并提取它们。如果x - leftEdge&lt;我们从计数索引开始 - 我们希望从字符串长度中减去计数值,这样我们只得到字符串的剩余部分而不超过它。

enter image description here

相关问题