【问题标题】:C# : Given a string of a specified length replace chars in an array based on position and lengthC#:给定一个指定长度的字符串,根据位置和长度替换数组中的字符
【发布时间】:2016-04-15 22:55:44
【问题描述】:

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

00B50578 00A41434 00B50578

并且,给定以下字符串:

string foo = "6F6F6F6F6F";

我想以某种方式将我从 XML 文件中读取的字符替换为“foo”中的字符。但是,我想开始替换“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”...

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

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

【问题讨论】:

  • 使用正则表达式,捕获 00 之后的值并将其替换为字符串。

标签: c# arrays string hex stringbuilder


【解决方案1】:

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

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 循环中提到的迭代器\条件吗?
  • 谢谢大卫!不胜感激。
【解决方案2】:

这可以简单地使用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));
}

【讨论】:

    猜你喜欢
    • 2022-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-21
    • 2021-03-03
    • 1970-01-01
    • 2022-01-04
    • 1970-01-01
    相关资源
    最近更新 更多