从左或右移动一个数组 Into/Onto 另一个固定大小的数组

Shifting One Array Into/Onto Another Fixed Size Array From Left Or Right

我考虑这个问题有一段时间了,一直在寻找嵌套的 For 循环和一堆 If/Thens...

我正在尝试为单行 LCD 字符显示器创建文本 scrolling/slide-in 效果。我希望能够将输入起点和终点设置到数组中。例如,我有一个大小为 16 的基本字节数组,想开始移动一个数组 onto/into 它。

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


_______________ <-从空白数组开始[16]
_____________H_ <-在指定的起始位置开始移入,例如[14]
____________He_
___________Hel_
__________Hell_
_________Hello_
________Hello__
_______Hello___
______Hello____
_____Hello_____ <-结束移动到指定位置,例如[5]

相反,我希望能够像这样将其移出:
_____Hello_____ <-起始数组<-需要创建
____Hello______
___Hello_______
__Hello________
_Hello_________
_ello__________
_llo___________
_lo____________
_o_____________
_______________

有没有 efficient/native 方法来做到这一点?我正在使用 NetMF,因此该框架存在一些限制。

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

  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);

我们已经创建了字符串的 'left part' - 文本行以 0 开始并以 x 或左边框的位置结束(以防 x - leftEdge 小于零。现在是时候计算:1) 我们应该为 SubString 方法采用文本中的什么索引 ('hello world') 以及 2) 我们想要提取多少个字符。

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

2) 这取决于 x 相对于右边界的位置。如果我们的 x 位置 + 文本 string.length 没有超出我们将采用该字符串的边界。在其他情况下,我们应该计算限制内的字符数并提取它们。此外,如果 x - leftEdge < 0 并且我们从 count 的索引开始 - 我们想从字符串长度中减去 count 值,这样我们只得到字符串的剩余部分而不超过它。