CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 2009
    Posts
    3

    [RESOLVED] Advanced string replaceing

    Hello CodeGuru community.
    Im working on a, well i guess you could call it a kind of macro system.
    It works this way:
    User a wants to send a message to user b, user a writes "Hello $User how are you my name is $Me".
    The message is saved and sent to user b but as its saved its converted to "Hello b how are you my name is a."

    This I got working the part where im stuck is at $DateSS5. Which I want to be converted into DateTime.Now.AddSecounds(5); //since 5 was the number. All of this has to be dynamical. My current code is:
    public string Encode(string Author, string Recipent, string input)
    {
    string Identifier = "$";
    string PreDefBye = "With kind regards $Me";
    string PreDefUser = Recipent;
    string PreDefMe = Author;
    string TimeFormat = "HH:mm:ss dd-MM-yyyy";
    DateTime PreDefNow = DateTime.Now;
    input = input.Replace(Identifier + "Bye", PreDefBye).Replace(Identifier + "User", PreDefUser).Replace(Identifier + "Me", PreDefMe);
    input = input.Replace(Identifier + "Now", PreDefNow.ToString(TimeFormat));
    int StartPos = 0;
    char[] Dates = { Convert.ToChar(Identifier + "DateSS"), Convert.ToChar(Identifier + "DateMI"), Convert.ToChar(Identifier + "DateHH"), Convert.ToChar(Identifier + "DateDD"), Convert.ToChar(Identifier + "DateMO"), Convert.ToChar(Identifier + "DateYY") };
    do
    {
    StartPos = input.IndexOfAny(Dates, StartPos);
    if (StartPos > -1)
    {
    string DateIden = input[StartPos + 5].ToString().Remove(6);
    int result;
    int.TryParse((DateIden.Remove(0, 2).Remove(DateIden.IndexOf("."))),out result);
    if (result > 0)
    {
    DateTime Dt = DateTime.Now;
    string DateIdentifier = DateIden.Remove(2);
    if (DateIdentifier.ToLower() == "ss")
    {
    Dt.AddSeconds(result);
    }
    if (DateIdentifier.ToLower() == "mi")
    {
    Dt.AddMinutes(result);
    }
    if (DateIdentifier.ToLower() == "hh")
    {
    Dt.AddHours(result);
    }
    if (DateIdentifier.ToLower() == "dd")
    {
    Dt.AddDays(result);
    }
    if (DateIdentifier.ToLower() == "mo")
    {
    Dt.AddMonths(result);
    }
    if (DateIdentifier.ToLower() == "yy")
    {
    Dt.AddYears(result);
    }
    input.Insert(StartPos, Dt.ToString(TimeFormat));
    input.Remove(StartPos, (input.IndexOf(".", StartPos, 10)));
    }

    }
    } while (StartPos > -1);
    return input;
    }

    This code does not work since im useing a Char[] which is broken since its has more than 1 identifier, i just cant seem to find another way to tackle this problem. Any surgestions for others ways are warmly welcome!

    Thank you for reading $Me

  2. #2
    Join Date
    Jul 2009
    Posts
    3

    Re: Advanced string replaceing

    a Quick note: this is framework 3.5 and its for an asp.net site

  3. #3
    Join Date
    Jul 2009
    Posts
    3

    Re: Advanced string replaceing

    I've managed to rewrite it myself. If anyone would be in need of it, the code is below.


    public string Encode(string Author, string Recipent, string input)
    {
    string Identifier = "$";
    string PreDefBye = "With kind regards $Me";
    string PreDefUser = Recipent;
    string PreDefMe = Author;
    string TimeFormat = "HH:mm:ss dd-MM-yyyy";
    DateTime PreDefNow = DateTime.Now;
    input = input.Replace(Identifier + "Bye", PreDefBye).Replace(Identifier + "User", PreDefUser).Replace(Identifier + "Me", PreDefMe);
    input = input.Replace(Identifier + "Now", PreDefNow.ToString(TimeFormat));

    string RegDates = @"DateSS|DateMI|DateHH|DateDD|DateMO|DateYY";
    Regex r = new Regex(RegDates, RegexOptions.IgnoreCase);

    Match m = r.Match(Identifier + input);
    while (m.Success)
    {
    DateTime dt = DateTime.Now;
    string Value = input.Remove(0, input.IndexOf(m.Groups[0].Value) + m.Groups[0].Length);
    Value = Value.Remove(Value.IndexOf("."));
    int value = Convert.ToInt32(Value);

    string Before = input.Substring(0, input.IndexOf(m.Groups[0].Value) - 1);
    string After = input.Substring((Before.Length + 7 + Value.Length + 1));
    if (m.Groups[0].Value.ToLower() == "datess")
    {
    dt = dt.AddSeconds(value);
    }
    if (m.Groups[0].Value.ToLower() == "datemi")
    {
    dt = dt.AddMinutes(value);
    }
    if (m.Groups[0].Value.ToLower() == "datehh")
    {
    dt = dt.AddHours(value);
    }
    if (m.Groups[0].Value.ToLower() == "datedd")
    {
    dt = dt.AddDays(value);
    }
    if (m.Groups[0].Value.ToLower() == "datemo")
    {
    dt = dt.AddMonths(value);
    }
    if (m.Groups[0].Value.ToLower() == "dateyy")
    {
    dt = dt.AddYears(value);
    }
    input = Before + dt.ToString(TimeFormat) + After;

    m = m.NextMatch();
    }
    return input;
    }

    With kind Regards Kolklik

  4. #4
    Join Date
    Jun 2008
    Posts
    2,477

    Re: [RESOLVED] Advanced string replaceing

    You should really look into regular expressions. This could grow into a big maintenance headache.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured