Why does String.Format("/")
get converted to "-"?
To replace all forward slashes in a string: Call the replace() method, passing it a regular expression that matches all forward slashes as the first parameter and the replacement string as the second. The replace method will return a new string with all forward slashes replaced.
If you want to include a backslash character itself, you need two backslashes or use the @ verbatim string: var s = "\\Tasks"; // or var s = @"\Tasks"; Read the MSDN documentation/C# Specification which discusses the characters that are escaped using the backslash character and the use of the verbatim string literal.
We should double for a backslash escape a forward slash / in a regular expression. A backslash \ is used to denote character classes, e.g. \d . So it's a special character in regular expression (just like in regular strings).
I suspect that you are using the /
symbol inside a {0}
placeholder. It is a reserved symbol used as datetime separator in the given culture. You could escape it, like this:
string date = string.Format("{0:dd\\/MM\\/yyyy}", DateTime.Now);
As per Custom Date and Time Format Strings, /
refers to the culture's date separator. So you need to escape it. You can either use a backslash as per Darin's answer, or you can quote it in single quotes. For example:
using System;
using System.Globalization;
class Test
{
static void Main()
{
DateTime date = DateTime.Now;
CultureInfo da = new CultureInfo("da");
// Prints 09-08-2011
Console.WriteLine(string.Format(da, "{0:dd/MM/yyyy}", date));
// Prints 09/08/2011
Console.WriteLine(string.Format(da, "{0:dd'/'MM'/'yyyy}", date));
// Prints 09/08/2011
Console.WriteLine(string.Format(da, "{0:dd\\/MM\\/yyyy}", date));
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With