tags:

views:

93

answers:

3

Hello,

I need a solution for a problem I am working on. I have a string which would be delivered to my application in the format below: ece4241692a1c7434da51fc1399ea2fa155d4fc983084ea59d1455afc79fafed

What I need to do is format it for my database so it reads as follows: <ece42416 92a1c743 4da51fc1 399ea2fa 155d4fc9 83084ea5 9d1455af c79fafed>

I assume the easiest way to do this would be using regular expressions, but I have never used them before, and this is the first time I have ever needed to, and to be honest, I simply don't have the time to read up on them at the moment, so if anyone could help me with this I would be eternally grateful.

Kind Regards

+2  A: 

Try

Regex.Replace(YOURTEXT, "(.{8})", "$1 ");
S.Mark
+2  A: 

What about:

string input ="ece4241692a1c7434da51fc1399ea2fa155d4fc983084ea59d1455afc79fafed";
string target = "<" + Regex.Replace(input, "(.{8})", "$1 ").Trim() + ">";

Or

string another = "<" + String.Join(" ", Regex.Split(input, "(.{8})")) + ">";
Rubens Farias
probably... if that whas the question <g>
Lieven
He wants to go the other direction.
Joel Etherton
sorry guys, fixed
Rubens Farias
+2  A: 

You might just be better served having a small static string parsing method to handle it. A regular expression might get it done, but unless you're doing a bunch in a batch you won't save enough in system resources for it to be worth the maintenance of a RegEx (if you're not already familiar with them I mean). Something like:

    private string parseIt(string str) 
    {
        if(str.Length % 8 != 0) throw new Exception("Bad string length");
        StringBuilder retVal = new StringBuilder(str)
        for (int i = str.Length - 1; i >=0; i=i-8)
        {
            retVal.Insert(i, " ");    
        }
        return "<" + retVal.ToString() + ">";
    }
Joel Etherton
+1 for not using RE :-)
logout