最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

c#?如何將字符串轉(zhuǎn)換為大寫或小寫

 更新時(shí)間:2022年06月10日 08:45:35   作者:錫城筱凱  
這篇文章主要介紹了c#?如何將字符串轉(zhuǎn)換為大寫或小寫,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

c#將字符串轉(zhuǎn)換為大寫或小寫

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace 將字符串轉(zhuǎn)換為大寫小寫
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            label1.Text = "輸入字符串";
            label2.Text = "轉(zhuǎn)換后的字符串";
            button1.Text = "確認(rèn)轉(zhuǎn)換";
            radioButton1.Text = "大寫";
            radioButton2.Text = "小寫";
            radioButton1.Checked = true;
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == " ")            //判斷文本框是否為空
            {
                MessageBox.Show("請(qǐng)輸入字符串");
            }
            if(radioButton1.Checked==true)        
            {
                textBox2.Text = textBox1.Text.ToUpper();            //ToUpper()方法將小寫字母轉(zhuǎn)換成大寫字母
            }
            else 
            {
                textBox2.Text = textBox1.Text.ToLower();           //ToUpper()方法將大寫字母轉(zhuǎn)換成小寫字母
            }
        }
 
        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)        //用到了textbox的keypress事件,用來(lái)判斷輸入的是否為想要的字母,如果不是,將禁止輸入
        {
            if ((e.KeyChar >= '1' && e.KeyChar <= '9' )||( e.KeyChar >= 'a' && e.KeyChar <= 'z' )|| (e.KeyChar >= 'A' && e.KeyChar <= 'Z'))
            {
                e.Handled = false;        //e.handled=false表示沒有處理過(guò),同意輸入
            }
            else
            {
                e.Handled = true;        //e.handled=false表示處理過(guò)了,禁止輸入
            }
        }
    }
}

運(yùn)行結(jié)果

c#大小寫轉(zhuǎn)換合集

?private static readonly String cnNumber = "零壹貳叁肆伍陸柒捌玖";
?private static readonly String cnUnit = "分角元拾佰仟萬(wàn)拾佰仟億拾佰仟兆拾佰仟";

1.在代碼中以 Excel 加日期的算法

  /// <summary>
        /// 傳入原日期和所加月數(shù),按照excel加月數(shù)算法,返回新日期
        /// Excel計(jì)算方式和C#的計(jì)算方式不一致
        /// C#的計(jì)算方式:若新日期超出了此月的最大日期數(shù),比如9月31日,新日期為該月的最后一天9月30日
        /// Excel的算法: 若新日期超出了此月的最大日期數(shù),比如9月31日,新日期為該月最后一日延后超出的天數(shù)即是10月1日
        /// </summary>
        /// <param name="old_date">原日期</param>
        /// <param name="monthNum">所加的月數(shù)</param>
        /// <returns>新的日期</returns>
        public static DateTime Excel計(jì)算日期(DateTime old_date, int monthNum)
        {
            int day = old_date.Day;
            DateTime new_date = DateTime.Now;
            new_date = old_date.AddMonths(monthNum);
            switch (day)
            {
                case 29:
                    if (!DateTime.IsLeapYear(new_date.Year) && new_date.Month == 2 && new_date.Day == 28)
                    {
                        new_date = new_date.AddDays(1);
                    }
                    break;
                case 30:
                    if (new_date.Month == 2)
                    {
                        if (DateTime.IsLeapYear(new_date.Year))
                        {
                            if (new_date.Month == 2 && new_date.Day == 29)
                            {
                                new_date = new_date.AddDays(1);
                            }
                        }
                        else
                        {
                            if (new_date.Month == 2 && new_date.Day == 28)
                            {
                                new_date = new_date.AddDays(1);
                            }
                        }
                    }
                    break;
 
                case 31:
                    if (new_date.Day == 30)
                    {
                        if (new int[] { 4, 6, 9, 11 }.Contains(new_date.Month))
                        {
                            new_date = new_date.AddDays(1);
                        }
                    }
                    else if (new_date.Day == 29)
                    {
                        if (DateTime.IsLeapYear(new_date.Year))
                        {
                            new_date = new_date.AddDays(1);
                        }
                    }
                    else if (new_date.Day == 28)
                    {
                        if (!DateTime.IsLeapYear(new_date.Year))
                        {
                            new_date = new_date.AddDays(1);
                        }
                    }
                    break;
                default:
                    break;
            }
            return new_date;
        }

2.數(shù)字轉(zhuǎn)大寫

        /// <summary>
        /// 數(shù)字轉(zhuǎn)化為大寫
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public static string numtoUpper(int num)
        {
            String str = num.ToString();
            string rstr = "";
            int n;
            for (int i = 0; i < str.Length; i++)
            {
                n = Convert.ToInt16(str[i].ToString());//char轉(zhuǎn)數(shù)字,轉(zhuǎn)換為字符串,再轉(zhuǎn)數(shù)字
                switch (n)
                {
                    case 0: rstr = rstr + "〇"; break;
                    case 1: rstr = rstr + "一"; break;
                    case 2: rstr = rstr + "二"; break;
                    case 3: rstr = rstr + "三"; break;
                    case 4: rstr = rstr + "四"; break;
                    case 5: rstr = rstr + "五"; break;
                    case 6: rstr = rstr + "六"; break;
                    case 7: rstr = rstr + "七"; break;
                    case 8: rstr = rstr + "八"; break;
                    default: rstr = rstr + "九"; break;
                }
            }
            return rstr;
        }

3.日、月、日期轉(zhuǎn)大寫

        //月轉(zhuǎn)化為大寫
        public static string monthtoUpper(int month)
        {
            if (month < 10)
            {
                return numtoUpper(month);
            }
            else
                if (month == 10) { return "十"; }
                else
                {
                    return "十" + numtoUpper(month - 10);
                }
        }
        //日轉(zhuǎn)化為大寫
        public static string daytoUpper(int day)
        {
            if (day < 20)
            {
                return monthtoUpper(day);
            }
            else
            {
                String str = day.ToString();
                if (str[1] == '0')
                {
                    return numtoUpper(Convert.ToInt16(str[0].ToString())) + "十";
                }
                else
                {
                    return numtoUpper(Convert.ToInt16(str[0].ToString())) + "十"
                        + numtoUpper(Convert.ToInt16(str[1].ToString()));
                }
            }
        }
        //日期轉(zhuǎn)換為大寫
        public static string dateToUpper(System.DateTime date)
        {
            int year = date.Year;
            int month = date.Month;
            int day = date.Day;
            return numtoUpper(year) + "年" + monthtoUpper(month) + "月" + daytoUpper(day) + "日";
        }

4.人民幣金額小寫轉(zhuǎn)大寫

        /// <summary> 
        /// 轉(zhuǎn)換人民幣大小金額 
        /// </summary> 
        /// <param name="num">金額</param> 
        /// <returns>返回大寫形式</returns> 
        public static string GetUpper(decimal num)
        {
            String[] tmpString = num.ToString().Split('.');
            String intString = num.ToString();   // 默認(rèn)為整數(shù)
            String decString = "";            // 保存小數(shù)部分字串
            String rmbCapital = "";            // 保存中文大寫字串
            int k;
            int j;
            int n;
 
            if (tmpString.Length > 1)
            {
                intString = tmpString[0];             // 取整數(shù)部分
                decString = tmpString[1];             // 取小數(shù)部分
            }
            decString += "00";
            decString = decString.Substring(0, 2);   // 保留兩位小數(shù)位
            intString += decString;
 
            try
            {
                k = intString.Length - 1;
                if (k > 0 && k < 18)
                {
                    for (int i = 0; i <= k; i++)
                    {
                        j = (int)intString[i] - 48;
                        // rmbCapital = rmbCapital + cnNumber[j] + cnUnit[k-i];     // 供調(diào)試用的直接轉(zhuǎn)換
                        n = i + 1 >= k ? (int)intString[k] - 48 : (int)intString[i + 1] - 48; // 等效于 if( ){ }else{ }
                        if (j == 0)
                        {
                            if (k - i == 2 || k - i == 6 || k - i == 10 || k - i == 14)
                            {
                                rmbCapital += cnUnit[k - i];
                            }
                            else
                            {
                                if (n != 0)
                                {
                                    rmbCapital += cnNumber[j];
                                }
                            }
                        }
                        else
                        {
                            rmbCapital = rmbCapital + cnNumber[j] + cnUnit[k - i];
                        }
                    }
 
                    rmbCapital = rmbCapital.Replace("兆億萬(wàn)", "兆");
                    rmbCapital = rmbCapital.Replace("兆億", "兆");
                    rmbCapital = rmbCapital.Replace("億萬(wàn)", "億");
                    rmbCapital = rmbCapital.TrimStart('元');
                    rmbCapital = rmbCapital.TrimStart('零');
 
                    string lastStr = rmbCapital.Substring(rmbCapital.Length - 1);
                    if (lastStr == "元" || lastStr == "角")
                        rmbCapital += "整";
                    return rmbCapital;
                }
                else
                {
                    return "";   // 超出轉(zhuǎn)換范圍時(shí),返回零長(zhǎng)字串
                }
            }
            catch
            {
                return "";   // 含有非數(shù)值字符時(shí),返回零長(zhǎng)字串
            }
        }

