Language
Note
ãã®ããŒãžã«ã¢ã¯ã»ã¹ããã«ã¯ãæ¿èªãå¿ èŠã§ãã ãµã€ã³ã€ã³ãŸãã¯ãã£ã¬ã¯ããªã®å€æŽã詊ãããšãã§ããŸãã
ãã®ããŒãžã«ã¢ã¯ã»ã¹ããã«ã¯ãæ¿èªãå¿ èŠã§ãã ãã£ã¬ã¯ããªã®å€æŽã詊ãããšãã§ããŸãã
ã°ããŒããªãŒãŒã·ã§ã³ã«ã¯ãè€æ°ã®ã«ã«ãã£ã®ãŠãŒã¶ãŒåãã«ããŒã«ã©ã€ãºãããã€ã³ã¿ãŒãã§ã€ã¹ãšå°åããŒã¿ããµããŒããããäžç察å¿ã®ã¢ããªã®èšèšãšéçºãå«ãŸããŸãã èšèšãã§ãŒãºãéå§ããåã«ãã¢ããªããµããŒãããã«ã«ãã£ã決å®ããå¿ èŠããããŸãã ã¢ããªã¯ 1 ã€ã®ã«ã«ãã£ãŸãã¯ãªãŒãžã§ã³ãæ¢å®ãšããŠå¯Ÿè±¡ãšããŸãããä»ã®ã«ã«ãã£ãå°åã®ãŠãŒã¶ãŒã«ç°¡åã«æ¡åŒµã§ããããã«ãã¢ããªãèšèšããã³èšè¿°ã§ããŸãã
éçºè
ã¯çãã«ã«ãã£ã«ãã£ãŠåœ¢æããããŠãŒã¶ãŒ ã€ã³ã¿ãŒãã§ã€ã¹ãšããŒã¿ã«é¢ããåæãæã£ãŠããŸãã ããšãã°ãç±³åœã®è±èªã話ãéçºè
ã«ãšã£ãŠãæ¥ä»ãšæå»ã®ããŒã¿ãæååãšããŠã·ãªã¢ã«åãã MM/dd/yyyy hh:mm:ss ã¯ãå®å
šã«åŠ¥åœãªããã§ãã ãã ããç°ãªãã«ã«ãã£ã®ã·ã¹ãã ã§ãã®æååãéã·ãªã¢ã«åãããšãFormatException äŸå€ãçºçããããäžæ£ç¢ºãªããŒã¿ãçæããããããå¯èœæ§ããããŸãã ã°ããŒããªãŒãŒã·ã§ã³ã䜿çšãããšããã®ãããªã«ã«ãã£åºæã®åæãç¹å®ããã¢ããªã®èšèšãã³ãŒãã«åœ±é¿ãäžããªãããšã確èªã§ããŸãã
ãã®èšäºã§ã¯ãèæ ®ãã¹ãäž»ãªåé¡ã®äžéšãšãã°ããŒãã«åãããã¢ããªã§æååãæ¥ä»ãšæå»ã®å€ãæ°å€ãåŠçãããšãã«åŸãããšãã§ãããã¹ã ãã©ã¯ãã£ã¹ã«ã€ããŠèª¬æããŸãã
ã¹ããªã³ã°ã¹
æåãšæååã®åŠçã¯ãåã«ã«ãã£ãŸãã¯é åã§ç°ãªãæåãšæåã»ããã䜿çšããç°ãªãæ¹æ³ã§äžŠã¹æ¿ããå¯èœæ§ããããããã°ããŒããªãŒãŒã·ã§ã³ã®äžå¿çãªçŠç¹ã§ãã ãã®ã»ã¯ã·ã§ã³ã§ã¯ãã°ããŒãã«åãããã¢ããªã§æååã䜿çšããããã®æšå¥šäºé ã瀺ããŸãã
Unicode ãå éšçã«äœ¿çšãã
æ¢å®ã§ã¯ã.NET ã§ã¯ Unicode æååã䜿çšãããŸãã Unicode æååã¯ã0 åã1 ã€ä»¥äžã® Char ãªããžã§ã¯ãã§æ§æãããããããã UTF-16 ã³ãŒãåäœã衚ããŸãã äžçäžã§äœ¿çšãããŠãããã¹ãŠã®æåã»ããã®ã»ãŒãã¹ãŠã®æåã« Unicode 衚çŸããããŸãã
Windows ãªãã¬ãŒãã£ã³ã° ã·ã¹ãã ãå«ãå€ãã®ã¢ããªã±ãŒã·ã§ã³ãšãªãã¬ãŒãã£ã³ã° ã·ã¹ãã ã§ã¯ãã³ãŒã ããŒãžã䜿çšããŠæåã»ããã衚ãããšãã§ããŸãã ã³ãŒã ããŒãžã«ã¯éåžžã0x00ãã 0x7F ãŸã§ã®æšæº ASCII å€ãå«ãŸããŠãããä»ã®æåã 0x80 ãã 0xFF ãŸã§ã®æ®ãã®å€ã«ãããããŸãã 0x80ãã0xFFãŸã§ã®å€ã®è§£éã¯ãç¹å®ã®ã³ãŒã ããŒãžã«ãã£ãŠç°ãªããŸãã ãã®ãããå¯èœã§ããã°ãã°ããŒãã«åãããã¢ããªã§ã³ãŒã ããŒãžã䜿çšããªãããã«ããå¿ èŠããããŸãã
次ã®äŸã¯ãã·ã¹ãã äžã®æ¢å®ã®ã³ãŒã ããŒãžããããŒã¿ãä¿åãããã³ãŒã ããŒãžãšç°ãªãå Žåã«ãã³ãŒã ããŒãž ããŒã¿ãè§£éããå±éºæ§ã瀺ããŠããŸãã (ãã®ã·ããªãªãã·ãã¥ã¬ãŒãããããã«ããã®äŸã§ã¯ç°ãªãã³ãŒã ããŒãžãæç€ºçã«æå®ããŸã)ãæåã«ãã®ãªã·ã£èªã®ã¢ã«ãã¡ãããã®å€§æåã§æ§æãããé åãå®çŸ©ããŸãã ã³ãŒãããŒãž 737 (MS-DOS ã®ãªã·ã£) ã䜿çšããŠãã€ãé åã«ãšã³ã³ãŒããããã®ãã€ãé åããã¡ã€ã«ã«ä¿åããŸãã ãã¡ã€ã«ãååŸããããã®ãã€ãé åãã³ãŒãã»ããŒãž 737 ã䜿çšããŠãã³ãŒãããããšãå ã®æåã埩å ãããŸãã ãã ãããã¡ã€ã«ãååŸããããã®ãã€ãé åãã³ãŒã ããŒãž 1252 (ãŸãã¯ã©ãã³ã¢ã«ãã¡ãããã®æåã衚ã Windows-1252) ã䜿çšããŠãã³ãŒããããå Žåãå ã®æåã¯å€±ãããŸãã
using System;
using System.IO;
using System.Text;
public class Example
{
public static void CodePages()
{
// Represent Greek uppercase characters in code page 737.
char[] greekChars =
{
'Î', 'Î', 'Î', 'Î', 'Î', 'Î', 'Î', 'Î',
'Î', 'Î', 'Î', 'Î', 'Î', 'Î', 'Î', 'Î ',
'Ρ', 'Σ', '΀', 'Υ', 'Ί', 'Χ', 'Κ', 'Ω'
};
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
Encoding cp737 = Encoding.GetEncoding(737);
int nBytes = cp737.GetByteCount(greekChars);
byte[] bytes737 = new byte[nBytes];
bytes737 = cp737.GetBytes(greekChars);
// Write the bytes to a file.
FileStream fs = new FileStream(@".\\CodePageBytes.dat", FileMode.Create);
fs.Write(bytes737, 0, bytes737.Length);
fs.Close();
// Retrieve the byte data from the file.
fs = new FileStream(@".\\CodePageBytes.dat", FileMode.Open);
byte[] bytes1 = new byte[fs.Length];
fs.Read(bytes1, 0, (int)fs.Length);
fs.Close();
// Restore the data on a system whose code page is 737.
string data = cp737.GetString(bytes1);
Console.WriteLine(data);
Console.WriteLine();
// Restore the data on a system whose code page is 1252.
Encoding cp1252 = Encoding.GetEncoding(1252);
data = cp1252.GetString(bytes1);
Console.WriteLine(data);
}
}
// The example displays the following output:
// ÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎΠΡΣ΀ΥΊΧΚΩ
// â¬ÂâÆââŠâ â¡Ëâ°Å â¹ÅÂÅœÂÂââ""â¢ââ
Imports System.IO
Imports System.Text
Module Example
Public Sub CodePages()
' Represent Greek uppercase characters in code page 737.
Dim greekChars() As Char = {"Î"c, "Î"c, "Î"c, "Î"c, "Î"c, "Î"c, "Î"c, "Î"c,
"Î"c, "Î"c, "Î"c, "Î"c, "Î"c, "Î"c, "Î"c, "Î "c,
"Ρ"c, "Σ"c, "΀"c, "Υ"c, "Ί"c, "Χ"c, "Κ"c, "Ω"c}
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance)
Dim cp737 As Encoding = Encoding.GetEncoding(737)
Dim nBytes As Integer = CInt(cp737.GetByteCount(greekChars))
Dim bytes737(nBytes - 1) As Byte
bytes737 = cp737.GetBytes(greekChars)
' Write the bytes to a file.
Dim fs As New FileStream(".\CodePageBytes.dat", FileMode.Create)
fs.Write(bytes737, 0, bytes737.Length)
fs.Close()
' Retrieve the byte data from the file.
fs = New FileStream(".\CodePageBytes.dat", FileMode.Open)
Dim bytes1(CInt(fs.Length - 1)) As Byte
fs.Read(bytes1, 0, CInt(fs.Length))
fs.Close()
' Restore the data on a system whose code page is 737.
Dim data As String = cp737.GetString(bytes1)
Console.WriteLine(data)
Console.WriteLine()
' Restore the data on a system whose code page is 1252.
Dim cp1252 As Encoding = Encoding.GetEncoding(1252)
data = cp1252.GetString(bytes1)
Console.WriteLine(data)
End Sub
End Module
' The example displays the following output:
' ÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎΠΡΣ΀ΥΊΧΚΩ
' â¬ÂâÆââŠâ â¡Ëâ°Å â¹ÅÂÅœÂÂââ""â¢ââ
Unicode ã䜿çšãããšãåãã³ãŒãåäœãåžžã«åãæåã«ããããããåãæåãåžžã«åããã€ãé åã«ããããããããã«ãªããŸãã
ãªãœãŒã¹ ãã¡ã€ã«ã䜿çšãã
åäžã®ã«ã«ãã£ãŸãã¯ãªãŒãžã§ã³ã察象ãšããã¢ããªãéçºããŠããå Žåã§ãããªãœãŒã¹ ãã¡ã€ã«ã䜿çšããŠããŠãŒã¶ãŒ ã€ã³ã¿ãŒãã§ã€ã¹ã«è¡šç€ºãããæååããã®ä»ã®ãªãœãŒã¹ãæ ŒçŽããå¿ èŠããããŸãã ã³ãŒãã«çŽæ¥è¿œå ããªãã§ãã ããã ãªãœãŒã¹ ãã¡ã€ã«ã®äœ¿çšã«ã¯ãããã€ãã®å©ç¹ããããŸãã
- ãã¹ãŠã®æåå㯠1 ã€ã®å Žæã«ãããŸãã ç¹å®ã®èšèªãŸãã¯ã«ã«ãã£ã倿Žããæååãèå¥ããããã«ããœãŒã¹ ã³ãŒãå šäœãæ€çŽ¢ããå¿ èŠã¯ãããŸããã
- æååãè€è£œããå¿ èŠã¯ãããŸããã ãªãœãŒã¹ ãã¡ã€ã«ã䜿çšããªãéçºè ã¯ãå€ãã®å Žåãè€æ°ã®ãœãŒã¹ ã³ãŒã ãã¡ã€ã«ã§åãæååãå®çŸ©ããŸãã ãã®éè€ã«ãããæååã倿Žããããšãã« 1 ã€ä»¥äžã®ã€ã³ã¹ã¿ã³ã¹ãèŠèœãšãããå¯èœæ§ãé«ããªããŸãã
- ã€ã¡ãŒãžããã€ã㪠ããŒã¿ãªã©ã®æåå以å€ã®ãªãœãŒã¹ã¯ãå¥ã®ã¹ã¿ã³ãã¢ãã³ ãã¡ã€ã«ã«æ ŒçŽããã®ã§ã¯ãªãããªãœãŒã¹ ãã¡ã€ã«ã«å«ããããšãã§ãããããç°¡åã«ååŸã§ããŸãã
ããŒã«ã©ã€ãºãããã¢ããªãäœæããå ŽåããªãœãŒã¹ ãã¡ã€ã«ã®äœ¿çšã«ã¯ç¹ã«å©ç¹ããããŸãã ãµãã©ã€ã ã¢ã»ã³ããªã«ãªãœãŒã¹ããããã€ãããšãå ±éèšèªã©ã³ã¿ã€ã ã¯ã CultureInfo.CurrentUICulture ããããã£ã§å®çŸ©ãããŠãããŠãŒã¶ãŒã®çŸåšã® UI ã«ã«ãã£ã«åºã¥ããŠãã«ã«ãã£ã«é©ãããªãœãŒã¹ãèªåçã«éžæããŸãã é©åãªã«ã«ãã£åºæã®ãªãœãŒã¹ãæå®ãã ResourceManager ãªããžã§ã¯ããæ£ããã€ã³ã¹ã¿ã³ã¹åããããå³å¯ã«åæå®ããããªãœãŒã¹ ã¯ã©ã¹ã䜿çšããéããã©ã³ã¿ã€ã ã¯é©åãªãªãœãŒã¹ã®ååŸã®è©³çްãåŠçããŸãã
ãªãœãŒã¹ã»ãã¡ã€ã«ã®äœæã®è©³çްã«ã€ããŠã¯ããªãœãŒã¹ã»ãã¡ã€ã« ã®äœæãåç §ããŠãã ããã ãµãã©ã€ã ã¢ã»ã³ããªã®äœæãšé 眮ã®è©³çްã«ã€ããŠã¯ãããµãã©ã€ã ã¢ã»ã³ããªã® äœæ ãããã³ã ãªãœãŒã¹ã®ããã±ãŒãžåãšãããã€ããåç §ããŠãã ããã
æååã®æ€çŽ¢ãšæ¯èŒ
å¯èœãªéããæååãäžé£ã®åã ã®æåãšããŠåŠçããã®ã§ã¯ãªããæååå šäœãšããŠåŠçããå¿ èŠããããŸãã ããã¯ãçµåãããæåã®è§£æã«é¢é£ããåé¡ãé²ãããã«ãéšåæååãäžŠã¹æ¿ãããæ€çŽ¢ãããããå Žåã«ç¹ã«éèŠã§ãã
ãã³ã
StringInfo ã¯ã©ã¹ã䜿çšããŠãæååå ã®åã ã®æåã§ã¯ãªãããã¹ãèŠçŽ ãæäœã§ããŸãã
æååã®æ€çŽ¢ãšæ¯èŒã§ã¯ãäžè¬çãªééãã¯ãæååãæåã®ã³ã¬ã¯ã·ã§ã³ãšããŠæ±ãããšã§ãããããããã Char ãªããžã§ã¯ãã«ãã£ãŠè¡šãããŸãã å®éã«ã¯ã1 ã€ã®æåã¯ã1 ã€ã2 ã€ããŸãã¯è€æ°ã® Char ãªããžã§ã¯ãã«ãã£ãŠåœ¢æãããå ŽåããããŸãã ãã®ãããªæåã¯ãUnicode Basic ã©ãã³æåç¯å² (U+0021 ãã U+007E) ã®ç¯å²å€ã®æåã§æ§æãããã¢ã«ãã¡ããããæã€ã«ã«ãã£ã®æååã§æãé »ç¹ã«èŠãããŸãã æ¬¡ã®äŸã§ã¯ãæååå ã®ã©ãã³å€§æå A WITH GRAVE æå (U+00C0) ã®ã€ã³ããã¯ã¹ãæ€çŽ¢ããŸãã ãã ãããã®æåã¯ã1 ã€ã®ã³ãŒãåäœ (U+00C0) ãŸãã¯è€åæå (U+0041 ãš U+0300 ã® 2 ã€ã®ã³ãŒãåäœ) ã® 2 ã€ã®ç°ãªãæ¹æ³ã§è¡šãããšãã§ããŸãã ãã®å Žåãæååã€ã³ã¹ã¿ã³ã¹ã§ã¯ãU+0041 ãš U+0300 ã® 2 ã€ã® Char ãªããžã§ã¯ãã«ãã£ãŠæåã衚ãããŸãã ãã®ã³ãŒãäŸã§ã¯ã String.IndexOf(Char) ãš String.IndexOf(String) ãªãŒããŒããŒããåŒã³åºããŠãæååã€ã³ã¹ã¿ã³ã¹å ã®ãã®æåã®äœçœ®ãæ€çŽ¢ããŸããããããã¯ç°ãªãçµæãè¿ããŸãã æåã®ã¡ãœããåŒã³åºãã«ã¯ Char åŒæ°ããããŸããåºæ°æ¯èŒãå®è¡ãããããäžèŽãããã®ãèŠã€ãããŸããã 2 çªç®ã®åŒã³åºãã«ã¯ String åŒæ°ããããŸããã«ã«ãã£ã«äŸåããæ¯èŒãå®è¡ãããããäžèŽãèŠã€ãããŸãã
using System;
using System.Globalization;
using System.Threading;
public class Example17
{
public static void Main17()
{
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("pl-PL");
string composite = "\u0041\u0300";
Console.WriteLine($"Comparing using Char: {composite.IndexOf('\u00C0')}");
Console.WriteLine($"Comparing using String: {composite.IndexOf("\u00C0")}");
}
}
// The example displays the following output:
// Comparing using Char: -1
// Comparing using String: 0
Imports System.Globalization
Imports System.Threading
Module Example17
Public Sub Main17()
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("pl-PL")
Dim composite As String = ChrW(&H41) + ChrW(&H300)
Console.WriteLine("Comparing using Char: {0}", composite.IndexOf(ChrW(&HC0)))
Console.WriteLine("Comparing using String: {0}", composite.IndexOf(ChrW(&HC0).ToString()))
End Sub
End Module
' The example displays the following output:
' Comparing using Char: -1
' Comparing using String: 0
StringComparisonãString.IndexOf(String, StringComparison) ã¡ãœãããªã©ã®String.LastIndexOf(String, StringComparison) ãã©ã¡ãŒã¿ãŒãå«ããªãŒããŒããŒããåŒã³åºãããšã§ããã®äŸã®ãããŸãã (ç°ãªãçµæãè¿ãã¡ãœããã® 2 ã€ã®åæ§ã®ãªãŒããŒããŒãã®åŒã³åºã) ãåé¿ã§ããŸãã
ãã ããæ€çŽ¢ã¯å¿ ãããæåã«ææã§ã¯ãããŸããã æ€çŽ¢ã®ç®çãã»ãã¥ãªãã£äžã®æ±ºå®ãè¡ãå ŽåããŸãã¯ãªãœãŒã¹ãžã®ã¢ã¯ã»ã¹ãèš±å¯ãŸãã¯çŠæ¢ããå Žåã¯ã次ã®ã»ã¯ã·ã§ã³ã§èª¬æããããã«ãæ¯èŒãåºæ°ã«ããå¿ èŠããããŸãã
æååã®ç䟡æ§ããã¹ããã
äžŠã¹æ¿ãé åºã§æ¯èŒããæ¹æ³ã決å®ããã®ã§ã¯ãªãã2 ã€ã®æååã®ç䟡æ§ããã¹ãããå Žåã¯ãString.EqualsãString.Compareãªã©ã®æå忝èŒã¡ãœããã§ã¯ãªããCompareInfo.Compare ã¡ãœããã䜿çšããŸãã
ç䟡æ§ã®æ¯èŒã¯ãéåžžãæ¡ä»¶ä»ãã§äžéšã®ãªãœãŒã¹ã«ã¢ã¯ã»ã¹ããããã«å®è¡ãããŸãã ããšãã°ããã¹ã¯ãŒããæ€èšŒãããããã¡ã€ã«ãååšããããšã確èªãããããããã«ãç䟡æ§ã®æ¯èŒãå®è¡ã§ããŸãã ãã®ãããªéèšèªçãªæ¯èŒã¯ãåžžã«ã«ã«ãã£ã«äŸåããã®ã§ã¯ãªãåºæ°ã§ããå¿ èŠããããŸãã äžè¬ã«ãã€ã³ã¹ã¿ã³ã¹ String.Equals(String, StringComparison) ã¡ãœãããŸãã¯éç String.Equals(String, String, StringComparison) ã¡ãœãããåŒã³åºãããã¹ã¯ãŒããªã©ã®æååã®å€ã StringComparison.Ordinal ãããã¡ã€ã«åã URI ãªã©ã®æååã®å€ã StringComparison.OrdinalIgnoreCase ããå¿ èŠããããŸãã
ç䟡æ§ã®æ¯èŒã«ã¯ã String.Equals ã¡ãœããã®åŒã³åºãã§ã¯ãªããæ€çŽ¢ãéšåæååã®æ¯èŒãå«ãŸããå ŽåããããŸãã å Žåã«ãã£ãŠã¯ãéšåæååæ€çŽ¢ã䜿çšããŠããã®éšåæååãå¥ã®æååãšçãããã©ããã倿ã§ããŸãã ãã®æ¯èŒã®ç®çãèšèªçã§ãªãå Žåã¯ãã«ã«ãã£ã«äŸåããã®ã§ã¯ãªããæ€çŽ¢ã®é åºãæå®ããå¿ èŠããããŸãã
次ã®äŸã¯ãéèšèªããŒã¿ã«å¯Ÿããã«ã«ãã£ã«äŸåããæ€çŽ¢ã®å±éºæ§ã瀺ããŠããŸãã
AccessesFileSystemã¡ãœããã¯ãéšåæåå "FILE" ã§å§ãŸã URI ã®ãã¡ã€ã« ã·ã¹ãã ã¢ã¯ã»ã¹ãçŠæ¢ããããã«èšèšãããŠããŸãã ãããå®è¡ãããããã«ã«ãã£ã«äŸåãã倧æåãšå°æåãåºå¥ããªãæ¯èŒãå®è¡ããŠãURI ã®å
é ãšæåå "FILE" ãæ¯èŒããŸãã ãã¡ã€ã« ã·ã¹ãã ã«ã¢ã¯ã»ã¹ãã URI 㯠"FILE:" ãŸã㯠"file:" ã§å§ããããšãã§ãããããæé»çãªåæã¯ã"i" (U+0069) ã¯åžžã«å°æåã® "I" (U+0049) ã«çžåœããŸãã ãã ãããã«ã³èªãšã¢ãŒã«ãã€ãžã£ã³èªã§ã¯ã倧æåã® "i" 㯠"İ" (U+0130) ã§ãã ãã®äžäžèŽã®ãããã«ã«ãã£ã«äŸåããæ¯èŒã§ã¯ãçŠæ¢ããå¿
èŠãããå Žåã«ãã¡ã€ã« ã·ã¹ãã ãžã®ã¢ã¯ã»ã¹ãèš±å¯ãããŸãã
using System;
using System.Globalization;
using System.Threading;
public class Example10
{
public static void Main10()
{
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("tr-TR");
string uri = @"file:\\c:\users\username\Documents\bio.txt";
if (!AccessesFileSystem(uri))
// Permit access to resource specified by URI
Console.WriteLine("Access is allowed.");
else
// Prohibit access.
Console.WriteLine("Access is not allowed.");
}
private static bool AccessesFileSystem(string uri)
{
return uri.StartsWith("FILE", true, CultureInfo.CurrentCulture);
}
}
// The example displays the following output:
// Access is allowed.
Imports System.Globalization
Imports System.Threading
Module Example10
Public Sub Main10()
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("tr-TR")
Dim uri As String = "file:\\c:\users\username\Documents\bio.txt"
If Not AccessesFileSystem(uri) Then
' Permit access to resource specified by URI
Console.WriteLine("Access is allowed.")
Else
' Prohibit access.
Console.WriteLine("Access is not allowed.")
End If
End Sub
Private Function AccessesFileSystem(uri As String) As Boolean
Return uri.StartsWith("FILE", True, CultureInfo.CurrentCulture)
End Function
End Module
' The example displays the following output:
' Access is allowed.
次ã®äŸã«ç€ºãããã«ã倧æåãšå°æåãç¡èŠããåºæ°ã«åºã¥ãæ¯èŒãå®è¡ããŠããã®åé¡ãåé¿ã§ããŸãã
using System;
using System.Globalization;
using System.Threading;
public class Example11
{
public static void Main11()
{
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("tr-TR");
string uri = @"file:\\c:\users\username\Documents\bio.txt";
if (!AccessesFileSystem(uri))
// Permit access to resource specified by URI
Console.WriteLine("Access is allowed.");
else
// Prohibit access.
Console.WriteLine("Access is not allowed.");
}
private static bool AccessesFileSystem(string uri)
{
return uri.StartsWith("FILE", StringComparison.OrdinalIgnoreCase);
}
}
// The example displays the following output:
// Access is not allowed.
Imports System.Globalization
Imports System.Threading
Module Example11
Public Sub Main11()
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("tr-TR")
Dim uri As String = "file:\\c:\users\username\Documents\bio.txt"
If Not AccessesFileSystem(uri) Then
' Permit access to resource specified by URI
Console.WriteLine("Access is allowed.")
Else
' Prohibit access.
Console.WriteLine("Access is not allowed.")
End If
End Sub
Private Function AccessesFileSystem(uri As String) As Boolean
Return uri.StartsWith("FILE", StringComparison.OrdinalIgnoreCase)
End Function
End Module
' The example displays the following output:
' Access is not allowed.
æååã®æŽåãšäžŠã¹æ¿ã
éåžžããŠãŒã¶ãŒ ã€ã³ã¿ãŒãã§ã€ã¹ã«è¡šç€ºãããé åºä»ããããæååã¯ãã«ã«ãã£ã«åºã¥ããŠäžŠã¹æ¿ããå¿ èŠããããŸãã ã»ãšãã©ã®å Žåããã®ãããªæå忝èŒã¯ã Array.Sort ã List<T>.Sortãªã©ã®æååãäžŠã¹æ¿ããã¡ãœãããåŒã³åºããšãã«ã.NET ã«ãã£ãŠæé»çã«åŠçãããŸãã ããã©ã«ãã§ã¯ãæååã¯çŸåšã®æåã®ãœãŒãèŠåã䜿çšããŠäžŠã¹æ¿ããããŸãã æ¬¡ã®äŸã¯ãè±èª (ç±³åœ) ã«ã«ãã£ãšã¹ãŠã§ãŒãã³èª (ã¹ãŠã§ãŒãã³) ã«ã«ãã£ã®èŠåã䜿çšããŠæååã®é åãäžŠã¹æ¿ããå Žåã®éãã瀺ããŠããŸãã
using System;
using System.Globalization;
using System.Threading;
public class Example18
{
public static void Main18()
{
string[] values = { "able", "Ã¥ngström", "apple", "Ãble",
"Windows", "Visual Studio" };
// Change thread to en-US.
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
// Sort the array and copy it to a new array to preserve the order.
Array.Sort(values);
string[] enValues = (String[])values.Clone();
// Change culture to Swedish (Sweden).
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("sv-SE");
Array.Sort(values);
string[] svValues = (String[])values.Clone();
// Compare the sorted arrays.
Console.WriteLine("{0,-8} {1,-15} {2,-15}\n", "Position", "en-US", "sv-SE");
for (int ctr = 0; ctr <= values.GetUpperBound(0); ctr++)
Console.WriteLine("{0,-8} {1,-15} {2,-15}", ctr, enValues[ctr], svValues[ctr]);
}
}
// The example displays the following output:
// Position en-US sv-SE
//
// 0 able able
// 1 Ãble Ãble
// 2 ångström apple
// 3 apple Windows
// 4 Visual Studio Visual Studio
// 5 Windows ångström
Imports System.Globalization
Imports System.Threading
Module Example18
Public Sub Main18()
Dim values() As String = {"able", "ångström", "apple",
"Ãble", "Windows", "Visual Studio"}
' Change thread to en-US.
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US")
' Sort the array and copy it to a new array to preserve the order.
Array.Sort(values)
Dim enValues() As String = CType(values.Clone(), String())
' Change culture to Swedish (Sweden).
Thread.CurrentThread.CurrentCulture = New CultureInfo("sv-SE")
Array.Sort(values)
Dim svValues() As String = CType(values.Clone(), String())
' Compare the sorted arrays.
Console.WriteLine("{0,-8} {1,-15} {2,-15}", "Position", "en-US", "sv-SE")
Console.WriteLine()
For ctr As Integer = 0 To values.GetUpperBound(0)
Console.WriteLine("{0,-8} {1,-15} {2,-15}", ctr, enValues(ctr), svValues(ctr))
Next
End Sub
End Module
' The example displays the following output:
' Position en-US sv-SE
'
' 0 able able
' 1 Ãble Ãble
' 2 ångström apple
' 3 apple Windows
' 4 Visual Studio Visual Studio
' 5 Windows ångström
ã«ã«ãã£ã«äŸåããæå忝èŒã¯ãåã«ã«ãã£ã®CompareInfo ããããã£ã«ãã£ãŠè¿ãããCultureInfo.CompareInfo ãªããžã§ã¯ãã«ãã£ãŠå®çŸ©ãããŸãã String.Compare ã¡ãœããã®ãªãŒããŒããŒãã䜿çšããã«ã«ãã£ã«äŸåããæå忝èŒã§ããCompareInfo ãªããžã§ã¯ãã䜿çšãããŸãã
.NET ã§ã¯ãããŒãã«ã䜿çšããŠãæååããŒã¿ã«å¯ŸããŠã«ã«ãã£ã«äŸåããäžŠã¹æ¿ããå®è¡ããŸãã äžŠã¹æ¿ãã®éã¿ãšæååã®æ£èŠåã«é¢ããããŒã¿ãå«ããããã®ããŒãã«ã®å 容ã¯ã.NET ã®ç¹å®ã®ããŒãžã§ã³ã«ãã£ãŠå®è£ ããã Unicode æšæºã®ããŒãžã§ã³ã«ãã£ãŠæ±ºãŸããŸãã æ¬¡ã®è¡šã«ãæå®ããããŒãžã§ã³ã® .NET ã«ãã£ãŠå®è£ ããã Unicode ã®ããŒãžã§ã³ã瀺ããŸãã ãµããŒããããŠãã Unicode ããŒãžã§ã³ã®äžèЧã¯ãæåã®æ¯èŒãšäžŠã¹æ¿ãã«ã®ã¿é©çšãããŸããã«ããŽãªå¥ã® Unicode æåã®åé¡ã«ã¯é©çšãããŸããã 詳现ã«ã€ããŠã¯ã String èšäºã®ãæååãš Unicode æšæºãã»ã¯ã·ã§ã³ãåç §ããŠãã ããã
| .NET Framework ã®ããŒãžã§ã³ | ãªãã¬ãŒãã£ã³ã° ã·ã¹ãã | Unicode ããŒãžã§ã³ |
|---|---|---|
| .NET Framework 2.0 | ãã¹ãŠã®ãªãã¬ãŒãã£ã³ã° ã·ã¹ãã | Unicode 4.1 |
| .NET Framework 3.0 | ãã¹ãŠã®ãªãã¬ãŒãã£ã³ã° ã·ã¹ãã | Unicode 4.1 |
| .NET Framework 3.5 | ãã¹ãŠã®ãªãã¬ãŒãã£ã³ã° ã·ã¹ãã | Unicode 4.1 |
| .NET Framework 4 | ãã¹ãŠã®ãªãã¬ãŒãã£ã³ã° ã·ã¹ãã | Unicode 5.0 |
| .NET Framework 4.5 以é | Windows 7 | Unicode 5.0 |
| .NET Framework 4.5 以é | Windows 8 以éã®ãªãã¬ãŒãã£ã³ã° ã·ã¹ãã | Unicode 6.3.0 |
| .NET Core ãš .NET 5 以é | åºã«ãªã OS ã§ãµããŒããããŠãã Unicode æšæºã®ããŒãžã§ã³ã«ãã£ãŠç°ãªããŸãã |
.NET Framework 4.5 以éããã¹ãŠã®ããŒãžã§ã³ã® .NET Core ããã³ .NET 5 以éã§ã¯ãæååã®æ¯èŒãšäžŠã¹æ¿ãã¯ãªãã¬ãŒãã£ã³ã° ã·ã¹ãã ã«ãã£ãŠç°ãªããŸãã Windows 7 ã§å®è¡ãããŠãã .NET Framework 4.5 以éã§ã¯ãUnicode 5.0 ãå®è£ ããç¬èªã®ããŒãã«ããããŒã¿ãååŸãããŸãã Windows 8 以éã§å®è¡ãããŠãã .NET Framework 4.5 以éã§ã¯ãUnicode 6.3 ãå®è£ ãããªãã¬ãŒãã£ã³ã° ã·ã¹ãã ããŒãã«ããããŒã¿ãååŸãããŸãã .NET Core ããã³ .NET 5 以éã§ã¯ããµããŒããããŠãã Unicode ã®ããŒãžã§ã³ã¯ãåºã«ãªããªãã¬ãŒãã£ã³ã° ã·ã¹ãã ã«ãã£ãŠç°ãªããŸãã ã«ã«ãã£ã«äŸåããäžŠã¹æ¿ããããããŒã¿ãã·ãªã¢ã«åããå Žåã¯ã SortVersion ã¯ã©ã¹ã䜿çšããŠãã·ãªã¢ã«åãããããŒã¿ã .NET ãšãªãã¬ãŒãã£ã³ã° ã·ã¹ãã ã®äžŠã¹æ¿ãé åºãšäžèŽããããã«äžŠã¹æ¿ããå¿ èŠãããã¿ã€ãã³ã°ã倿ã§ããŸãã äŸã«ã€ããŠã¯ã SortVersion ã¯ã©ã¹ã®ãããã¯ãåç §ããŠãã ããã
ã¢ããªã§åºç¯ãªã«ã«ãã£åºæã®çš®é¡ã®æååããŒã¿ãå®è¡ããå Žåã¯ã SortKey ã¯ã©ã¹ã䜿çšããŠæååãæ¯èŒã§ããŸãã äžŠã¹æ¿ãããŒã¯ãç¹å®ã®æååã®ã¢ã«ãã¡ãããé ã倧æåãšå°æåã®åºå¥ãçºé³ã®åºå¥ãªã©ãã«ã«ãã£åºæã®äžŠã¹æ¿ããŠã§ã€ããåæ ããŠããŸãã äžŠã¹æ¿ãããŒã䜿çšããæ¯èŒã¯ãã€ããªã§ãããããæé»çãŸãã¯æç€ºçã« CompareInfo ãªããžã§ã¯ãã䜿çšããæ¯èŒãããé«éã§ãã ç¹å®ã®æååã®ã«ã«ãã£åºæã®äžŠã¹æ¿ãããŒãäœæããã«ã¯ãæååã CompareInfo.GetSortKey ã¡ãœããã«æž¡ããŸãã
次ã®äŸã¯ãåã®äŸã®ããã«ãªããŸãã ãã ããArray.Sort(Array) ã¡ãœãããæé»çã«åŒã³åºãCompareInfo.Compare ã¡ãœãããåŒã³åºã代ããã«ãäžŠã¹æ¿ãããŒãæ¯èŒããSystem.Collections.Generic.IComparer<T>å®è£ ãå®çŸ©ããŸãããã®å®è£ ã¯ãArray.Sort<T>(T[], IComparer<T>) ã¡ãœãããã€ã³ã¹ã¿ã³ã¹åããŠæž¡ããŸãã
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Threading;
public class SortKeyComparer : IComparer<String>
{
public int Compare(string? str1, string? str2)
{
return (str1, str2) switch
{
(null, null) => 0,
(null, _) => -1,
(_, null) => 1,
(var s1, var s2) => SortKey.Compare(
CultureInfo.CurrentCulture.CompareInfo.GetSortKey(s1),
CultureInfo.CurrentCulture.CompareInfo.GetSortKey(s1))
};
}
}
public class Example19
{
public static void Main19()
{
string[] values = { "able", "Ã¥ngström", "apple", "Ãble",
"Windows", "Visual Studio" };
SortKeyComparer comparer = new SortKeyComparer();
// Change thread to en-US.
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
// Sort the array and copy it to a new array to preserve the order.
Array.Sort(values, comparer);
string[] enValues = (String[])values.Clone();
// Change culture to Swedish (Sweden).
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("sv-SE");
Array.Sort(values, comparer);
string[] svValues = (String[])values.Clone();
// Compare the sorted arrays.
Console.WriteLine("{0,-8} {1,-15} {2,-15}\n", "Position", "en-US", "sv-SE");
for (int ctr = 0; ctr <= values.GetUpperBound(0); ctr++)
Console.WriteLine("{0,-8} {1,-15} {2,-15}", ctr, enValues[ctr], svValues[ctr]);
}
}
// The example displays the following output:
// Position en-US sv-SE
//
// 0 able able
// 1 Ãble Ãble
// 2 ångström apple
// 3 apple Windows
// 4 Visual Studio Visual Studio
// 5 Windows ångström
Imports System.Collections.Generic
Imports System.Globalization
Imports System.Threading
Public Class SortKeyComparer : Implements IComparer(Of String)
Public Function Compare(str1 As String, str2 As String) As Integer _
Implements IComparer(Of String).Compare
Dim sk1, sk2 As SortKey
sk1 = CultureInfo.CurrentCulture.CompareInfo.GetSortKey(str1)
sk2 = CultureInfo.CurrentCulture.CompareInfo.GetSortKey(str2)
Return SortKey.Compare(sk1, sk2)
End Function
End Class
Module Example19
Public Sub Main19()
Dim values() As String = {"able", "ångström", "apple",
"Ãble", "Windows", "Visual Studio"}
Dim comparer As New SortKeyComparer()
' Change thread to en-US.
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US")
' Sort the array and copy it to a new array to preserve the order.
Array.Sort(values, comparer)
Dim enValues() As String = CType(values.Clone(), String())
' Change culture to Swedish (Sweden).
Thread.CurrentThread.CurrentCulture = New CultureInfo("sv-SE")
Array.Sort(values, comparer)
Dim svValues() As String = CType(values.Clone(), String())
' Compare the sorted arrays.
Console.WriteLine("{0,-8} {1,-15} {2,-15}", "Position", "en-US", "sv-SE")
Console.WriteLine()
For ctr As Integer = 0 To values.GetUpperBound(0)
Console.WriteLine("{0,-8} {1,-15} {2,-15}", ctr, enValues(ctr), svValues(ctr))
Next
End Sub
End Module
' The example displays the following output:
' Position en-US sv-SE
'
' 0 able able
' 1 Ãble Ãble
' 2 ångström apple
' 3 apple Windows
' 4 Visual Studio Visual Studio
' 5 Windows ångström
æååé£çµãåé¿ãã
å¯èœã§ããã°ãé£çµãããèªå¥ããå®è¡æã«æ§ç¯ãããè€åæååã䜿çšããªãã§ãã ããã è€åæååã¯ãå€ãã®å Žåãä»ã®ããŒã«ã©ã€ãºãããèšèªã«ã¯é©çšãããªãã¢ããªã®å ã®èšèªã§ææ³äžã®é åºãæ³å®ãããããããŒã«ã©ã€ãºãå°é£ã§ãã
æ¥ä»ãšæå»ãåŠçãã
æ¥ä»ãšæå»ã®å€ã®åŠçæ¹æ³ã¯ããŠãŒã¶ãŒ ã€ã³ã¿ãŒãã§ã€ã¹ã«è¡šç€ºãããããæ°žç¶åãããŠãããã«ãã£ãŠç°ãªããŸãã ãã®ã»ã¯ã·ã§ã³ã§ã¯ãäž¡æ¹ã®äœ¿çšæ³ã«ã€ããŠèª¬æããŸãã ãŸããæ¥ä»ãšæå»ãæäœãããšãã«ã¿ã€ã ãŸãŒã³ã®éããšç®è¡æŒç®ãåŠçããæ¹æ³ã«ã€ããŠã説æããŸãã
æ¥ä»ãšæå»ã衚瀺ãã
éåžžããŠãŒã¶ãŒ ã€ã³ã¿ãŒãã§ã€ã¹ã«æ¥ä»ãšæå»ã衚瀺ãããå Žåã¯ããŠãŒã¶ãŒã®ã«ã«ãã£ã®æžåŒèšå®èŠåã䜿çšããå¿
èŠããããŸããããã¯ãCultureInfo.CurrentCulture ããããã£ãšãDateTimeFormatInfo ããããã£ã«ãã£ãŠè¿ãããCultureInfo.CurrentCulture.DateTimeFormat ãªããžã§ã¯ãã«ãã£ãŠå®çŸ©ãããŸãã çŸåšã®ã«ã«ãã£ã®æžåŒèšå®èŠåã¯ã次ã®ããããã®ã¡ãœããã䜿çšããŠæ¥ä»ãæžåŒèšå®ãããšãã«èªåçã«äœ¿çšãããŸãã
- ãã©ã¡ãŒã¿ãŒãªãã® DateTime.ToString() ã¡ãœããã
- æžåŒæå®æååãå«ã DateTime.ToString(String) ã¡ãœããã
- ãã©ã¡ãŒã¿ãŒãªãã® DateTimeOffset.ToString() ã¡ãœããã
- DateTimeOffset.ToString(String)ã¯æžåŒæå®æååãå«ãã§ããŸãã
- æ¥ä»ãšå ±ã«äœ¿çšããã è€åæžåŒèšå® æ©èœã
次ã®äŸã§ã¯ã2012 幎 10 æ 11 æ¥ã®æ¥ã®åºãšæ¥ã®å ¥ãã®ããŒã¿ã 2 å衚瀺ããŸãã æåã«ãçŸåšã®ã«ã«ãã£ãã¯ãã¢ãã¢èª (ã¯ãã¢ãã¢) ã«ã次ã«è±èª (è±åœ) ã«èšå®ããŸãã ãããã®å Žåããæ¥ä»ãšæå»ã¯ããã®ã«ã«ãã£ã«é©ãã圢åŒã§è¡šç€ºãããŸãã
using System;
using System.Globalization;
using System.Threading;
public class Example3
{
static DateTime[] dates = { new DateTime(2012, 10, 11, 7, 06, 0),
new DateTime(2012, 10, 11, 18, 19, 0) };
public static void Main3()
{
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("hr-HR");
ShowDayInfo();
Console.WriteLine();
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-GB");
ShowDayInfo();
}
private static void ShowDayInfo()
{
Console.WriteLine($"Date: {dates[0]:D}");
Console.WriteLine($" Sunrise: {dates[0]:T}");
Console.WriteLine($" Sunset: {dates[1]:T}");
}
}
// The example displays the following output:
// Date: 11. listopada 2012.
// Sunrise: 7:06:00
// Sunset: 18:19:00
//
// Date: 11 October 2012
// Sunrise: 07:06:00
// Sunset: 18:19:00
Imports System.Globalization
Imports System.Threading
Module Example3
Dim dates() As Date = {New Date(2012, 10, 11, 7, 6, 0),
New Date(2012, 10, 11, 18, 19, 0)}
Public Sub Main3()
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("hr-HR")
ShowDayInfo()
Console.WriteLine()
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-GB")
ShowDayInfo()
End Sub
Private Sub ShowDayInfo()
Console.WriteLine("Date: {0:D}", dates(0))
Console.WriteLine(" Sunrise: {0:T}", dates(0))
Console.WriteLine(" Sunset: {0:T}", dates(1))
End Sub
End Module
' The example displays the following output:
' Date: 11. listopada 2012.
' Sunrise: 7:06:00
' Sunset: 18:19:00
'
' Date: 11 October 2012
' Sunrise: 07:06:00
' Sunset: 18:19:00
æ¥ä»ãšæå»ãä¿æãã
æ¥ä»ãšæå»ã®ããŒã¿ã¯ãã«ã«ãã£ã«ãã£ãŠç°ãªã圢åŒã§ä¿æããªãã§ãã ããã ããã¯äžè¬çãªããã°ã©ãã³ã° ãšã©ãŒã§ãããŒã¿ãç ŽæããŠããããã©ã³ã¿ã€ã äŸå€ãçºçããŸãã æ¬¡ã®äŸã§ã¯ãè±èª (ç±³åœ) ã«ã«ãã£ã®æžåŒèšå®èŠåã䜿çšããŠã2013 幎 1 æ 9 æ¥ãš 2013 幎 8 æ 18 æ¥ã® 2 ã€ã®æ¥ä»ãæååãšããŠã·ãªã¢ã«åããŸãã è±èª (ç±³åœ) ã«ã«ãã£ã®èŠåã䜿çšããŠããŒã¿ãååŸããã³è§£æããããšãæ£åžžã«åŸ©å ãããŸãã ãã ããè±èª (è±åœ) ã«ã«ãã£ã®èŠåã䜿çšããŠååŸããã³è§£æããããšãæåã®æ¥ä»ã¯ 9 æ 1 æ¥ãšèª€ã£ãŠè§£éãããã°ã¬ãŽãªãªæŠã«ã¯ 18 æããªãããã2 çªç®ã®æ¥ä»ã¯è§£æã«å€±æããŸãã
using System;
using System.IO;
using System.Globalization;
using System.Threading;
public class Example4
{
public static void Main4()
{
// Persist two dates as strings.
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
DateTime[] dates = { new DateTime(2013, 1, 9),
new DateTime(2013, 8, 18) };
StreamWriter sw = new StreamWriter("dateData.dat");
sw.Write("{0:d}|{1:d}", dates[0], dates[1]);
sw.Close();
// Read the persisted data.
StreamReader sr = new StreamReader("dateData.dat");
string dateData = sr.ReadToEnd();
sr.Close();
string[] dateStrings = dateData.Split('|');
// Restore and display the data using the conventions of the en-US culture.
Console.WriteLine($"Current Culture: {Thread.CurrentThread.CurrentCulture.DisplayName}");
foreach (var dateStr in dateStrings)
{
DateTime restoredDate;
if (DateTime.TryParse(dateStr, out restoredDate))
Console.WriteLine($"The date is {restoredDate:D}");
else
Console.WriteLine($"ERROR: Unable to parse {dateStr}");
}
Console.WriteLine();
// Restore and display the data using the conventions of the en-GB culture.
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-GB");
Console.WriteLine($"Current Culture: {Thread.CurrentThread.CurrentCulture.DisplayName}");
foreach (var dateStr in dateStrings)
{
DateTime restoredDate;
if (DateTime.TryParse(dateStr, out restoredDate))
Console.WriteLine($"The date is {restoredDate:D}");
else
Console.WriteLine($"ERROR: Unable to parse {dateStr}");
}
}
}
// The example displays the following output:
// Current Culture: English (United States)
// The date is Wednesday, January 09, 2013
// The date is Sunday, August 18, 2013
//
// Current Culture: English (United Kingdom)
// The date is 01 September 2013
// ERROR: Unable to parse 8/18/2013
Imports System.Globalization
Imports System.IO
Imports System.Threading
Module Example4
Public Sub Main4()
' Persist two dates as strings.
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US")
Dim dates() As DateTime = {New DateTime(2013, 1, 9),
New DateTime(2013, 8, 18)}
Dim sw As New StreamWriter("dateData.dat")
sw.Write("{0:d}|{1:d}", dates(0), dates(1))
sw.Close()
' Read the persisted data.
Dim sr As New StreamReader("dateData.dat")
Dim dateData As String = sr.ReadToEnd()
sr.Close()
Dim dateStrings() As String = dateData.Split("|"c)
' Restore and display the data using the conventions of the en-US culture.
Console.WriteLine("Current Culture: {0}",
Thread.CurrentThread.CurrentCulture.DisplayName)
For Each dateStr In dateStrings
Dim restoredDate As Date
If Date.TryParse(dateStr, restoredDate) Then
Console.WriteLine("The date is {0:D}", restoredDate)
Else
Console.WriteLine("ERROR: Unable to parse {0}", dateStr)
End If
Next
Console.WriteLine()
' Restore and display the data using the conventions of the en-GB culture.
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-GB")
Console.WriteLine("Current Culture: {0}",
Thread.CurrentThread.CurrentCulture.DisplayName)
For Each dateStr In dateStrings
Dim restoredDate As Date
If Date.TryParse(dateStr, restoredDate) Then
Console.WriteLine("The date is {0:D}", restoredDate)
Else
Console.WriteLine("ERROR: Unable to parse {0}", dateStr)
End If
Next
End Sub
End Module
' The example displays the following output:
' Current Culture: English (United States)
' The date is Wednesday, January 09, 2013
' The date is Sunday, August 18, 2013
'
' Current Culture: English (United Kingdom)
' The date is 01 September 2013
' ERROR: Unable to parse 8/18/2013
ãã®åé¡ã¯ã次㮠3 ã€ã®æ¹æ³ã®ããããã§åé¿ã§ããŸãã
- æ¥ä»ãšæå»ãæååã§ã¯ãªããã€ããªåœ¢åŒã§ã·ãªã¢ã«åããŸãã
- ãŠãŒã¶ãŒã®ã«ã«ãã£ã«é¢ä¿ãªãåãã«ã¹ã¿ã æžåŒæå®æååã䜿çšããŠãæ¥ä»ãšæå»ã®æåå圢åŒãä¿åããŠè§£æããŸãã
- ã€ã³ããªã¢ã³ã ã«ã«ãã£ã®æžåŒèšå®èŠåã䜿çšããŠãæååãä¿åããŸãã
次ã®äŸã¯ãæåŸã®ã¢ãããŒãã瀺ããŠããŸãã éç㪠CultureInfo.InvariantCulture ããããã£ã«ãã£ãŠè¿ãããã€ã³ããªã¢ã³ã ã«ã«ãã£ã®æžåŒèšå®èŠåã䜿çšããŸãã
using System;
using System.IO;
using System.Globalization;
using System.Threading;
public class Example5
{
public static void Main5()
{
// Persist two dates as strings.
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
DateTime[] dates = { new DateTime(2013, 1, 9),
new DateTime(2013, 8, 18) };
StreamWriter sw = new StreamWriter("dateData.dat");
sw.Write(String.Format(CultureInfo.InvariantCulture,
"{0:d}|{1:d}", dates[0], dates[1]));
sw.Close();
// Read the persisted data.
StreamReader sr = new StreamReader("dateData.dat");
string dateData = sr.ReadToEnd();
sr.Close();
string[] dateStrings = dateData.Split('|');
// Restore and display the data using the conventions of the en-US culture.
Console.WriteLine($"Current Culture: {Thread.CurrentThread.CurrentCulture.DisplayName}");
foreach (var dateStr in dateStrings)
{
DateTime restoredDate;
if (DateTime.TryParse(dateStr, CultureInfo.InvariantCulture,
DateTimeStyles.None, out restoredDate))
Console.WriteLine($"The date is {restoredDate:D}");
else
Console.WriteLine($"ERROR: Unable to parse {dateStr}");
}
Console.WriteLine();
// Restore and display the data using the conventions of the en-GB culture.
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-GB");
Console.WriteLine($"Current Culture: {Thread.CurrentThread.CurrentCulture.DisplayName}");
foreach (var dateStr in dateStrings)
{
DateTime restoredDate;
if (DateTime.TryParse(dateStr, CultureInfo.InvariantCulture,
DateTimeStyles.None, out restoredDate))
Console.WriteLine($"The date is {restoredDate:D}");
else
Console.WriteLine($"ERROR: Unable to parse {dateStr}");
}
}
}
// The example displays the following output:
// Current Culture: English (United States)
// The date is Wednesday, January 09, 2013
// The date is Sunday, August 18, 2013
//
// Current Culture: English (United Kingdom)
// The date is 09 January 2013
// The date is 18 August 2013
Imports System.Globalization
Imports System.IO
Imports System.Threading
Module Example5
Public Sub Main5()
' Persist two dates as strings.
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US")
Dim dates() As DateTime = {New DateTime(2013, 1, 9),
New DateTime(2013, 8, 18)}
Dim sw As New StreamWriter("dateData.dat")
sw.Write(String.Format(CultureInfo.InvariantCulture,
"{0:d}|{1:d}", dates(0), dates(1)))
sw.Close()
' Read the persisted data.
Dim sr As New StreamReader("dateData.dat")
Dim dateData As String = sr.ReadToEnd()
sr.Close()
Dim dateStrings() As String = dateData.Split("|"c)
' Restore and display the data using the conventions of the en-US culture.
Console.WriteLine("Current Culture: {0}",
Thread.CurrentThread.CurrentCulture.DisplayName)
For Each dateStr In dateStrings
Dim restoredDate As Date
If Date.TryParse(dateStr, CultureInfo.InvariantCulture,
DateTimeStyles.None, restoredDate) Then
Console.WriteLine("The date is {0:D}", restoredDate)
Else
Console.WriteLine("ERROR: Unable to parse {0}", dateStr)
End If
Next
Console.WriteLine()
' Restore and display the data using the conventions of the en-GB culture.
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-GB")
Console.WriteLine("Current Culture: {0}",
Thread.CurrentThread.CurrentCulture.DisplayName)
For Each dateStr In dateStrings
Dim restoredDate As Date
If Date.TryParse(dateStr, CultureInfo.InvariantCulture,
DateTimeStyles.None, restoredDate) Then
Console.WriteLine("The date is {0:D}", restoredDate)
Else
Console.WriteLine("ERROR: Unable to parse {0}", dateStr)
End If
Next
End Sub
End Module
' The example displays the following output:
' Current Culture: English (United States)
' The date is Wednesday, January 09, 2013
' The date is Sunday, August 18, 2013
'
' Current Culture: English (United Kingdom)
' The date is 09 January 2013
' The date is 18 August 2013
ã·ãªã¢ã«åãšã¿ã€ã ãŸãŒã³ã®èªè
æ¥ä»ãšæå»ã®å€ã«ã¯ãäžè¬çãªæå» ("The stores open on January 2, 2013, at 9:00 A.M.") ããç¹å®ã®æç¹ ("Date of birth: January 2, 2013 6:32:00 A.M.") ãŸã§ãè€æ°ã®è§£éã䜿çšã§ããŸãã æå»å€ãç¹å®ã®æç¹ã衚ããã·ãªã¢ã«åãããå€ãã埩å ããå Žåã¯ããŠãŒã¶ãŒã®å°ççãªå Žæãã¿ã€ã ãŸãŒã³ã«é¢ä¿ãªããåãæç¹ã衚ãããã«ããå¿ èŠããããŸãã
次ã®äŸã¯ããã®åé¡ã瀺ããŠããŸãã 1 ã€ã®ããŒã«ã«ã®æ¥ä»ãšæå»ã®å€ã 3 ã€ã® æšæºåœ¢åŒã®æååãšããŠä¿åããŸãã
- äžè¬ã®æ¥ä»ãšé·ã圢åŒã®æå»ã®å Žå㯠"G"ã
- äžŠã¹æ¿ãå¯èœãªæ¥ä»/æå»ã®å Žå㯠"s" ã§ãã
- ã©ãŠã³ãããªããã®æ¥ä»/æå»ã®å Žå㯠"o" ã§ãã
using System;
using System.IO;
public class Example6
{
public static void Main6()
{
DateTime dateOriginal = new DateTime(2023, 3, 30, 18, 0, 0);
dateOriginal = DateTime.SpecifyKind(dateOriginal, DateTimeKind.Local);
// Serialize a date.
if (!File.Exists("DateInfo.dat"))
{
StreamWriter sw = new StreamWriter("DateInfo.dat");
sw.Write("{0:G}|{0:s}|{0:o}", dateOriginal);
sw.Close();
Console.WriteLine("Serialized dates to DateInfo.dat");
}
Console.WriteLine();
// Restore the date from string values.
StreamReader sr = new StreamReader("DateInfo.dat");
string datesToSplit = sr.ReadToEnd();
string[] dateStrings = datesToSplit.Split('|');
foreach (var dateStr in dateStrings)
{
DateTime newDate = DateTime.Parse(dateStr);
Console.WriteLine($"'{dateStr}' --> {newDate} {newDate.Kind}");
}
}
}
Imports System.IO
Module Example6
Public Sub Main6()
' Serialize a date.
Dim dateOriginal As Date = #03/30/2023 6:00PM#
dateOriginal = DateTime.SpecifyKind(dateOriginal, DateTimeKind.Local)
' Serialize the date in string form.
If Not File.Exists("DateInfo.dat") Then
Dim sw As New StreamWriter("DateInfo.dat")
sw.Write("{0:G}|{0:s}|{0:o}", dateOriginal)
sw.Close()
End If
' Restore the date from string values.
Dim sr As New StreamReader("DateInfo.dat")
Dim datesToSplit As String = sr.ReadToEnd()
Dim dateStrings() As String = datesToSplit.Split("|"c)
For Each dateStr In dateStrings
Dim newDate As DateTime = DateTime.Parse(dateStr)
Console.WriteLine("'{0}' --> {1} {2}",
dateStr, newDate, newDate.Kind)
Next
End Sub
End Module
ã·ãªã¢ã«åãããã·ã¹ãã ãšåãã¿ã€ã ãŸãŒã³ã®ã·ã¹ãã ã§ããŒã¿ã埩å ããããšãéã·ãªã¢ã«åãããæ¥ä»ãšæå»ã®å€ã¯ãåºåã«ç€ºãããã«å ã®å€ãæ£ç¢ºã«åæ ããŸãã
'3/30/2013 6:00:00 PM' --> 3/30/2013 6:00:00 PM Unspecified
'2013-03-30T18:00:00' --> 3/30/2013 6:00:00 PM Unspecified
'2013-03-30T18:00:00.0000000-07:00' --> 3/30/2013 6:00:00 PM Local
ãã ããç°ãªãã¿ã€ã ãŸãŒã³ã®ã·ã¹ãã äžã®ããŒã¿ã埩å ããå Žåã¯ã"o" (ã©ãŠã³ãããªãã) æšæºæžåŒæå®æååã§æžåŒèšå®ãããæ¥ä»ãšæå»ã®å€ã®ã¿ãã¿ã€ã ãŸãŒã³æ å ±ãä¿æãããããåãã€ã³ã¹ã¿ã³ã ã€ã³ ã¿ã€ã ã衚ããŸãã ããã³ã¹æšæºã¿ã€ã ãŸãŒã³ã®ã·ã¹ãã ã§æ¥ä»ãšæå»ã®ããŒã¿ã埩å ãããå Žåã®åºåãæ¬¡ã«ç€ºããŸãã
'3/30/2023 6:00:00 PM' --> 3/30/2023 6:00:00 PM Unspecified
'2023-03-30T18:00:00' --> 3/30/2023 6:00:00 PM Unspecified
'2023-03-30T18:00:00.0000000-07:00' --> 3/31/2023 3:00:00 AM Local
ããŒã¿ãéã·ãªã¢ã«åãããã·ã¹ãã ã®ã¿ã€ã ãŸãŒã³ã«é¢ä¿ãªãã1 ã€ã®æå»ãè¡šãæ¥ä»ãšæå»ã®å€ãæ£ç¢ºã«åæ ããã«ã¯ã次ã®ããããã®æäœãè¡ããŸãã
- "o" (ã©ãŠã³ãããªãã) æšæºæžåŒæå®æååã䜿çšããŠãå€ãæååãšããŠä¿åããŸãã æ¬¡ã«ãã¿ãŒã²ãã ã·ã¹ãã ã§éã·ãªã¢ã«åããŸãã
- ããã UTC ã«å€æãã"r" (RFC1123) æšæºæžåŒæå®æååã䜿çšããŠæååãšããŠä¿åããŸãã æ¬¡ã«ãã¿ãŒã²ãã ã·ã¹ãã ã§éã·ãªã¢ã«åããããŒã«ã«æå»ã«å€æããŸãã
- ããã UTC ã«å€æãã"u" (æ±çšäžŠã¹æ¿ãå¯èœ) æšæºæžåŒæå®æååã䜿çšããŠæååãšããŠä¿åããŸãã æ¬¡ã«ãã¿ãŒã²ãã ã·ã¹ãã ã§éã·ãªã¢ã«åããããŒã«ã«æå»ã«å€æããŸãã
次ã®äŸã¯ãåææ³ã瀺ããŠããŸãã
using System;
using System.IO;
public class Example9
{
public static void Main9()
{
// Serialize a date.
DateTime dateOriginal = new DateTime(2023, 3, 30, 18, 0, 0);
dateOriginal = DateTime.SpecifyKind(dateOriginal, DateTimeKind.Local);
// Serialize the date in string form.
if (!File.Exists("DateInfo2.dat"))
{
StreamWriter sw = new StreamWriter("DateInfo2.dat");
sw.Write("{0:o}|{1:r}|{1:u}", dateOriginal,
dateOriginal.ToUniversalTime());
sw.Close();
}
// Restore the date from string values.
StreamReader sr = new StreamReader("DateInfo2.dat");
string datesToSplit = sr.ReadToEnd();
string[] dateStrings = datesToSplit.Split('|');
for (int ctr = 0; ctr < dateStrings.Length; ctr++)
{
DateTime newDate = DateTime.Parse(dateStrings[ctr]);
if (ctr == 1)
{
Console.WriteLine($"'{dateStrings[ctr]}' --> {newDate} {newDate.Kind}");
}
else
{
DateTime newLocalDate = newDate.ToLocalTime();
Console.WriteLine($"'{dateStrings[ctr]}' --> {newLocalDate} {newLocalDate.Kind}");
}
}
}
}
Imports System.IO
Module Example9
Public Sub Main9()
' Serialize a date.
Dim dateOriginal As Date = #03/30/2023 6:00PM#
dateOriginal = DateTime.SpecifyKind(dateOriginal, DateTimeKind.Local)
' Serialize the date in string form.
If Not File.Exists("DateInfo2.dat") Then
Dim sw As New StreamWriter("DateInfo2.dat")
sw.Write("{0:o}|{1:r}|{1:u}", dateOriginal,
dateOriginal.ToUniversalTime())
sw.Close()
End If
' Restore the date from string values.
Dim sr As New StreamReader("DateInfo2.dat")
Dim datesToSplit As String = sr.ReadToEnd()
Dim dateStrings() As String = datesToSplit.Split("|"c)
For ctr As Integer = 0 To dateStrings.Length - 1
Dim newDate As DateTime = DateTime.Parse(dateStrings(ctr))
If ctr = 1 Then
Console.WriteLine("'{0}' --> {1} {2}",
dateStrings(ctr), newDate, newDate.Kind)
Else
Dim newLocalDate As DateTime = newDate.ToLocalTime()
Console.WriteLine("'{0}' --> {1} {2}",
dateStrings(ctr), newLocalDate, newLocalDate.Kind)
End If
Next
End Sub
End Module
ããŒã¿ãå€ªå¹³æŽæšæºã¿ã€ã ãŸãŒã³ã®ã·ã¹ãã ã§ã·ãªã¢ã«åãããããã³ã¹æšæºã¿ã€ã ãŸãŒã³ã®ã·ã¹ãã ã§éã·ãªã¢ã«åããããšã次ã®åºåã衚瀺ãããŸãã
'2023-03-30T18:00:00.0000000-07:00' --> 3/31/2023 3:00:00 AM Local
'Sun, 31 Mar 2023 01:00:00 GMT' --> 3/31/2023 3:00:00 AM Local
'2023-03-31 01:00:00Z' --> 3/31/2023 3:00:00 AM Local
詳现ã«ã€ããŠã¯ãã ã¿ã€ã ãŸãŒã³éã®æå»ã®å€æããåç §ããŠãã ããã
æ¥ä»ãšæå»ã®ç®è¡æŒç®ãå®è¡ãã
DateTimeåãšDateTimeOffsetåã®äž¡æ¹ã§ç®è¡æŒç®ããµããŒããããŸãã 2 ã€ã®æ¥ä»å€ã®å·®ãèšç®ããããšããæ¥ä»å€ãšã®éã§ç¹å®ã®æéééãå ç®ãŸãã¯æžç®ããããšãã§ããŸãã ãã ããæ¥ä»ãšæå»ã®å€ã«å¯Ÿããç®è¡æŒç®ã§ã¯ãã¿ã€ã ãŸãŒã³ãšã¿ã€ã ãŸãŒã³èª¿æŽã«ãŒã«ã¯èæ ®ãããŸããã ãã®ãããæå»ã®ç¬éã衚ãå€ã«å¯Ÿããæ¥ä»ãšæå»ã®ç®è¡æŒç®ã§ã¯ãäžæ£ç¢ºãªçµæãè¿ãããå¯èœæ§ããããŸãã
ããšãã°ãå€ªå¹³æŽæšæºæãã倪平æŽå€æéãžã®ç§»è¡ã¯ã2013 幎㮠3 æ 10 æ¥ã§ãã 3 æã®ç¬¬ 2 æ¥ææ¥ã«è¡ãããŸãã æ¬¡ã®äŸã«ç€ºãããã«ãå€ªå¹³æŽæšæºæãŸãŒã³ã®ã·ã¹ãã ã§ 2013 幎 3 æ 9 æ¥åå 10 æ 30 åãã 48 æéåŸã®æ¥ä»ãšæå»ãèšç®ããå Žåã2013 幎 3 æ 11 æ¥åå 10 æ 30 åã«ã¯ãä»å ¥ããæé調æŽã¯èæ ®ãããŸããã
using System;
public class Example7
{
public static void Main7()
{
DateTime date1 = DateTime.SpecifyKind(new DateTime(2013, 3, 9, 10, 30, 0),
DateTimeKind.Local);
TimeSpan interval = new TimeSpan(48, 0, 0);
DateTime date2 = date1 + interval;
Console.WriteLine($"{date1:g} + {interval.TotalHours:N1} hours = {date2:g}");
}
}
// The example displays the following output:
// 3/9/2013 10:30 AM + 48.0 hours = 3/11/2013 10:30 AM
Module Example7
Public Sub Main7()
Dim date1 As Date = DateTime.SpecifyKind(#3/9/2013 10:30AM#,
DateTimeKind.Local)
Dim interval As New TimeSpan(48, 0, 0)
Dim date2 As Date = date1 + interval
Console.WriteLine("{0:g} + {1:N1} hours = {2:g}",
date1, interval.TotalHours, date2)
End Sub
End Module
' The example displays the following output:
' 3/9/2013 10:30 AM + 48.0 hours = 3/11/2013 10:30 AM
æ¥ä»ãšæå»ã®å€ã«å¯Ÿããç®è¡æŒç®ã«ãã£ãŠæ£ç¢ºãªçµæãåŸãããããã«ããã«ã¯ãæ¬¡ã®æé ã«åŸããŸãã
- ãœãŒã¹ ã¿ã€ã ãŸãŒã³ã®æå»ã UTC ã«å€æããŸãã
- ç®è¡æŒç®ãå®è¡ããŸãã
- çµæãæ¥ä»ãšæå»ã®å€ã§ããå Žåã¯ãUTC ãããœãŒã¹ ã¿ã€ã ãŸãŒã³ã®æå»ã«å€æããŸãã
次ã®äŸã¯åã®äŸãšäŒŒãŠããŸããã2013 幎 3 æ 9 æ¥åå 10 æ 30 åã« 48 æéãæ£ãã远å ããã«ã¯ã次㮠3 ã€ã®æé ã«åŸããŸãã
using System;
public class Example8
{
public static void Main8()
{
TimeZoneInfo pst = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
DateTime date1 = DateTime.SpecifyKind(new DateTime(2013, 3, 9, 10, 30, 0),
DateTimeKind.Local);
DateTime utc1 = date1.ToUniversalTime();
TimeSpan interval = new TimeSpan(48, 0, 0);
DateTime utc2 = utc1 + interval;
DateTime date2 = TimeZoneInfo.ConvertTimeFromUtc(utc2, pst);
Console.WriteLine($"{date1:g} + {interval.TotalHours:N1} hours = {date2:g}");
}
}
// The example displays the following output:
// 3/9/2013 10:30 AM + 48.0 hours = 3/11/2013 11:30 AM
Module Example8
Public Sub Main8()
Dim pst As TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time")
Dim date1 As Date = DateTime.SpecifyKind(#3/9/2013 10:30AM#,
DateTimeKind.Local)
Dim utc1 As Date = date1.ToUniversalTime()
Dim interval As New TimeSpan(48, 0, 0)
Dim utc2 As Date = utc1 + interval
Dim date2 As Date = TimeZoneInfo.ConvertTimeFromUtc(utc2, pst)
Console.WriteLine("{0:g} + {1:N1} hours = {2:g}",
date1, interval.TotalHours, date2)
End Sub
End Module
' The example displays the following output:
' 3/9/2013 10:30 AM + 48.0 hours = 3/11/2013 11:30 AM
詳现ã«ã€ããŠã¯ãã æ¥ä»ãšæå»ã䜿çšããç®è¡æŒç®ã®å®è¡ããåç §ããŠãã ããã
æ¥ä»èŠçŽ ã«ã«ã«ãã£ã«äŸåããååã䜿çšãã
ã¢ããªã§ã¯ãæã®ååãŸãã¯ææ¥ã®è¡šç€ºãå¿ èŠã«ãªãå ŽåããããŸãã ãããè¡ãã«ã¯ã次ã®ãããªã³ãŒããäžè¬çã§ãã
using System;
public class Example12
{
public static void Main12()
{
DateTime midYear = new DateTime(2013, 7, 1);
Console.WriteLine($"{midYear:d} is a {GetDayName(midYear)}.");
}
private static string GetDayName(DateTime date)
{
return date.DayOfWeek.ToString("G");
}
}
// The example displays the following output:
// 7/1/2013 is a Monday.
Module Example12
Public Sub Main12()
Dim midYear As Date = #07/01/2013#
Console.WriteLine("{0:d} is a {1}.", midYear, GetDayName(midYear))
End Sub
Private Function GetDayName(dat As Date) As String
Return dat.DayOfWeek.ToString("G")
End Function
End Module
' The example displays the following output:
' 7/1/2013 is a Monday.
ãã ãããã®ã³ãŒãã¯åžžã«ææ¥ã®ååãè±èªã§è¿ããŸãã æã®ååãæœåºããã³ãŒãã¯ãå€ãã®å Žåãããã«æè»æ§ãé«ããããŸããã å€ãã®å Žåãç¹å®ã®èšèªã®æã®ååãæã€ 12 ãæã®ã«ã¬ã³ããŒãæ³å®ããŠããŸãã
次ã®äŸã«ç€ºãããã« ãã«ã¹ã¿ã ã®æ¥ææžåŒæå®æåå ãŸã㯠DateTimeFormatInfo ãªããžã§ã¯ãã®ããããã£ã䜿çšãããšããŠãŒã¶ãŒã®ã«ã«ãã£ã§ææ¥ãŸãã¯æã®ååãåæ ããæååãç°¡åã«æœåºã§ããŸãã ãã®äŸã§ã¯ãçŸåšã®ã«ã«ãã£ããã©ã³ã¹èª (ãã©ã³ã¹) ã«å€æŽãã2013 幎 7 æ 1 æ¥ã®ææ¥ã®ååãšæã®ååã衚瀺ããŸãã
using System;
using System.Globalization;
public class Example13
{
public static void Main13()
{
// Set the current culture to French (France).
CultureInfo.CurrentCulture = CultureInfo.CreateSpecificCulture("fr-FR");
DateTime midYear = new DateTime(2013, 7, 1);
Console.WriteLine($"{midYear:d} is a {DateUtilities.GetDayName(midYear)}.");
Console.WriteLine($"{midYear:d} is a {DateUtilities.GetDayName((int)midYear.DayOfWeek)}.");
Console.WriteLine($"{midYear:d} is in {DateUtilities.GetMonthName(midYear)}.");
Console.WriteLine($"{midYear:d} is in {DateUtilities.GetMonthName(midYear.Month)}.");
}
}
public static class DateUtilities
{
public static string GetDayName(int dayOfWeek)
{
if (dayOfWeek < 0 | dayOfWeek > DateTimeFormatInfo.CurrentInfo.DayNames.Length)
return String.Empty;
else
return DateTimeFormatInfo.CurrentInfo.DayNames[dayOfWeek];
}
public static string GetDayName(DateTime date)
{
return date.ToString("dddd");
}
public static string GetMonthName(int month)
{
if (month < 1 | month > DateTimeFormatInfo.CurrentInfo.MonthNames.Length - 1)
return String.Empty;
else
return DateTimeFormatInfo.CurrentInfo.MonthNames[month - 1];
}
public static string GetMonthName(DateTime date)
{
return date.ToString("MMMM");
}
}
// The example displays the following output:
// 01/07/2013 is a lundi.
// 01/07/2013 is a lundi.
// 01/07/2013 is in juillet.
// 01/07/2013 is in juillet.
Imports System.Globalization
Imports System.Threading
Module Example13
Public Sub Main13()
' Set the current culture to French (France).
CultureInfo.CurrentCulture = CultureInfo.CreateSpecificCulture("fr-FR")
Dim midYear As Date = #07/01/2013#
Console.WriteLine("{0:d} is a {1}.", midYear, DateUtilities.GetDayName(midYear))
Console.WriteLine("{0:d} is a {1}.", midYear, DateUtilities.GetDayName(midYear.DayOfWeek))
Console.WriteLine("{0:d} is in {1}.", midYear, DateUtilities.GetMonthName(midYear))
Console.WriteLine("{0:d} is in {1}.", midYear, DateUtilities.GetMonthName(midYear.Month))
End Sub
End Module
Public Class DateUtilities
Public Shared Function GetDayName(dayOfWeek As Integer) As String
If dayOfWeek < 0 Or dayOfWeek > DateTimeFormatInfo.CurrentInfo.DayNames.Length Then
Return String.Empty
Else
Return DateTimeFormatInfo.CurrentInfo.DayNames(dayOfWeek)
End If
End Function
Public Shared Function GetDayName(dat As Date) As String
Return dat.ToString("dddd")
End Function
Public Shared Function GetMonthName(month As Integer) As String
If month < 1 Or month > DateTimeFormatInfo.CurrentInfo.MonthNames.Length - 1 Then
Return String.Empty
Else
Return DateTimeFormatInfo.CurrentInfo.MonthNames(month - 1)
End If
End Function
Public Shared Function GetMonthName(dat As Date) As String
Return dat.ToString("MMMM")
End Function
End Class
' The example displays the following output:
' 01/07/2013 is a lundi.
' 01/07/2013 is a lundi.
' 01/07/2013 is in juillet.
' 01/07/2013 is in juillet.
æ°å€
æ°å€ã®åŠçã¯ããŠãŒã¶ãŒ ã€ã³ã¿ãŒãã§ã€ã¹ã«è¡šç€ºãããããæ°žç¶åãããŠãããã«ãã£ãŠç°ãªããŸãã ãã®ã»ã¯ã·ã§ã³ã§ã¯ãäž¡æ¹ã®äœ¿çšæ³ã«ã€ããŠèª¬æããŸãã
泚
è§£æããã³æžåŒèšå®æäœã§ã¯ã.NET ã§ã¯ãåºæ¬ã©ãã³æå 0 ãã 9 (U+0030 ãã U+0039) ã®ã¿ãæ°åãšããŠèªèãããŸãã
æ°å€ã衚瀺ãã
éåžžããŠãŒã¶ãŒ ã€ã³ã¿ãŒãã§ã€ã¹ã«æ°å€ã衚瀺ãããå Žåã¯ããŠãŒã¶ãŒã®ã«ã«ãã£ã®æžåŒèšå®èŠåã䜿çšããå¿
èŠããããŸããããã¯ãCultureInfo.CurrentCulture ããããã£ãšãNumberFormatInfo ããããã£ã«ãã£ãŠè¿ãããCultureInfo.CurrentCulture.NumberFormat ãªããžã§ã¯ãã«ãã£ãŠå®çŸ©ãããŸãã çŸåšã®ã«ã«ãã£ã®æžåŒèšå®èŠåã¯ãæ¬¡ã®æ¹æ³ã§æ¥ä»ãæžåŒèšå®ãããšãã«èªåçã«äœ¿çšãããŸãã
- ä»»æã®æ°å€åã®ãã©ã¡ãŒã¿ãŒãªãã®
ToStringã¡ãœããã䜿çšããŸãã - åŒæ°ãšããŠæžåŒæå®æååãå«ãä»»æã®æ°å€åã®
ToString(String)ã¡ãœããã䜿çšããŸãã - æ°å€ ã䜿çšããè€åæžåŒ ã®äœ¿çšã
次ã®äœ¿çšäŸã¯ãããª, ãã©ã³ã¹ ã® 1 ã¶æãããã®å¹³å枩床ã衚瀺ããŸãã ããŒã¿ã衚瀺ããåã«ãæåã«çŸåšã®ã«ã«ãã£ããã©ã³ã¹èª (ãã©ã³ã¹) ã«èšå®ããæ¬¡ã«è±èª (ç±³åœ) ã«èšå®ããŸãã ãããã®å Žåããæã®ååãšæž©åºŠã¯ããã®ã«ã«ãã£ã«é©ãã圢åŒã§è¡šç€ºãããŸãã 2 ã€ã®ã«ã«ãã£ã§ã¯ã枩床å€ã«ç°ãªãå°æ°ç¹åºåãèšå·ã䜿çšãããããšã«æ³šæããŠãã ããã ãŸãããã®äŸã§ã¯ã"MMMM" ã«ã¹ã¿ã ã®æ¥ä»ãšæå»ã®æžåŒæå®æååã䜿çšããŠå®å šãªæã®ååã衚瀺ãã DateTimeFormatInfo.MonthNames é åå ã®æãé·ãæã®ååã®é·ããæ±ºå®ããããšã«ãã£ãŠãçµææååã®æåã«é©åãªéã®é åãå²ãåœãŠãããšã«ã泚æããŠãã ããã
using System;
using System.Globalization;
using System.Threading;
public class Example14
{
public static void Main14()
{
DateTime dateForMonth = new DateTime(2013, 1, 1);
double[] temperatures = { 3.4, 3.5, 7.6, 10.4, 14.5, 17.2,
19.9, 18.2, 15.9, 11.3, 6.9, 5.3 };
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("fr-FR");
Console.WriteLine($"Current Culture: {CultureInfo.CurrentCulture.DisplayName}");
// Build the format string dynamically so we allocate enough space for the month name.
string fmtString = "{0,-" + GetLongestMonthNameLength().ToString() + ":MMMM} {1,4}";
for (int ctr = 0; ctr < temperatures.Length; ctr++)
Console.WriteLine(fmtString,
dateForMonth.AddMonths(ctr),
temperatures[ctr]);
Console.WriteLine();
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
Console.WriteLine($"Current Culture: {CultureInfo.CurrentCulture.DisplayName}");
fmtString = "{0,-" + GetLongestMonthNameLength().ToString() + ":MMMM} {1,4}";
for (int ctr = 0; ctr < temperatures.Length; ctr++)
Console.WriteLine(fmtString,
dateForMonth.AddMonths(ctr),
temperatures[ctr]);
}
private static int GetLongestMonthNameLength()
{
int length = 0;
foreach (var nameOfMonth in DateTimeFormatInfo.CurrentInfo.MonthNames)
if (nameOfMonth.Length > length) length = nameOfMonth.Length;
return length;
}
}
// The example displays the following output:
// Current Culture: French (France)
// janvier 3,4
// février 3,5
// mars 7,6
// avril 10,4
// mai 14,5
// juin 17,2
// juillet 19,9
// août 18,2
// septembre 15,9
// octobre 11,3
// novembre 6,9
// décembre 5,3
//
// Current Culture: English (United States)
// January 3.4
// February 3.5
// March 7.6
// April 10.4
// May 14.5
// June 17.2
// July 19.9
// August 18.2
// September 15.9
// October 11.3
// November 6.9
// December 5.3
Imports System.Globalization
Imports System.Threading
Module Example14
Public Sub Main14()
Dim dateForMonth As Date = #1/1/2013#
Dim temperatures() As Double = {3.4, 3.5, 7.6, 10.4, 14.5, 17.2,
19.9, 18.2, 15.9, 11.3, 6.9, 5.3}
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("fr-FR")
Console.WriteLine("Current Culture: {0}", CultureInfo.CurrentCulture.DisplayName)
Dim fmtString As String = "{0,-" + GetLongestMonthNameLength().ToString() + ":MMMM} {1,4}"
For ctr = 0 To temperatures.Length - 1
Console.WriteLine(fmtString,
dateForMonth.AddMonths(ctr),
temperatures(ctr))
Next
Console.WriteLine()
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US")
Console.WriteLine("Current Culture: {0}", CultureInfo.CurrentCulture.DisplayName)
' Build the format string dynamically so we allocate enough space for the month name.
fmtString = "{0,-" + GetLongestMonthNameLength().ToString() + ":MMMM} {1,4}"
For ctr = 0 To temperatures.Length - 1
Console.WriteLine(fmtString,
dateForMonth.AddMonths(ctr),
temperatures(ctr))
Next
End Sub
Private Function GetLongestMonthNameLength() As Integer
Dim length As Integer
For Each nameOfMonth In DateTimeFormatInfo.CurrentInfo.MonthNames
If nameOfMonth.Length > length Then length = nameOfMonth.Length
Next
Return length
End Function
End Module
' The example displays the following output:
' Current Culture: French (France)
' janvier 3,4
' février 3,5
' mars 7,6
' avril 10,4
' mai 14,5
' juin 17,2
' juillet 19,9
' août 18,2
' septembre 15,9
' octobre 11,3
' novembre 6,9
' décembre 5,3
'
' Current Culture: English (United States)
' January 3.4
' February 3.5
' March 7.6
' April 10.4
' May 14.5
' June 17.2
' July 19.9
' August 18.2
' September 15.9
' October 11.3
' November 6.9
' December 5.3
æ°å€ãä¿æãã
ã«ã«ãã£åºæã®åœ¢åŒã§æ°å€ããŒã¿ãä¿æããªãã§ãã ããã ããã¯äžè¬çãªããã°ã©ãã³ã° ãšã©ãŒã§ãããŒã¿ãç ŽæããŠããããã©ã³ã¿ã€ã äŸå€ãçºçããŸãã æ¬¡ã®äŸã§ã¯ã10 åã®ã©ã³ãã ãªæµ®åå°æ°ç¹æ°ãçæããè±èª (ç±³åœ) ã«ã«ãã£ã®æžåŒèšå®èŠåã䜿çšããŠæååãšããŠã·ãªã¢ã«åããŸãã è±èª (ç±³åœ) ã«ã«ãã£ã®èŠåã䜿çšããŠããŒã¿ãååŸããã³è§£æããããšãæ£åžžã«åŸ©å ãããŸãã ãã ãããã©ã³ã¹èª (ãã©ã³ã¹) ã«ã«ãã£ã®èŠåã䜿çšããŠååŸããã³è§£æããå Žåãã«ã«ãã£ã§ç°ãªãå°æ°ç¹åºåãèšå·ã䜿çšããããããã©ã®æ°å€ãè§£æã§ããŸããã
using System;
using System.Globalization;
using System.IO;
using System.Threading;
public class Example15
{
public static void Main15()
{
// Create ten random doubles.
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
double[] numbers = GetRandomNumbers(10);
DisplayRandomNumbers(numbers);
// Persist the numbers as strings.
StreamWriter sw = new StreamWriter("randoms.dat");
for (int ctr = 0; ctr < numbers.Length; ctr++)
sw.Write("{0:R}{1}", numbers[ctr], ctr < numbers.Length - 1 ? "|" : "");
sw.Close();
// Read the persisted data.
StreamReader sr = new StreamReader("randoms.dat");
string numericData = sr.ReadToEnd();
sr.Close();
string[] numberStrings = numericData.Split('|');
// Restore and display the data using the conventions of the en-US culture.
Console.WriteLine($"Current Culture: {Thread.CurrentThread.CurrentCulture.DisplayName}");
foreach (var numberStr in numberStrings)
{
double restoredNumber;
if (Double.TryParse(numberStr, out restoredNumber))
Console.WriteLine(restoredNumber.ToString("R"));
else
Console.WriteLine($"ERROR: Unable to parse '{numberStr}'");
}
Console.WriteLine();
// Restore and display the data using the conventions of the fr-FR culture.
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("fr-FR");
Console.WriteLine($"Current Culture: {Thread.CurrentThread.CurrentCulture.DisplayName}");
foreach (var numberStr in numberStrings)
{
double restoredNumber;
if (Double.TryParse(numberStr, out restoredNumber))
Console.WriteLine(restoredNumber.ToString("R"));
else
Console.WriteLine($"ERROR: Unable to parse '{numberStr}'");
}
}
private static double[] GetRandomNumbers(int n)
{
Random rnd = new Random();
double[] numbers = new double[n];
for (int ctr = 0; ctr < n; ctr++)
numbers[ctr] = rnd.NextDouble() * 1000;
return numbers;
}
private static void DisplayRandomNumbers(double[] numbers)
{
for (int ctr = 0; ctr < numbers.Length; ctr++)
Console.WriteLine(numbers[ctr].ToString("R"));
Console.WriteLine();
}
}
// The example displays output like the following:
// 487.0313743534644
// 674.12000879371533
// 498.72077885024288
// 42.3034229512808
// 970.57311049223563
// 531.33717716268131
// 587.82905693530529
// 562.25210175023039
// 600.7711019370571
// 299.46113717717174
//
// Current Culture: English (United States)
// 487.0313743534644
// 674.12000879371533
// 498.72077885024288
// 42.3034229512808
// 970.57311049223563
// 531.33717716268131
// 587.82905693530529
// 562.25210175023039
// 600.7711019370571
// 299.46113717717174
//
// Current Culture: French (France)
// ERROR: Unable to parse '487.0313743534644'
// ERROR: Unable to parse '674.12000879371533'
// ERROR: Unable to parse '498.72077885024288'
// ERROR: Unable to parse '42.3034229512808'
// ERROR: Unable to parse '970.57311049223563'
// ERROR: Unable to parse '531.33717716268131'
// ERROR: Unable to parse '587.82905693530529'
// ERROR: Unable to parse '562.25210175023039'
// ERROR: Unable to parse '600.7711019370571'
// ERROR: Unable to parse '299.46113717717174'
Imports System.Globalization
Imports System.IO
Imports System.Threading
Module Example15
Public Sub Main15()
' Create ten random doubles.
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US")
Dim numbers() As Double = GetRandomNumbers(10)
DisplayRandomNumbers(numbers)
' Persist the numbers as strings.
Dim sw As New StreamWriter("randoms.dat")
For ctr As Integer = 0 To numbers.Length - 1
sw.Write("{0:R}{1}", numbers(ctr), If(ctr < numbers.Length - 1, "|", ""))
Next
sw.Close()
' Read the persisted data.
Dim sr As New StreamReader("randoms.dat")
Dim numericData As String = sr.ReadToEnd()
sr.Close()
Dim numberStrings() As String = numericData.Split("|"c)
' Restore and display the data using the conventions of the en-US culture.
Console.WriteLine("Current Culture: {0}",
Thread.CurrentThread.CurrentCulture.DisplayName)
For Each numberStr In numberStrings
Dim restoredNumber As Double
If Double.TryParse(numberStr, restoredNumber) Then
Console.WriteLine(restoredNumber.ToString("R"))
Else
Console.WriteLine("ERROR: Unable to parse '{0}'", numberStr)
End If
Next
Console.WriteLine()
' Restore and display the data using the conventions of the fr-FR culture.
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("fr-FR")
Console.WriteLine("Current Culture: {0}",
Thread.CurrentThread.CurrentCulture.DisplayName)
For Each numberStr In numberStrings
Dim restoredNumber As Double
If Double.TryParse(numberStr, restoredNumber) Then
Console.WriteLine(restoredNumber.ToString("R"))
Else
Console.WriteLine("ERROR: Unable to parse '{0}'", numberStr)
End If
Next
End Sub
Private Function GetRandomNumbers(n As Integer) As Double()
Dim rnd As New Random()
Dim numbers(n - 1) As Double
For ctr As Integer = 0 To n - 1
numbers(ctr) = rnd.NextDouble * 1000
Next
Return numbers
End Function
Private Sub DisplayRandomNumbers(numbers As Double())
For ctr As Integer = 0 To numbers.Length - 1
Console.WriteLine(numbers(ctr).ToString("R"))
Next
Console.WriteLine()
End Sub
End Module
' The example displays output like the following:
' 487.0313743534644
' 674.12000879371533
' 498.72077885024288
' 42.3034229512808
' 970.57311049223563
' 531.33717716268131
' 587.82905693530529
' 562.25210175023039
' 600.7711019370571
' 299.46113717717174
'
' Current Culture: English (United States)
' 487.0313743534644
' 674.12000879371533
' 498.72077885024288
' 42.3034229512808
' 970.57311049223563
' 531.33717716268131
' 587.82905693530529
' 562.25210175023039
' 600.7711019370571
' 299.46113717717174
'
' Current Culture: French (France)
' ERROR: Unable to parse '487.0313743534644'
' ERROR: Unable to parse '674.12000879371533'
' ERROR: Unable to parse '498.72077885024288'
' ERROR: Unable to parse '42.3034229512808'
' ERROR: Unable to parse '970.57311049223563'
' ERROR: Unable to parse '531.33717716268131'
' ERROR: Unable to parse '587.82905693530529'
' ERROR: Unable to parse '562.25210175023039'
' ERROR: Unable to parse '600.7711019370571'
' ERROR: Unable to parse '299.46113717717174'
ãã®åé¡ãåé¿ããã«ã¯ã次ã®ããããã®ææ³ã䜿çšããŸãã
- ãŠãŒã¶ãŒã®ã«ã«ãã£ã«é¢ä¿ãªãåãã«ã¹ã¿ã æžåŒæå®æååã䜿çšããŠãæ°å€ã®æåå圢åŒãä¿åããŠè§£æããŸãã
- CultureInfo.InvariantCulture ããããã£ã«ãã£ãŠè¿ãããã€ã³ããªã¢ã³ã ã«ã«ãã£ã®æžåŒèšå®èŠåã䜿çšããŠãæ°å€ãæååãšããŠä¿åããŸãã
é貚å€ã®ã·ãªã¢ã«åã¯ç¹æ®ãªã±ãŒã¹ã§ãã é貚å€ã¯è¡šçŸãããé貚ã®åäœã«äŸåãããããç¬ç«ããæ°å€ãšããŠæ±ãã®ã¯ã»ãšãã©æå³ããããŸããã ãã ããé貚èšå·ãå«ãæžåŒèšå®ãããæååãšããŠé貚å€ãä¿åããå Žåãæ¬¡ã®äŸã«ç€ºãããã«ãæ¢å®ã®ã«ã«ãã£ã§å¥ã®é貚èšå·ã䜿çšãããŠããã·ã¹ãã ã§ã¯éã·ãªã¢ã«åã§ããŸããã
using System;
using System.Globalization;
using System.IO;
using System.Threading;
public class Example1
{
public static void Main1()
{
// Display the currency value.
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
Decimal value = 16039.47m;
Console.WriteLine($"Current Culture: {CultureInfo.CurrentCulture.DisplayName}");
Console.WriteLine($"Currency Value: {value:C2}");
// Persist the currency value as a string.
StreamWriter sw = new StreamWriter("currency.dat");
sw.Write(value.ToString("C2"));
sw.Close();
// Read the persisted data using the current culture.
StreamReader sr = new StreamReader("currency.dat");
string currencyData = sr.ReadToEnd();
sr.Close();
// Restore and display the data using the conventions of the current culture.
Decimal restoredValue;
if (Decimal.TryParse(currencyData, out restoredValue))
Console.WriteLine(restoredValue.ToString("C2"));
else
Console.WriteLine($"ERROR: Unable to parse '{currencyData}'");
Console.WriteLine();
// Restore and display the data using the conventions of the en-GB culture.
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-GB");
Console.WriteLine($"Current Culture: {Thread.CurrentThread.CurrentCulture.DisplayName}");
if (Decimal.TryParse(currencyData, NumberStyles.Currency, null, out restoredValue))
Console.WriteLine(restoredValue.ToString("C2"));
else
Console.WriteLine($"ERROR: Unable to parse '{currencyData}'");
Console.WriteLine();
}
}
// The example displays output like the following:
// Current Culture: English (United States)
// Currency Value: $16,039.47
// ERROR: Unable to parse '$16,039.47'
//
// Current Culture: English (United Kingdom)
// ERROR: Unable to parse '$16,039.47'
Imports System.Globalization
Imports System.IO
Imports System.Threading
Module Example1
Public Sub Main1()
' Display the currency value.
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US")
Dim value As Decimal = 16039.47D
Console.WriteLine("Current Culture: {0}", CultureInfo.CurrentCulture.DisplayName)
Console.WriteLine("Currency Value: {0:C2}", value)
' Persist the currency value as a string.
Dim sw As New StreamWriter("currency.dat")
sw.Write(value.ToString("C2"))
sw.Close()
' Read the persisted data using the current culture.
Dim sr As New StreamReader("currency.dat")
Dim currencyData As String = sr.ReadToEnd()
sr.Close()
' Restore and display the data using the conventions of the current culture.
Dim restoredValue As Decimal
If Decimal.TryParse(currencyData, restoredValue) Then
Console.WriteLine(restoredValue.ToString("C2"))
Else
Console.WriteLine("ERROR: Unable to parse '{0}'", currencyData)
End If
Console.WriteLine()
' Restore and display the data using the conventions of the en-GB culture.
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-GB")
Console.WriteLine("Current Culture: {0}",
Thread.CurrentThread.CurrentCulture.DisplayName)
If Decimal.TryParse(currencyData, NumberStyles.Currency, Nothing, restoredValue) Then
Console.WriteLine(restoredValue.ToString("C2"))
Else
Console.WriteLine("ERROR: Unable to parse '{0}'", currencyData)
End If
Console.WriteLine()
End Sub
End Module
' The example displays output like the following:
' Current Culture: English (United States)
' Currency Value: $16,039.47
' ERROR: Unable to parse '$16,039.47'
'
' Current Culture: English (United Kingdom)
' ERROR: Unable to parse '$16,039.47'
代ããã«ãçŸåšã®ã«ã«ãã£ãšã¯å¥ã«å€ãšãã®é貚èšå·ãéã·ãªã¢ã«åã§ããããã«ãã«ã«ãã£ã®ååãªã©ã®ã«ã«ãã£æ
å ±ãšå
±ã«æ°å€ãã·ãªã¢ã«åããå¿
èŠããããŸãã æ¬¡ã®äŸã§ã¯ãCurrencyValueå€ãšå€ãå±ããã«ã«ãã£ã®ååãšãã 2 ã€ã®ã¡ã³ããŒãæã€Decimalæ§é äœãå®çŸ©ããŸãã
using System;
using System.Globalization;
using System.Text.Json;
using System.Threading;
public class Example2
{
public static void Main2()
{
// Display the currency value.
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
Decimal value = 16039.47m;
Console.WriteLine($"Current Culture: {CultureInfo.CurrentCulture.DisplayName}");
Console.WriteLine($"Currency Value: {value:C2}");
// Serialize the currency data.
CurrencyValue data = new()
{
Amount = value,
CultureName = CultureInfo.CurrentCulture.Name
};
string serialized = JsonSerializer.Serialize(data);
Console.WriteLine();
// Change the current culture.
CultureInfo.CurrentCulture = CultureInfo.CreateSpecificCulture("en-GB");
Console.WriteLine($"Current Culture: {CultureInfo.CurrentCulture.DisplayName}");
// Deserialize the data.
CurrencyValue restoredData = JsonSerializer.Deserialize<CurrencyValue>(serialized);
// Display the round-tripped value.
CultureInfo culture = CultureInfo.CreateSpecificCulture(restoredData.CultureName);
Console.WriteLine($"Currency Value: {restoredData.Amount.ToString("C2", culture)}");
}
}
internal struct CurrencyValue
{
public decimal Amount { get; set; }
public string CultureName { get; set; }
}
// The example displays the following output:
// Current Culture: English (United States)
// Currency Value: $16,039.47
//
// Current Culture: English (United Kingdom)
// Currency Value: $16,039.47
Imports System.Globalization
Imports System.Text.Json
Imports System.Threading
Friend Structure CurrencyValue
Public Property Amount As Decimal
Public Property CultureName As String
End Structure
Module Example2
Public Sub Main2()
' Display the currency value.
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US")
Dim value As Decimal = 16039.47D
Console.WriteLine("Current Culture: {0}", CultureInfo.CurrentCulture.DisplayName)
Console.WriteLine("Currency Value: {0:C2}", value)
' Serialize the currency data.
Dim data As New CurrencyValue With {
.Amount = value,
.CultureName = CultureInfo.CurrentCulture.Name
}
Dim serialized As String = JsonSerializer.Serialize(data)
Console.WriteLine()
' Change the current culture.
CultureInfo.CurrentCulture = CultureInfo.CreateSpecificCulture("en-GB")
Console.WriteLine("Current Culture: {0}", CultureInfo.CurrentCulture.DisplayName)
' Deserialize the data.
Dim restoredData As CurrencyValue = JsonSerializer.Deserialize(Of CurrencyValue)(serialized)
' Display the round-tripped value.
Dim culture As CultureInfo = CultureInfo.CreateSpecificCulture(restoredData.CultureName)
Console.WriteLine("Currency Value: {0}", restoredData.Amount.ToString("C2", culture))
End Sub
End Module
' The example displays the following output:
' Current Culture: English (United States)
' Currency Value: $16,039.47
'
' Current Culture: English (United Kingdom)
' Currency Value: $16,039.47
ã«ã«ãã£åºæã®èšå®ã䜿çšãã
.NET ã§ã¯ã CultureInfo ã¯ã©ã¹ã¯ç¹å®ã®ã«ã«ãã£ãŸãã¯ãªãŒãžã§ã³ã衚ããŸãã ãã®ããããã£ã®äžéšã¯ãã«ã«ãã£ã®äžéšã®åŽé¢ã«é¢ããç¹å®ã®æ å ±ãæäŸãããªããžã§ã¯ããè¿ããŸãã
CultureInfo.CompareInfo ããããã£ã¯ãã«ã«ãã£ãæååãæ¯èŒããã³é åºä»ãããæ¹æ³ã«é¢ããæ å ±ãå«ãCompareInfo ãªããžã§ã¯ããè¿ããŸãã
CultureInfo.DateTimeFormat ããããã£ã¯ãæ¥ä»ãšæå»ã®ããŒã¿ã®æžåŒèšå®ã«äœ¿çšãããã«ã«ãã£åºæã®æ å ±ãæäŸããDateTimeFormatInfo ãªããžã§ã¯ããè¿ããŸãã
CultureInfo.NumberFormat ããããã£ã¯ãæ°å€ããŒã¿ã®æžåŒèšå®ã«äœ¿çšãããã«ã«ãã£åºæã®æ å ±ãæäŸããNumberFormatInfo ãªããžã§ã¯ããè¿ããŸãã
CultureInfo.TextInfo ããããã£ã¯ãã«ã«ãã£ã®æåã·ã¹ãã ã«é¢ããæ å ±ãæäŸããTextInfo ãªããžã§ã¯ããè¿ããŸãã
äžè¬ã«ãç¹å®ã® CultureInfo ããããã£ãšãã®é¢é£ãªããžã§ã¯ãã®å€ã«é¢ããæ³å®ã¯äžåè¡ããªãã§ãã ããã 代ããã«ã次ã®çç±ã«ãããã«ã«ãã£åºæã®ããŒã¿ã倿Žã®å¯Ÿè±¡ãšããŠè¡šç€ºããå¿ èŠããããŸãã
åã ã®ããããã£å€ã¯ãããŒã¿ã®ä¿®æ£ãããŒã¿ã®æ¹åããŸãã¯ã«ã«ãã£åºæã®èŠåã®å€æŽã«äŒŽããæéã®çµéãšãšãã«å€æŽãããããšããããŸãã
åã ã®ããããã£å€ã¯ã.NET ãŸãã¯ãªãã¬ãŒãã£ã³ã° ã·ã¹ãã ã®ããŒãžã§ã³ã«ãã£ãŠç°ãªãå ŽåããããŸãã
.NET ã§ã¯ã眮æã«ã«ãã£ããµããŒããããŠããŸãã ããã«ãããæ¢åã®æšæºã«ã«ãã£ãè£å®ããããæ¢åã®æšæºã«ã«ãã£ãå®å šã«çœ®ãæããæ°ããã«ã¹ã¿ã ã«ã«ãã£ãå®çŸ©ã§ããŸãã
Windows ã·ã¹ãã ã§ã¯ããŠãŒã¶ãŒã¯ã³ã³ãããŒã« ããã«ã® [å°åãšèšèª ] ã¢ããªã䜿çšããŠãã«ã«ãã£åºæã®èšå®ãã«ã¹ã¿ãã€ãºã§ããŸãã CultureInfo ãªããžã§ã¯ããã€ã³ã¹ã¿ã³ã¹åãããšãã«ãCultureInfo(String, Boolean) ã³ã³ã¹ãã©ã¯ã¿ãŒãåŒã³åºãããšã«ãã£ãŠããããã®ãŠãŒã¶ãŒã®ã«ã¹ã¿ãã€ãºãåæ ãããŠãããã©ããã倿ã§ããŸãã éåžžããšã³ã ãŠãŒã¶ãŒ ã¢ããªã®å Žåã¯ããŠãŒã¶ãŒãæåŸ ãã圢åŒã§ããŒã¿ã衚瀺ãããããã«ããŠãŒã¶ãŒèšå®ãå°éããå¿ èŠããããŸãã