ホームページ  >  記事  >  バックエンド開発  >  C#.net で一般的に使用される小さな関数とメソッドのコレクション

C#.net で一般的に使用される小さな関数とメソッドのコレクション

巴扎黑
巴扎黑オリジナル
2016-12-20 17:02:431073ブラウズ

1. DateTime 数値型 System.DateTime currentTime=new System.DateTime(); 1.1 現在の年、月、日、時、分、秒を取得します。 currentTime=System.DateTime.Now; 1.2 現在の年を取得します。 . Year; 1.3 現在の月を取得します int Month = currentTime.Month; 1.4 現在の日付を取得します int day = currentTime.Day; 1.6 現在の分を取得します int minutes = currentTime.Minute ; 1.7 現在の秒を取得します int Second = currentTime.Second; 1.8 現在のミリ秒を取得します (変数は中国語で利用可能です) 2. Int32.Parse (変数) Int32.Parse ("定数") 文字型3. Variable.ToString() 文字型 文字列に変換 12345.ToString("n");//12,345.0012345.ToString("C");//生成 ¥12,345.0012345.ToString( "e");// 1.234500e+00412345.ToString(" f4") を生成;// 12345.000012345.ToString("x") を生成;// 3039 (16 進数) 12345.ToString("p") を生成;// 1,234,500.00%4 を生成します。変数 .Length デジタル型は文字列長を受け取ります。例: string str="China";int Len = str.Length;//Len はカスタム変数、str は文字列の変数名です。テスト済み 5. System.Text.Encoding.Default.GetBytes (変数) word コードを次のようなビット コードに変換します。 byte[] bytStr = System.Text.Encoding.Default.GetBytes(str); 次に、ビット長を取得できます。 : len = bytStr.Length; 6. System.Text.StringBuilder("") 文字列の追加 (+ 記号も同じですか?) 例: System.Text.StringBuilder sb = new System.Text.StringBuilder("") ;sb.Append("China");sb.Append("People" );sb.Append("Republic");7. Variable.Substring(パラメータ 1, パラメータ 2); パラメータ 1 は文字列のインターセプト部分です。左からの開始桁、パラメータ 2 はインターセプトする桁数です。例: string s1 = str.Substring(0,2); 8. String user_IP=Request.ServerVariables["REMOTE_ADDR"].ToString(); リモート ユーザーの IP アドレスを取得します。プロキシ サーバー: if(Request.ServerVariables["HTTP_VIA"]!=null){stringuser_IP=Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();}else{stringuser_IP=Request.ServerVariables["REMOTE_ADDR"].ToString() ;}10 、Session["variable"]; セッション値にアクセスします。例: Session["username"]="Bush" 値: Object objName=Session["username"];String strName=objName.ToString( ) ; Clear: Session.RemoveAll(); 11. String str=Request.QueryString["Variable"]; 変数を転送するには、ハイパーリンクを使用します。たとえば、任意のページにハイパーリンクを作成します: をクリックして、Edit.aspx ページの値を取得します: String str=Request.QueryString["fdid" ];12 , DOC object.CreateElement("新しいノード名"); XML 文書の新しいノードを作成します。 13. 親ノード.AppendChild (子ノード); 新しく作成した子ノードを XML 文書の親ノードに追加します。 . 親ノード.RemoveChild (ノード) ;ノード 15 を削除、ResponseResponse.Write("string");Response.Write(variable);ページに出力します。 Response.Redirect("URL アドレス"); URL で指定されたページにジャンプします。 16. char.IsWhiteSpce (文字列変数、桁数) - 指定された位置が null 文字であるかどうかを確認するための論理型。 ="中国人" ;Response.Write(char.IsWhiteSpace(str,2)); //結果は次のようになります。最初の文字は 0、3 番目の文字は 2 です。 17. char.IsPunctuation('Character') -- 文字が句読点であるかどうかの論理チェック。例: Response.Write(char.IsPunctuation('A'));//Return: False18, (int)'Character ' 数値に変換し、コード ポイントを調べ、一重引用符に注意してください。例: Response.Write((int)'中');//結果は中国語の文字コード: 2001319 です。(char) コードは数値を文字に変換し、コードで表される文字をチェックします。例: Response.Write((char)22269);//文字「国」を返します。 20. Trim() は文字列の前後のスペースをクリアします。 21. 文字列変数.Replace("substring", "replace with") 文字列置換: string str="China";str=str.Replace("国") ,"central");//各国文字を中心文字に置き換えます Response.Write(str);//出力結果は「central」になります 別の例: (これは非常に実用的です) string str="これは <");//左の山かっこを に、< を スクリプトです」と表示されます。 、