5.獲取中文字拼音首字母

        /// <summary>
        /// 獲取中文拼音首字母
        /// </summary>
        /// <param name="ChineseStr">中文字符串</param>
        /// <param name="ToUpper">是否轉(zhuǎn)化為大寫</param>
        /// <returns></returns>
        public static string GetSpellCode(string ChineseStr, bool ToUpper = true)
        {
            string Capstr = string.Empty;
            byte[] ZW = new byte[2];
            long ChineseStr_int;
            string CharStr, ChinaStr = "";
            for (int i = 0; i <= ChineseStr.Length - 1; i++)
            {
                CharStr = ChineseStr.Substring(i, 1).ToString();
                ZW = System.Text.Encoding.Default.GetBytes(CharStr);// 得到漢字符的字節(jié)數(shù)組
                if (ZW.Length == 1)
                {
                    ChinaStr = CutString(CharStr.ToUpper(), 1);
                }
                else
                {
                    int i1 = (short)(ZW[0]);
                    int i2 = (short)(ZW[1]);
                    ChineseStr_int = i1 * 256 + i2;
                    if ((ChineseStr_int >= 45217) && (ChineseStr_int <= 45252)) { ChinaStr = "a"; }
                    else if ((ChineseStr_int >= 45253) && (ChineseStr_int <= 45760)) { ChinaStr = "b"; }
                    else if ((ChineseStr_int >= 45761) && (ChineseStr_int <= 46317)) { ChinaStr = "c"; }
                    else if ((ChineseStr_int >= 46318) && (ChineseStr_int <= 46825)) { ChinaStr = "d"; }
                    else if ((ChineseStr_int >= 46826) && (ChineseStr_int <= 47009)) { ChinaStr = "e"; }
                    else if ((ChineseStr_int >= 47010) && (ChineseStr_int <= 47296)) { ChinaStr = "f"; }
                    else if ((ChineseStr_int >= 47297) && (ChineseStr_int <= 47613)) { ChinaStr = "g"; }
                    else if ((ChineseStr_int >= 47614) && (ChineseStr_int <= 48118)) { ChinaStr = "h"; }
                    else if ((ChineseStr_int >= 48119) && (ChineseStr_int <= 49061)) { ChinaStr = "j"; }
                    else if ((ChineseStr_int >= 49062) && (ChineseStr_int <= 49323)) { ChinaStr = "k"; }
                    else if ((ChineseStr_int >= 49324) && (ChineseStr_int <= 49895)) { ChinaStr = "l"; }
                    else if ((ChineseStr_int >= 49896) && (ChineseStr_int <= 50370)) { ChinaStr = "m"; }
                    else if ((ChineseStr_int >= 50371) && (ChineseStr_int <= 50613)) { ChinaStr = "n"; }
                    else if ((ChineseStr_int >= 50614) && (ChineseStr_int <= 50621)) { ChinaStr = "o"; }
                    else if ((ChineseStr_int >= 50622) && (ChineseStr_int <= 50905)) { ChinaStr = "p"; }
                    else if ((ChineseStr_int >= 50906) && (ChineseStr_int <= 51386)) { ChinaStr = "q"; }
                    else if ((ChineseStr_int >= 51387) && (ChineseStr_int <= 51445)) { ChinaStr = "r"; }
                    else if ((ChineseStr_int >= 51446) && (ChineseStr_int <= 52217)) { ChinaStr = "s"; }
                    else if ((ChineseStr_int >= 52218) && (ChineseStr_int <= 52697)) { ChinaStr = "t"; }
                    else if ((ChineseStr_int >= 52698) && (ChineseStr_int <= 52979)) { ChinaStr = "w"; }
                    else if ((ChineseStr_int >= 52980) && (ChineseStr_int <= 53640)) { ChinaStr = "x"; }
                    else if ((ChineseStr_int >= 53689) && (ChineseStr_int <= 54480)) { ChinaStr = "y"; }
                    else if ((ChineseStr_int >= 54481) && (ChineseStr_int <= 55289)) { ChinaStr = "z"; }
                    else { ChinaStr = CharStr; }
                }
                Capstr += ChinaStr;
            }
            Capstr = Capstr.Replace("昊", "h").Replace("丞", "c").Replace("鑫", "x").Replace("蘄", "Q");
            return ToUpper ? Capstr.ToUpper() : Capstr.ToLower();
        }

6.銀行卡號(hào)設(shè)置4位加空格

        /// <summary>
        /// 銀行卡每四位加空格
        /// </summary>
        /// <param name="Card">銀行卡號(hào)</param>
        /// <returns></returns>
        public static string 處理銀行卡號(hào)(string Card)
        {
            if (String.IsNullOrEmpty(Card))
                return "";
            Card = Card.Replace(" ","");           
            return Regex.Replace(Card, @"(\d{4}(?!$))", "$1 ");
        }

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • C#實(shí)現(xiàn)石頭剪刀布游戲

    C#實(shí)現(xiàn)石頭剪刀布游戲

    這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)石頭剪刀布游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-10-10
  • C# CSV文件讀寫的實(shí)現(xiàn)

    C# CSV文件讀寫的實(shí)現(xiàn)

    本文主要介紹了C# CSV文件讀寫的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03
  • Unity UI或3D場(chǎng)景實(shí)現(xiàn)跟隨手機(jī)陀螺儀的晃動(dòng)效果

    Unity UI或3D場(chǎng)景實(shí)現(xiàn)跟隨手機(jī)陀螺儀的晃動(dòng)效果

    這篇文章主要介紹了Unity UI或3D場(chǎng)景實(shí)現(xiàn)跟隨手機(jī)陀螺儀的晃動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-11-11
  • c#使用反射調(diào)用類型成員示例

    c#使用反射調(diào)用類型成員示例

    學(xué)習(xí)C#的時(shí)候就知道使用反射可以對(duì)我們編程提供極大的便利(動(dòng)態(tài)的獲取信息、調(diào)用類型成員、創(chuàng)建實(shí)例等等),下面示例說(shuō)明一下使用方法
    2014-01-01
  • C#實(shí)現(xiàn)線性查找算法

    C#實(shí)現(xiàn)線性查找算法

    這篇文章介紹了C#實(shí)現(xiàn)線性查找的算法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-08-08
  • C#實(shí)現(xiàn)目錄跳轉(zhuǎn)(TreeView和SplitContainer)的示例代碼

    C#實(shí)現(xiàn)目錄跳轉(zhuǎn)(TreeView和SplitContainer)的示例代碼

    本文主要介紹了C#實(shí)現(xiàn)目錄跳轉(zhuǎn)(TreeView和SplitContainer)的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • C#網(wǎng)絡(luò)爬蟲代碼分享 C#簡(jiǎn)單的爬取工具

    C#網(wǎng)絡(luò)爬蟲代碼分享 C#簡(jiǎn)單的爬取工具

    這篇文章主要為大家詳細(xì)介紹了C#網(wǎng)絡(luò)爬蟲代碼,教大家如何制作了簡(jiǎn)單的爬取工具,感興趣的小伙伴們可以參考一下
    2016-07-07
  • Unity3D基于OnGUI實(shí)時(shí)顯示FPS

    Unity3D基于OnGUI實(shí)時(shí)顯示FPS

    這篇文章主要介紹了Unity3D基于OnGUI實(shí)時(shí)顯示FPS,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-11-11
  • C#自定義導(dǎo)出數(shù)據(jù)到Excel的類實(shí)例

    C#自定義導(dǎo)出數(shù)據(jù)到Excel的類實(shí)例

    這篇文章主要介紹了C#自定義導(dǎo)出數(shù)據(jù)到Excel的類,實(shí)例分析了C#操作Excel的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2015-03-03
  • C#訪問(wèn)PostGreSQL數(shù)據(jù)庫(kù)的方法

    C#訪問(wèn)PostGreSQL數(shù)據(jù)庫(kù)的方法

    這次的項(xiàng)目中的一個(gè)環(huán)節(jié)要求我把PostGreSQL數(shù)據(jù)取出來(lái),然后放到SqlServer里,再去處理分析。
    2013-04-04

最新評(píng)論

永兴县| 繁峙县| 遂溪县| 古丈县| 公主岭市| 司法| 民和| 龙里县| 台南市| 墨竹工卡县| 麟游县| 牡丹江市| 苏州市| 华坪县| 萝北县| 洪雅县| 商水县| 九龙坡区| 新晃| 扎鲁特旗| 西藏| 郎溪县| 师宗县| 泰州市| 营山县| 凌云县| 五常市| 封开县| 襄樊市| 秦安县| 平罗县| 盐山县| 永吉县| 股票| 承德县| 雷波县| 石棉县| 黄浦区| 汝南县| 舒兰市| 永修县|