• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            C++ Programmer's Cookbook

            {C++ 基礎} {C++ 高級} {C#界面,C++核心算法} {設計模式} {C#基礎}

            .net下漢字字符串,一般字符串,時間日期的常用方法

            // 獲得漢字的區位碼
               byte []?array? = ? new ? byte [ 2 ];
              array?
            = ?System.Text.Encoding.Default.GetBytes( " " );?

            int ?i1? = ?( short )(array[ 0 ]? - ?\\\ 0 \);
              
            int ?i2? = ?( short )(array[ 1 ]? - ?\\\ 0 \);

            // unicode解碼方式下的漢字碼
              array? = ?System.Text.Encoding.Unicode.GetBytes( " " );
              i1?
            = ?( short )(array[ 0 ]? - ?\\\ 0 \);
              i2?
            = ?( short )(array[ 1 ]? - ?\\\ 0 \);

            // unicode反解碼為漢字
               string ?str? = ? " 4a55 " ;
              
            string ?s1? = ?str.Substring( 0 , 2 );
              
            string ?s2? = ?str.Substring( 2 , 2 );

            int ?t1? = ?Convert.ToInt32(s1, 16 );
              
            int ?t2? = ?Convert.ToInt32(s2, 16 );

            array[
            0 ]? = ?( byte )t1;
              array[
            1 ]? = ?( byte )t2;

            string ?s? = ?System.Text.Encoding.Unicode.GetString(array);

            // default方式反解碼為漢字
              array[ 0 ]? = ?( byte ) 196 ;
              array[
            1 ]? = ?( byte ) 207 ;
              s?
            = ?System.Text.Encoding.Default.GetString(array);

            // 取字符串長度
              s? = ? " iam方槍槍 " ;
              
            int ?len? = ?s.Length; // will?output?as?6
               byte []?sarr? = ?System.Text.Encoding.Default.GetBytes(s);
              len?
            = ?sarr.Length; // will?output?as?3+3*2=9

            // 字符串相加
              System.Text.StringBuilder?sb? = ? new ?System.Text.StringBuilder( "" );
              sb.Append(
            " i? " );
              sb.Append(
            " am? " );
              sb.Append(
            " 方槍槍 " );?

            1 、DateTime?數字型?
            System.DateTime?currentTime
            = new ?System.DateTime();?
            1.1 ?取當前年月日時分秒?
            currentTime
            = System.DateTime.Now;?
            1.2 ?取當前年?
            int ?年 = currentTime.Year;?
            1.3 ?取當前月?
            int ?月 = currentTime.Month;?
            1.4 ?取當前日?
            int ?日 = currentTime.Day;?
            1.5 ?取當前時?
            int ?時 = currentTime.Hour;?
            1.6 ?取當前分?
            int ?分 = currentTime.Minute;?
            1.7 ?取當前秒?
            int ?秒 = currentTime.Second;?
            1.8 ?取當前毫秒?
            int ?毫秒 = currentTime.Millisecond;?
            (變量可用中文)?

            2 、Int32.Parse(變量)?Int32.Parse( " 常量 " )?
            字符型轉換?轉為32位數字型?

            3 、?變量.ToString()?
            字符型轉換?轉為字符串?
            12345 .ToString( " n " );? // 生成?12,345.00?
            12345 .ToString( " C " );? // 生成?¥12,345.00?
            12345 .ToString( " e " );? // 生成?1.234500e+004?
            12345 .ToString( " f4 " );? // 生成?12345.0000?
            12345 .ToString( " x " );? // 生成?3039?(16進制)?
            12345 .ToString( " p " );? // 生成?1,234,500.00%?


            4 、變量.Length?數字型?
            取字串長度:?
            如:?
            string ?str = " 中國 " ;?
            int ?Len? = ?str.Length?;? // Len是自定義變量,?str是求測的字串的變量名?

            5 、System.Text.Encoding.Default.GetBytes(變量)?
            字碼轉換?轉為比特碼?
            如:
            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(
            " 中華 " );?
            sb.Append(
            " 人民 " );?
            sb.Append(
            " 共和國 " );?

            7 、變量.Substring(參數1,參數2);?
            截取字串的一部分,參數1為左起始位數,參數2為截取幾位。?
            如:
            string ?s1? = ?str.Substring( 0 , 2 );?

            8 、String?user_IP = Request.ServerVariables[ " REMOTE_ADDR " ].ToString();?
            取遠程用戶IP地址?

            9 、穿過代理服務器取遠程用戶真實IP地址:?
            if (Request.ServerVariables[ " HTTP_VIA " ] != null ) {?
            string ?user_IP = Request.ServerVariables[ " HTTP_X_FORWARDED_FOR " ].ToString();?
            }
            else {?
            string ?user_IP = Request.ServerVariables[ " REMOTE_ADDR " ].ToString();?
            }
            ?

            10 、?Session[ " 變量 " ];?
            存取Session值;?
            如,賦值:?Session[
            " username " ] = " 小布什 " ;?

            取值:?Object?objName
            = Session[ " username " ];?
            String?strName
            = objName.ToString();?
            清空:?Session.RemoveAll();?

            11 、String?str = Request.QueryString[ " 變量 " ];?
            用超鏈接傳送變量。?
            如在任一頁中建超鏈接:
            < a?href = Edit.aspx ? fbid = 23 > 點擊 </ a > ?
            在Edit.aspx頁中取值:String?str
            = Request.QueryString[ " fdid " ];?

            12 、DOC對象.CreateElement( " 新建節點名 " );?
            創建XML文檔新節點?

            13 、父節點.AppendChild(子節點);?
            將新建的子節點加到XML文檔父節點下?

            14 、?父節點.RemoveChild(節點);?
            刪除節點?

            15 、Response?
            Response.Write(
            " 字串 " );?
            Response.Write(變量);?
            向頁面輸出。?

            Response.Redirect(
            " URL地址 " );?
            跳轉到URL指定的頁面?

            16 char .IsWhiteSpce(字串變量,位數)——邏輯型?
            查指定位置是否空字符;?
            如:?
            string ?str = " 中國?人民 " ;?
            Response.Write(
            char .IsWhiteSpace(str, 2 ));? // 結果為:True,?第一個字符是0位,2是第三個字符。?

            17 char .IsPunctuation( ' 字符 ' )? -- 邏輯型?
            查字符是否是標點符號?
            如:Response.Write(
            char .IsPunctuation( ' A ' ));? // 返回:False?

            18 、( int ) ' 字符 ' ?
            把字符轉為數字,查代碼點,注意是單引號。?
            如:?
            Response.Write((
            int ) ' ' );? // 結果為中字的代碼:20013?

            19 、( char )代碼?
            把數字轉為字符,查代碼代表的字符。?
            如:?
            Response.Write((
            char ) 22269 );? // 返回“國”字。?

            20 、?Trim()?
            清除字串前后空格?

            21 ?、字串變量.Replace( " 子字串 " , " 替換為 " )?
            字串替換?
            如:?
            string ?str = " 中國 " ;?
            str
            = str.Replace( " " , " " );? // 將國字換為央字?
            Response.Write(str);? // 輸出結果為“中央”?

            再如:(這個非常實用)?

            string ?str = " 這是<script>腳本 " ;?
            str
            = str.Replace( " < " , " <font><</font> " );? // 將左尖括號替換為<font>?與?<?與?</font>?(或換為<,但估計經XML存諸后,再提出仍會還原)?
            Response.Write(str);? // 顯示為:“這是<script>腳本”?

            如果不替換,
            < script > 將不顯示,如果是一段腳本,將運行;而替換后,腳本將不運行。?
            這段代碼的價值在于:你可以讓一個文本中的所有HTML標簽失效,全部顯示出來,保護你的具有交互性的站點。?
            具體實現:將你的表單提交按鈕腳本加上下面代碼:?
            string ?strSubmit = label1.Text;? // label1是你讓用戶提交數據的控件ID。?
            strSubmit = strSubmit.Replace( " < " , " <font><</font> " );?
            然后保存或輸出strSubmit。?
            用此方法還可以簡單實現UBB代碼。?

            22 、Math.Max(i,j)?
            取i與j中的最大值?
            如?
            int ?x = Math.Max( 5 , 10 );? // ?x將取值?10?

            23 、字串對比一般都用:? if (str1 == str2) {?} ?,?但還有別的方法:?

            (
            1 )、?
            string ?str1;?str2?
            // 語法:?str1.EndsWith(str2);?__檢測字串str1是否以字串str2結尾,返回布爾值.如:?
            if (str1.EndsWith(str2)) {?Response.Write( " 字串str1是以 " + str2 + " 結束的 " );?} ?

            (
            2 )、?
            // 語法:str1.Equals(str2);?__檢測字串str1是否與字串str2相等,返回布爾值,用法同上.?

            (
            3 )、?
            // 語法?Equals(str1,str2);?__檢測字串str1是否與字串str2相等,返回布爾值,用法同上.?

            24 、IndexOf()?、LastIndexOf()?
            查找字串中指定字符或字串首次(最后一次)出現的位置,返回索引值,如:?
            str1.IndexOf(
            " " );? // 查找“字”在str1中的索引值(位置)?
            str1.IndexOf( " 字串 " ); // 查找“字串”的第一個字符在str1中的索引值(位置)?
            str1.IndexOf( " 字串 " , 3 , 2 ); // 從str1第4個字符起,查找2個字符,查找“字串”的第一個字符在str1中的索引值(位置)?

            25 、Insert()?
            在字串中指定索引位插入指定字符。如:?
            str1.Insert(
            1 , " " );在str1的第二個字符處插入“字”,如果str1 = " 中國 " ,插入后為“中字國”;?

            26 、PadLeft()、PadRight()?
            在字串左(或右)加空格或指定char字符,使字串達到指定長度,如:?
            <% ?
            string ?str1 = " 中國人 " ;?
            str1
            = str1.PadLeft( 10 , ' 1 ' );? // 無第二參數為加空格?
            Response.Write(str1);? // 結果為“1111111中國人”?,?字串長為10?
            %> ?

            27 、Remove()?
            從指定位置開始刪除指定數的字符?
            字串對比一般都用:?
            if (str1 == str2) {?} ?,?但還有別的方法:?

            1 、?
            string ?str1;?str2?
            // 語法:?str1.EndsWith(str2);?__檢測字串str1是否以字串str2結尾,返回布爾值.如:?
            if (str1.EndsWith(str2)) {?Response.Write( " 字串str1是以 " + str2 + " 結束的 " );?} ?

            2 、?
            // 語法:str1.Equals(str2);?__檢測字串str1是否與字串str2相等,返回布爾值,用法同上.?

            3 、?
            // 語法?Equals(str1,str2);?__檢測字串str1是否與字串str2相等,返回布爾值,用法同上.?

            IndexOf()?
            查找字串中指定字符或字串首次出現的位置,返首索引值,如:?
            str1.IndexOf(
            " " );? // 查找“字”在str1中的索引值(位置)?
            str1.IndexOf( " 字串 " ); // 查找“字串”的第一個字符在str1中的索引值(位置)?
            str1.IndexOf( " 字串 " , 3 , 2 ); // 從str1第4個字符起,查找2個字符,查找“字串”的第一個字符在str1中的索引值(位置)?

            1.9 ?取中文日期顯示——年月日時分?
            string ?strY = currentTime.ToString( " f " );? // 不顯示秒?

            1.10 ?取中文日期顯示_年月?
            string ?strYM = currentTime.ToString( " y " );?

            1.11 ?取中文日期顯示_月日?
            string ?strMD = currentTime.ToString( " m " );?

            1.12 ?取當前年月日,格式為: 2003 - 9 - 23 ?
            string ?strYMD = currentTime.ToString( " d " );?

            1.13 ?取當前時分,格式為: 14 24 ?
            string ?strT = currentTime.ToString( " t " );?

            posted on 2006-07-24 09:00 夢在天涯 閱讀(1174) 評論(0)  編輯 收藏 引用 所屬分類: C#/.NET

            公告

            EMail:itech001#126.com

            導航

            統計

            • 隨筆 - 461
            • 文章 - 4
            • 評論 - 746
            • 引用 - 0

            常用鏈接

            隨筆分類

            隨筆檔案

            收藏夾

            Blogs

            c#(csharp)

            C++(cpp)

            Enlish

            Forums(bbs)

            My self

            Often go

            Useful Webs

            Xml/Uml/html

            搜索

            •  

            積分與排名

            • 積分 - 1804663
            • 排名 - 5

            最新評論

            閱讀排行榜

            久久人人妻人人爽人人爽| 久久综合久久久| 99精品国产免费久久久久久下载| 99久久精品国产一区二区| 93精91精品国产综合久久香蕉| 51久久夜色精品国产| 香蕉久久AⅤ一区二区三区| 亚洲欧美一区二区三区久久| 中文无码久久精品| 18岁日韩内射颜射午夜久久成人| 狠狠色丁香久久婷婷综合图片| 人妻少妇久久中文字幕| 久久久久这里只有精品| 中文国产成人精品久久不卡| 国产精品久久久久久久久鸭| 久久综合伊人77777| 亚洲国产精品婷婷久久| 精品综合久久久久久888蜜芽| 日韩影院久久| 无码国内精品久久人妻麻豆按摩| 精品久久久久中文字幕一区| 久久亚洲精品成人av无码网站| 国产亚洲精品久久久久秋霞 | 久久精品亚洲男人的天堂| 久久99国产综合精品女同| jizzjizz国产精品久久| 91久久九九无码成人网站| 久久久久久久亚洲精品| 蜜桃麻豆www久久国产精品| 亚洲国产精品无码久久九九| 97久久国产综合精品女不卡| 国产成人精品久久免费动漫 | 热综合一本伊人久久精品| 久久人人爽人人爽人人爽| 亚洲色婷婷综合久久| 国内精品人妻无码久久久影院| 久久精品国产精品国产精品污| 久久精品国产一区二区三区| 久久久久人妻一区精品色| 久久国产精品无| 久久精品国产99久久丝袜|