c# 如何将字符串转换为大写或小写

2022-11-13 09:11:12 字符串 转换为 如何将

c#将字符串转换为大写或小写

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 将字符串转换为大写小写
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            label1.Text = "输入字符串";
            label2.Text = "转换后的字符串";
            button1.Text = "确认转换";
            radioButton1.Text = "大写";
            radioButton2.Text = "小写";
            radioButton1.Checked = true;
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == " ")            //判断文本框是否为空
            {
                MessageBox.Show("请输入字符串");
            }
            if(radioButton1.Checked==true)        
            {
                textBox2.Text = textBox1.Text.ToUpper();            //ToUpper()方法将小写字母转换成大写字母
            }
            else 
            {
                textBox2.Text = textBox1.Text.ToLower();           //ToUpper()方法将大写字母转换成小写字母
            }
        }
 
        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)        //用到了textbox的keypress事件,用来判断输入的是否为想要的字母,如果不是,将禁止输入
        {
            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表示没有处理过,同意输入
            }
            else
            {
                e.Handled = true;        //e.handled=false表示处理过了,禁止输入
            }
        }
    }
}

运行结果

c#大小写转换合集

 private static readonly String cnNumber = "零壹贰叁肆伍陆柒捌玖";
 private static readonly String cnUnit = "分角元拾佰仟万拾佰仟亿拾佰仟兆拾佰仟";

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

  /// <summary>
        /// 传入原日期和所加月数,按照excel加月数算法,返回新日期
        /// Excel计算方式和C#的计算方式不一致
        /// C#的计算方式:若新日期超出了此月的最大日期数,比如9月31日,新日期为该月的最后一天9月30日
        /// Excel的算法: 若新日期超出了此月的最大日期数,比如9月31日,新日期为该月最后一日延后超出的天数即是10月1日
        /// </summary>
        /// <param name="old_date">原日期</param>
        /// <param name="monthNum">所加的月数</param>
        /// <returns>新的日期</returns>
        public static DateTime Excel计算日期(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.数字转大写

        /// <summary>
        /// 数字转化为大写
        /// </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转数字,转换为字符串,再转数字
                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.日、月、日期转大写

        //月转化为大写
        public static string monthtoUpper(int month)
        {
            if (month < 10)
            {
                return numtoUpper(month);
            }
            else
                if (month == 10) { return "十"; }
                else
                {
                    return "十" + numtoUpper(month - 10);
                }
        }
        //日转化为大写
        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()));
                }
            }
        }
        //日期转换为大写
        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.人民币金额小写转大写

        /// <summary> 
        /// 转换人民币大小金额 
        /// </summary> 
        /// <param name="num">金额</param> 
        /// <returns>返回大写形式</returns> 
        public static string GetUpper(decimal num)
        {
            String[] tmpString = num.ToString().Split('.');
            String intString = num.ToString();   // 默认为整数
            String decString = "";            // 保存小数部分字串
            String rmbCapital = "";            // 保存中文大写字串
            int k;
            int j;
            int n;
 
            if (tmpString.Length > 1)
            {
                intString = tmpString[0];             // 取整数部分
                decString = tmpString[1];             // 取小数部分
            }
            decString += "00";
            decString = decString.Substring(0, 2);   // 保留两位小数位
            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];     // 供调试用的直接转换
                        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("兆亿万", "兆");
                    rmbCapital = rmbCapital.Replace("兆亿", "兆");
                    rmbCapital = rmbCapital.Replace("亿万", "亿");
                    rmbCapital = rmbCapital.TrimStart('元');
                    rmbCapital = rmbCapital.TrimStart('零');
 
                    string lastStr = rmbCapital.Substring(rmbCapital.Length - 1);
                    if (lastStr == "元" || lastStr == "角")
                        rmbCapital += "整";
                    return rmbCapital;
                }
                else
                {
                    return "";   // 超出转换范围时,返回零长字串
                }
            }
            catch
            {
                return "";   // 含有非数值字符时,返回零长字串
            }
        }

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

        /// <summary>
        /// 获取中文拼音首字母
        /// </summary>
        /// <param name="ChineseStr">中文字符串</param>
        /// <param name="ToUpper">是否转化为大写</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);// 得到汉字符的字节数组
                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.银行卡号设置4位加空格

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

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

相关文章