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事件,用來判斷輸入的是否為想要的字母,如果不是,將禁止輸入
{
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表示處理過了,禁止輸入
}
}
}
}運(yùn)行結(jié)果


c#大小寫轉(zhuǎn)換合集
?private static readonly String cnNumber = "零壹貳叁肆伍陸柒捌玖"; ?private static readonly String cnUnit = "分角元拾佰仟萬拾佰仟億拾佰仟兆拾佰仟";
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("兆億萬", "兆");
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 ""; // 超出轉(zhuǎn)換范圍時(shí),返回零長字串
}
}
catch
{
return ""; // 含有非數(shù)值字符時(shí),返回零長字串
}
}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è)參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- C#使用Enum.TryParse()實(shí)現(xiàn)枚舉安全轉(zhuǎn)換
- c#中(int)、int.Parse()、int.TryParse、Convert.ToInt32的區(qū)別詳解
- C#(int)中Convert、Parse、TryParse的區(qū)別
- C#字節(jié)數(shù)組(byte[])和字符串相互轉(zhuǎn)換方式
- C#中對(duì)象與JSON字符串互相轉(zhuǎn)換的三種方式
- C#中字符串與字節(jié)數(shù)組的轉(zhuǎn)換方式
- C#日期格式字符串的相互轉(zhuǎn)換操作實(shí)例分析
- C# 字符串、數(shù)組和List的截取和轉(zhuǎn)換實(shí)例
- C#實(shí)現(xiàn)char字符數(shù)組與字符串相互轉(zhuǎn)換的方法
- C# 中 TryParse如何將字符串轉(zhuǎn)換為特定類型
相關(guān)文章
Unity3D Shader實(shí)現(xiàn)動(dòng)態(tài)屏幕遮罩
這篇文章主要為大家詳細(xì)介紹了Unity3D Shader實(shí)現(xiàn)動(dòng)態(tài)屏幕遮罩效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02
C#如何實(shí)現(xiàn)監(jiān)控手機(jī)屏幕(附源碼下載)
這篇文章主要介紹了C#如何實(shí)現(xiàn)監(jiān)控手機(jī)屏幕(附源碼下載),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
詳解C#如何在不同工作簿之間復(fù)制選定單元格區(qū)域
處理Excel文檔時(shí),我們經(jīng)常需要將數(shù)據(jù)整合到一個(gè)工作表以便于我們進(jìn)行管理或數(shù)據(jù)對(duì)比。本文將演示如何通過編程方式將選定的單元格區(qū)域從一個(gè)工作簿復(fù)制到另一個(gè)工作簿2023-02-02
Unity打開淘寶app并跳轉(zhuǎn)到商品頁面功能的實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于如何利用Unity打開淘寶app并跳轉(zhuǎn)到商品頁面功能的相關(guān)資料,這個(gè)功能目前在網(wǎng)上找不到相關(guān)的解決方法,所以自己寫了出來,需要的朋友可以參考下2021-07-07
C#基礎(chǔ):基于const與readonly的深入研究
本篇文章是對(duì)c#中const與readonly進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05

