爲什麼String.Format("/")
轉換爲「 - 」?爲什麼String.Format將正斜槓轉換爲負號?
14
A
回答
21
我懷疑您在{0}
佔位符內使用/
符號。它是在給定的文化中用作日期時間分隔符的保留符號。你可以這樣逃避它:
string date = string.Format("{0:dd\\/MM\\/yyyy}", DateTime.Now);
1
6
我嘗試了所有可能的文化:
foreach (CultureInfo info in CultureInfo.GetCultures(CultureTypes.AllCultures)) {
Console.Write(String.Format(info, "/"));
}
輸出:
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////
所以,這不會發生在任何地方。
+0
哈哈,喜歡輸出 – SwDevMan81
13
根據Custom Date and Time Format Strings,/
是指培養物的日期分隔符。所以你需要逃避它。你可以按Darin的答案使用反斜槓,也可以用單引號引用它。例如:
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));
}
}
相關問題
- 1. Python將斜槓轉換爲正斜槓
- 2. 寫入文件:Printwriter將正斜槓轉換爲反斜槓
- 3. 將反斜槓逗號替換爲雙反斜槓逗號
- 4. 正斜槓替換爲反斜槓
- 5. 使用JavaScript將反斜槓轉換爲正斜槓無法正常工作?
- 6. 重寫「?」和「=」轉換爲正斜槓/
- 7. cygwin bash正斜槓轉換爲反斜槓
- 8. QMAKE&窗戶:如何反斜槓轉換爲正斜槓
- 9. Invoke-RestMethod將反斜槓轉換爲斜槓
- 10. 如何使用java將反斜槓替換爲正斜槓?
- 11. 爲正斜槓
- 12. 爲什麼wp_update_post帶斜槓?
- 13. 如何使用javascript替換正斜槓爲反斜槓和正斜槓
- 14. 正斜槓更改爲反斜槓和正斜槓
- 15. 爲什麼msyqli_real_escape_string()不能正確地轉義多個反斜槓?
- 16. 如何在C#中將單斜槓替換爲雙斜槓?
- 17. 將反斜槓更改爲在QString中正斜槓
- 18. 如何使用java將正斜槓更改爲反斜槓?
- 19. 紅寶石 - 將單個反斜槓unicode轉換爲雙反斜槓
- 20. 爲什麼將帶符號的負數轉換爲無符號數?
- 21. sed用反斜槓替換正斜槓
- 22. 「//」(正斜槓,正斜槓)在PHP路徑名中做什麼?
- 23. 爲什麼string.format?
- 24. 設置它採用TR來反斜槓轉換成正斜槓
- 25. AutoHotkey:正斜槓作爲「q」並在CMD中反轉括號
- 26. 正斜槓 '/' 作爲R
- 27. 將括號(負數)轉換爲雙倍
- 28. 爲什麼php在讀取反斜槓?
- 29. 爲什麼這個div id有斜槓?
- 30. encodeURIComponent和urldecode刪除斜槓,爲什麼?
堆棧溢出社區是免費的,並鼓勵他們盡最大努力改善問題。如果你對此不舒服,Stack Overflow可能不適合你。請儘早參閱[FAQ](http://stackoverflow.com/faq)。 –