string strNow = DateTime.Now.ToString();
string strNow = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
@ と二重引用符で囲む場合は、先頭に @ を付け、リテラル文字列の前後に二重引用符を付けます。次に例を示します。 @"good morning" // a string literal @ と二重引用符を使用した場合の利点は、エスケープ シーケンスが処理されないため、 たとえば、完全修飾ファイル名が書きやすくなることです。 @"c:\Docs\Source\a.txt" // rather than "c:\\Docs\\Source\\a.txt" @ と二重引用符で囲まれた文字列中に二重引用符を含めるには、二重引用符を二重にします。 @"""Ahoy!"" cried the captain." // "Ahoy!" cried the captain. また、参照 (/reference (メタデータのインポート)) 識別子が C# キーワードである場合も、 @ 記号を使用します。詳細については、「2.4.2 識別子」を参照してください。
String s = "(30)"; //ci = new CultureInfo("ja-JP"); でもよい //ci = System.Threading.Thread.CurrentThread.CurrentCulture; でもよい //ci = Application.CurrentCulture; でもよい CultureInfo ci = System.Threading.Thread.CurrentThread.CurrentCulture; //本当はLCIDの代わりに0を渡せばカレントLCIDを内部で取得してくれるが String s2 = Strings.StrConv(s, VbStrConv.Narrow, ci.LCID);
//指定バイト数に収まるように文字列を切り詰める String CutByByte(String s, int blen) { Encoding enc = Encoding.GetEncoding(932); int n= enc.GetByteCount(s); while(n > blen) { s = s.Substring(0, s.Length-1); n = enc.GetByteCount(s); } return s; }
static void Main(string[] args) { String s = "abcde"; String sHex = ""; for(int i = 0; i < s.Length; i++ ) { Byte b = Convert.ToByte(s[i]); sHex += String.Format("{0:X2}", b); } Console.WriteLine("s ={0}, sHex={1}", s, sHex ); Console.ReadLine(); }
"{0:N}" → カンマ付 "{0:F}" →カンマなし
"{0:N2}"となる
using System; class TestIntegerCustomFormats { static void Main( ) { int i = 123; Console.WriteLine("{0:#0}", i); // 123 Console.WriteLine("{0:#0;(#0)}", i); // 123 Console.WriteLine("{0:#0;(#0);<zero>}", i); // 123 Console.WriteLine("{0:#%}", i); // 12300% i = -123; Console.WriteLine("{0:#0}", i); // -123 Console.WriteLine("{0:#0;(#0)}", i); // (123) Console.WriteLine("{0:#0;(#0);<zero>}", i); // (123) Console.WriteLine("{0:#%}", i); // -12300% i = 0; Console.WriteLine("{0:#0}", i); // 0 Console.WriteLine("{0:#0;(#0)}", i); // 0 Console.WriteLine("{0:#0;(#0);<zero>}", i); // <zero> Console.WriteLine("{0:#%}", i); // % } }
double d = 1.23; Console.WriteLine("{0:#.000E+00}", d); // 1.230E+00 Console.WriteLine( "{0:#.000E+00;(#.000E+00)}", d); // 1.230E+00 Console.WriteLine( "{0:#.000E+00;(#.000E+00);<zero>}", d); // 1.230E+00 Console.WriteLine("{0:#%}", d); // 123% d = -1.23; Console.WriteLine("{0:#.000E+00}", d); // -1.230E+00 Console.WriteLine( "{0:#.000E+00;(#.000E+00)}", d); // (1.230E+00) Console.WriteLine( "{0:#.000E+00;(#.000E+00);<zero>}", d); // (1.230E+00) Console.WriteLine("{0:#%}", d); // -123% d = 0; Console.WriteLine("{0:#.000E+00}", d); // 0.000E-01 Console.WriteLine( "{0:#.000E+00;(#.000E+00)}", d); // 0.000E-01 Console.WriteLine( "{0:#.000E+00;(#.000E+00);<zero>}", d); // <zero> Console.WriteLine("{0:#%}", d); // %
DateTime dt = DateTime.Now; String strDate=""; strDate = dt.ToString("MM/dd/yyyy"); // 07/21/2007 strDate = dt.ToString("dddd, dd MMMM yyyy"); //Saturday, 21 July 2007 strDate = dt.ToString("dddd, dd MMMM yyyy HH:mm"); // Saturday, 21 July 2007 14:58 strDate = dt.ToString("dddd, dd MMMM yyyy hh:mm tt"); // Saturday, 21 July 2007 03:00 PM strDate = dt.ToString("dddd, dd MMMM yyyy H:mm"); // Saturday, 21 July 2007 5:01 strDate = dt.ToString("dddd, dd MMMM yyyy h:mm tt"); // Saturday, 21 July 2007 3:03 PM strDate = dt.ToString("dddd, dd MMMM yyyy HH:mm:ss"); // Saturday, 21 July 2007 15:04:10 strDate = dt.ToString("MM/dd/yyyy HH:mm"); // 07/21/2007 15:05 strDate = dt.ToString("MM/dd/yyyy hh:mm tt"); // 07/21/2007 03:06 PM strDate = dt.ToString("MM/dd/yyyy H:mm"); // 07/21/2007 15:07 strDate = dt.ToString("MM/dd/yyyy h:mm tt"); // 07/21/2007 3:07 PM strDate = dt.ToString("MM/dd/yyyy HH:mm:ss"); // 07/21/2007 15:09:29 strDate = dt.ToString("MMMM dd"); // July 21 strDate = dt.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffffK"); // 2007-07-21T15:11:19.1250000+05:30 strDate = dt.ToString("ddd, dd MMM yyyy HH':'mm':'ss 'GMT'"); // Sat, 21 Jul 2007 15:12:16 GMT strDate = dt.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss"); // 2007-07-21T15:12:57 strDate = dt.ToString("HH:mm"); // 15:14 strDate = dt.ToString("hh:mm tt"); // 03:14 PM strDate = dt.ToString("H:mm"); // 5:15 strDate = dt.ToString("h:mm tt"); // 3:16 PM strDate = dt.ToString("HH:mm:ss"); // 15:16:29 strDate = dt.ToString("yyyy'-'MM'-'dd HH':'mm':'ss'Z'"); // 2007-07-21 15:17:20Z strDate = dt.ToString("dddd, dd MMMM yyyy HH:mm:ss"); // Saturday, 21 July 2007 15:17:58 strDate = dt.ToString("yyyy MMMM"); // 2007 July