Results 1 to 8 of 8

Thread: Replace doesn't work

  1. #1

    Thread Starter
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Replace doesn't work

    this...
    Code:
    DateTime.Now.ToShortDateString().Replace("\\", "-")
    ...doesn't get rid of the "\" in the date string such as "02\12\03"

    it should be giving me "02-12-03".

    Whereas, this DOES work...
    Code:
    DateTime.Now.ToShortTimeString().Replace(":", ".")
    which gives me "10.15" rather than "10:15".

    What the hell is going on?

    this is my full code...
    Code:
    string pathTemp = itsLocalLogPath + category + "_" + DateTime.Now.ToShortDateString().Replace("\\", "-") + "_" + DateTime.Now.ToShortTimeString().Replace(":", ".") + EXTN_LOG_FILE;
    
    //write the error message to a local file on the HDD
    MySW = new StreamWriter(pathTemp, false);
    Last edited by wossname; Dec 2nd, 2003 at 11:00 AM.
    I don't live here any more.

  2. #2
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    the replace function doesnt work that way ( ie: DateTime.Now.ToShortDateString().Replace("\\", "-") )

    you have to make a value = the item , eg:
    VB Code:
    1. [COLOR=blue]string[/COLOR] strDate = DateTime.Now.ToShortDateString().Replace("\\", "-")
    then the string strDate will hold the date with the replaced item.
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  3. #3

    Thread Starter
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Unhappy

    I was thinking that too, but weirdly the bit highlited in blue does work (see original code again). And I haven't a clue why.

    Why should one work and the other not? They are doing pretty much the same thing anyway (removing characters that would confuse the filesystem).

    The only difference I can see is the escape sequence in the red code. But surely that is handled by the precompiler.
    I don't live here any more.

  4. #4
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    Originally posted by wossname
    I was thinking that too, but weirdly the bit highlited in blue does work (see original code again). And I haven't a clue why.

    Why should one work and the other not? They are doing pretty much the same thing anyway (removing characters that would confuse the filesystem).

    The only difference I can see is the escape sequence in the red code. But surely that is handled by the precompiler.
    It works because its being concatenated and assigned to another variable. Thats how the strings work.

  5. #5
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Replace(@"\", "-")

    You can do above instead of escaping it. The @ symbol makes the following string literal. Don't know if that will help your problem, but it is not widely known you can do that so I thought I would offer that up.

  6. #6
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    good point hellswraith , same applies if you are opening files ( say in a StreamReader ) you specify the path like this... @"C:\yourpath here"
    rather than using double \\
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  7. #7
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    PHP Code:
    DateTime.Now.ToShortDateString().Replace("\\\", "-") 
    works perfectly fine - It doesn't replace anything because there are no backslashes \ in DateTime.Now.ToShortDateString(). DateTime.Now.ToShortDateString() returns 12/3/2003 so if ya wanna use Replace, use .Replace("/","-"). What's kinda cool is instead of using the replace dealio, the ToString() on DateTime.Now can take formatters or a datetime format string as arguments so you can do this:
    PHP Code:
    DateTime.Now.ToString("dd-mm-yyyy_hh.MM"
    to get the same effect as the two replace snippets. Here's a link to an explanation of the characters you can use:http://msdn.microsoft.com/library/de...matstrings.asp

  8. #8

    Thread Starter
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682
    PVB has solved the mystery.

    My program was crashing and filling the console with the error message. It was something along the lines of "Cannot find path c:\...whatever\02\12\03\...

    It didn't even enter my head that the forward slash also works as a directory delimiter. And I'd got confused with the backslashes used in the date.

    Anyway the long and short of it is that it now works fine.

    Many thanks to all.

    PS I did happen to know the @"\" trick! But that didn't do anything to solve it.

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