C#:给定一个指定长度的字符串,根据位置和长度替换数组中的字符

C# : Given a string of a specified length replace chars in an array based on position and length

鉴于我从 XML 文件中读取的以下值:

00B50578 00A41434 00B50578

并且,给定以下字符串:

string foo = "6F6F6F6F6F";

我想以某种方式用“foo”中的字符替换我从 XML 文件中读取的字符。但是,我想开始替换“00”之后的那些字符,并且只继续替换这些字符,使其等于“foo”的长度。所以,如果我按照自己的方式行事,新值将如下所示:

006F6F6F 006F6F34 00B50578

我尝试了几种方法来解决这个问题,但都无济于事。

假设我将 XML 值读入名为“arrXmlValues”的数组中,我编写的代码如下所示……

        string foo = "6F6F6F6F6F";
        string[] arrXmlValues = new String[] { xmlReader.GetAttribute("w:val") };

        foreach (string r in arrXmlValues)
        {
            Console.WriteLine("Original value was : {0}", r);

            StringBuilder sb = new StringBuilder(r);
            sb.Remove(2, foo.Length);
            sb.Insert(2, foo);

            Console.WriteLine("New value is : {0}", sb);
            Console.WriteLine("");
        }

这里的警告是 8 位十六进制值块必须保持原样。所以,我一次只能替换每个块中的 6 个字符,任何没有写入列表中第一个块的字符都必须写入列表中的第二个块 但前提是我还没有写基于变量 "foo"...

当然,乐于接受完成此任务的新方法以及您可能提供的任何想法。我不是一个强大的编码员,但我的目标是为学校项目解决这个问题并学习。

感谢任何帮助和指导。实现此目标的示例代码会很棒。谢谢!!!

这很有趣。关键是在处理之前将输入字符串分解成更小的子集,因为您需要保持空格对齐。

static void Main(string[] args)
{
    string input = "00B50578 00A41434 00B50578";
    string foo = "6F6F6F6F6F";

    // start with a queue of characters filled with the
    // letters we are going to put into the input string.
    Queue <char> fooQueue = new Queue<char>(foo); 

    StringBuilder result = new StringBuilder();

    // iterate through each split, so that we maintain spaces.
    foreach (var item in input.Split(' '))
    {
        // go through each set of two characters in this specific chunk.
        for (int i = 0; i < item.Length - 1; i += 2) 
        {
            var substring = item.Substring(i, 2); // look at each chunk.
            if (substring == "00") // if the chunk is 00, then append 00. 
            {
                result.Append("00");
            }
            else // otherwise
            {
                // take either the next two characters out of the queue
                //and append them, or if the queue is empty, append the 
                //letters from the original text.
                for (int j = 0; j < 2; j++) 
                {
                    if (fooQueue.Count >= 1)
                        result.Append(fooQueue.Dequeue());
                    else
                        result.Append(substring[j]);
                }
            }
        }

        // add a space at the end of the chunk. 
        result.Append(' ');
    }

    // print all the chunks, but trim the end (which should at 
    // this point have an additional space at the end.
    Console.WriteLine(result.ToString().Trim());
}

这可以简单地使用 for 循环和一个变量 (index) 来跟踪 foo 字符串中剩余的字符数。

Working fiddle

string foo = "6F6F6F6F6F";
string[] arrXmlValues = new String[] { "00B50578", "00A41434", "00B50578" };

var index = 0;
foreach (string r in arrXmlValues)
{
    var arr = r.ToCharArray();  

    // magic is here
    for(var j = 2; j < arr.Length && index < foo.Length; j++) arr[j] = foo[index++];

    Console.WriteLine("Original value was : {0}", r);
    Console.WriteLine("New value is : " + new String(arr));
}