.NETでの文字列処理
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
検索
|
最終更新
|
ヘルプ
|
ログイン
] [
Twitter
]
開始行:
→.NETプログラミング
#contents
*日付型のToString()について [#l3bb2951]
-このように↓した場合、返ってくる文字列はコントロールパネ...
string strNow = DateTime.Now.ToString();
-特定の表示形式に依存する場合はこのように↓きちんと形式指...
string strNow = DateTime.Now.ToString("yyyy/MM/dd HH:mm:...
*文字列の前につける「@」について [#f86ead24]
-@をつけた文字列リテラルはエスケープシーケンスを処理しない
-Stringのヘルプより
@ と二重引用符で囲む場合は、先頭に @ を付け、リテラル文...
@"good morning" // a string literal
@ と二重引用符を使用した場合の利点は、エスケープ シーケ...
たとえば、完全修飾ファイル名が書きやすくなることです。
@"c:\Docs\Source\a.txt" // rather than "c:\\Docs\\Sourc...
@ と二重引用符で囲まれた文字列中に二重引用符を含めるには...
@"""Ahoy!"" cried the captain." // "Ahoy!" cried the cap...
また、参照 (/reference (メタデータのインポート)) 識別子...
@ 記号を使用します。詳細については、「2.4.2 識別子」を参...
*Strings UNDOCUMENTED [#n398678b]
-http://www.codeproject.com/dotnet/strings.asp
*英数字文字列の1バイト⇔2バイト文字変換をしたい [#g5bbdc...
-C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\Microsoft.V...
-using Microsoft.VisualBasic;をする(この機能はVisual Basi...
String s = "(30)";
//ci = new CultureInfo("ja-JP"); でもよい
//ci = System.Threading.Thread.CurrentThread.CurrentCult...
//ci = Application.CurrentCulture; でもよい
CultureInfo ci = System.Threading.Thread.CurrentThread.C...
//本当はLCIDの代わりに0を渡せばカレントLCIDを内部で取得...
String s2 = Strings.StrConv(s, VbStrConv.Narrow, ci.LCID...
*文字列のバイト長を取得したい [#v09419bd]
-System.Text.Encoding.GetByteCount() を使う
-応用例
//指定バイト数に収まるように文字列を切り詰める
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;
}
*文字列フォーマット(String.Format())のサンプル集 [#x8132b...
-文字列を16進表記に変換する例
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}" →カンマなし
-小数点桁数
--NとかFの次に桁数を付ける。例えばカンマ付数字の小数点を2...
"{0:N2}"
となる
-[[数値のフォーマット:http://www.atmarkit.co.jp/fdotnet/d...
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値のフォーマット
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); // %
-[[日付のフォーマット:http://www.atmarkit.co.jp/fdotnet/d...
DateTime dt = DateTime.Now;
String strDate="";
strDate = dt.ToString("MM/dd/yyyy"); // 07/21/2007
strDate = dt.ToString("dddd, dd MMMM yyyy"); //Saturda...
strDate = dt.ToString("dddd, dd MMMM yyyy HH:mm"); // Sa...
strDate = dt.ToString("dddd, dd MMMM yyyy hh:mm tt"); //...
strDate = dt.ToString("dddd, dd MMMM yyyy H:mm"); // Sat...
strDate = dt.ToString("dddd, dd MMMM yyyy h:mm tt"); // ...
strDate = dt.ToString("dddd, dd MMMM yyyy HH:mm:ss"); //...
strDate = dt.ToString("MM/dd/yyyy HH:mm"); // 07/21/2007...
strDate = dt.ToString("MM/dd/yyyy hh:mm tt"); // 07/21/2...
strDate = dt.ToString("MM/dd/yyyy H:mm"); // 07/21/2007 ...
strDate = dt.ToString("MM/dd/yyyy h:mm tt"); // 07/21/20...
strDate = dt.ToString("MM/dd/yyyy HH:mm:ss"); // 07/21/2...
strDate = dt.ToString("MMMM dd"); // July 21
strDate = dt.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss.fff...
strDate = dt.ToString("ddd, dd MMM yyyy HH':'mm':'ss 'GM...
strDate = dt.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss"); ...
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'");...
strDate = dt.ToString("dddd, dd MMMM yyyy HH:mm:ss"); //...
strDate = dt.ToString("yyyy MMMM"); // 2007 July
終了行:
→.NETプログラミング
#contents
*日付型のToString()について [#l3bb2951]
-このように↓した場合、返ってくる文字列はコントロールパネ...
string strNow = DateTime.Now.ToString();
-特定の表示形式に依存する場合はこのように↓きちんと形式指...
string strNow = DateTime.Now.ToString("yyyy/MM/dd HH:mm:...
*文字列の前につける「@」について [#f86ead24]
-@をつけた文字列リテラルはエスケープシーケンスを処理しない
-Stringのヘルプより
@ と二重引用符で囲む場合は、先頭に @ を付け、リテラル文...
@"good morning" // a string literal
@ と二重引用符を使用した場合の利点は、エスケープ シーケ...
たとえば、完全修飾ファイル名が書きやすくなることです。
@"c:\Docs\Source\a.txt" // rather than "c:\\Docs\\Sourc...
@ と二重引用符で囲まれた文字列中に二重引用符を含めるには...
@"""Ahoy!"" cried the captain." // "Ahoy!" cried the cap...
また、参照 (/reference (メタデータのインポート)) 識別子...
@ 記号を使用します。詳細については、「2.4.2 識別子」を参...
*Strings UNDOCUMENTED [#n398678b]
-http://www.codeproject.com/dotnet/strings.asp
*英数字文字列の1バイト⇔2バイト文字変換をしたい [#g5bbdc...
-C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\Microsoft.V...
-using Microsoft.VisualBasic;をする(この機能はVisual Basi...
String s = "(30)";
//ci = new CultureInfo("ja-JP"); でもよい
//ci = System.Threading.Thread.CurrentThread.CurrentCult...
//ci = Application.CurrentCulture; でもよい
CultureInfo ci = System.Threading.Thread.CurrentThread.C...
//本当はLCIDの代わりに0を渡せばカレントLCIDを内部で取得...
String s2 = Strings.StrConv(s, VbStrConv.Narrow, ci.LCID...
*文字列のバイト長を取得したい [#v09419bd]
-System.Text.Encoding.GetByteCount() を使う
-応用例
//指定バイト数に収まるように文字列を切り詰める
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;
}
*文字列フォーマット(String.Format())のサンプル集 [#x8132b...
-文字列を16進表記に変換する例
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}" →カンマなし
-小数点桁数
--NとかFの次に桁数を付ける。例えばカンマ付数字の小数点を2...
"{0:N2}"
となる
-[[数値のフォーマット:http://www.atmarkit.co.jp/fdotnet/d...
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値のフォーマット
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); // %
-[[日付のフォーマット:http://www.atmarkit.co.jp/fdotnet/d...
DateTime dt = DateTime.Now;
String strDate="";
strDate = dt.ToString("MM/dd/yyyy"); // 07/21/2007
strDate = dt.ToString("dddd, dd MMMM yyyy"); //Saturda...
strDate = dt.ToString("dddd, dd MMMM yyyy HH:mm"); // Sa...
strDate = dt.ToString("dddd, dd MMMM yyyy hh:mm tt"); //...
strDate = dt.ToString("dddd, dd MMMM yyyy H:mm"); // Sat...
strDate = dt.ToString("dddd, dd MMMM yyyy h:mm tt"); // ...
strDate = dt.ToString("dddd, dd MMMM yyyy HH:mm:ss"); //...
strDate = dt.ToString("MM/dd/yyyy HH:mm"); // 07/21/2007...
strDate = dt.ToString("MM/dd/yyyy hh:mm tt"); // 07/21/2...
strDate = dt.ToString("MM/dd/yyyy H:mm"); // 07/21/2007 ...
strDate = dt.ToString("MM/dd/yyyy h:mm tt"); // 07/21/20...
strDate = dt.ToString("MM/dd/yyyy HH:mm:ss"); // 07/21/2...
strDate = dt.ToString("MMMM dd"); // July 21
strDate = dt.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss.fff...
strDate = dt.ToString("ddd, dd MMM yyyy HH':'mm':'ss 'GM...
strDate = dt.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss"); ...
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'");...
strDate = dt.ToString("dddd, dd MMMM yyyy HH:mm:ss"); //...
strDate = dt.ToString("yyyy MMMM"); // 2007 July
ページ名: