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

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.


Cheers
scm0smlAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
AlexFMConnect With a Mentor Commented:
string s = DateTime.Now("...");   // set here required format
string s1 = s.Replace(":", "-");
0
 
SmithJWConnect With a Mentor Commented:
string.replace(':','-');

Where string is string taken from the datetime object.
This will replace all instances of ':' with '-'
0
 
scm0smlAuthor Commented:
cheers both!
0
 
daniel_ballaCommented:
Hi scm0sml,
actually, you could do string s = DateTime.Now.ToString("dd MMMM yyyy hh-mm-ss");

Cheers!
0
All Courses

From novice to tech pro — start learning today.