Swapping all instances of one character within a string with another character

Posted on 2006-04-27
Last Modified: 2010-04-16
Simple question but I'm struggling to find the answer anywhere.

I'm creating a new directory and want the name of the directory to be the date then time it was created.....taken from a DateTime object. e.g 27 April 2006 12:30:56

However the : in the time is an invalid character for a directory name so what I am doing at the mo is readin the time into a string and I want to swap the : for a valid character, say -.

How do I go about swapping these characters?

I'm open to suggesstions if any1 has a better method.

Question by:scm0sml
    LVL 4

    Assisted Solution


    Where string is string taken from the datetime object.
    This will replace all instances of ':' with '-'
    LVL 48

    Accepted Solution

    string s = DateTime.Now("...");   // set here required format
    string s1 = s.Replace(":", "-");

    Author Comment

    cheers both!
    LVL 9

    Expert Comment

    Hi scm0sml,
    actually, you could do string s = DateTime.Now.ToString("dd MMMM yyyy hh-mm-ss");


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Suggested Solutions

    Title # Comments Views Activity
    C# Get Networked Computers (SQL Servers) 2 30
    Email Body 4 30
    Export Dataset to Excel xlsx 8 56
    import issue in bit values 3 31
    Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now