0

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:

006F6F6F 006F6F34 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!!!

1
  • use a regex, capture the values that comes after 00 and replace it with the string instead. Commented Apr 15, 2016 at 22:58

2 Answers 2

2

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());
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you David for your help! Would you mind for my own learning experience explaining the iterators \ conditions noted in the two for loops you created?
Thank you David! It is appreciated.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.