简体   繁体   中英

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

Given the following values which I am reading from an XML file:

00B50578 00A41434 00B50578

And, given the following string:

string foo = "6F6F6F6F6F";

I'd like to somehow replace the characters which I am reading from the XML file with the characters in “foo”. However, I'd like to start replacing those characters after the “00” and only continue replacing those characters in such a way that it would be equal to the length of “foo”. So, if I had my way, the new values would be as follows:

00 6F6F6F 00 6F6F 34 00B50578

I've tried a few ways of solving this issue, but to no avail.

Let's say I read the XML values into an array called “arrXmlValues” I have written code that looks like this…

        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("");
        }

The caveat's here is the 8 digit blocks of hex values must remain as such. So, I can only replace 6 chars in each block at a time and whatever wasn't written to the fist block on the list must be written to the second block on the list but only if i have left to write based on the variable "foo"...

Certainly, open to new ways of accomplishing this and any ideas that you might offer. I am not a strong coder but my goal here is to solve this problem for a school project and also to learn.

I appreciate any help, guidance. Sample code to accomplish this goal would be great. Thank you!!!

This was a fun one. The key is to break down the input string into smaller subsets before processing it, as you need to keep the spaces in alignment.

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

That could be simply achieved using a for loop, and a variable ( index ) to track how many chars left in foo string.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM