C#获得字符串首字符字母(大写)
2024-08-29 21:54:03
/// <summary>
/// 获得字符串首字符字母(大写);
/// </summary>
/// <param name="cnChar"></param>
/// <returns></returns>
public string GetStringFirstSpell(string cnChar)
{
//除字母、数字、汉字以外的返回"*"
var result = "*"; if (string.IsNullOrEmpty(cnChar.Trim()))
return result; cnChar = cnChar.Trim().Substring(, );
byte[] arrCn = Encoding.Default.GetBytes(cnChar); //首字为字符,占一个字节
if (arrCn.Length <= )
{
//大写英文字母
if ((short)arrCn[] >= && (short)arrCn[] <= )
return cnChar; //小写英文字母
if (arrCn[] >= && arrCn[] <= )
return Encoding.Default.GetString(new byte[] { (byte)((short)arrCn[] - ) }); //数字
switch (cnChar)
{
case "":
result = "Y";
break;
case "":
result = "E";
break;
case "":
case "":
result = "S";
break;
case "":
result = "W";
break;
case "":
case "":
result = "L";
break;
case "":
result = "Q";
break;
case "":
result = "B";
break;
case "":
result = "J";
break;
}
return result;
}
//首字为汉字,占两个字节
else
{
int area = (short)arrCn[];
int pos = (short)arrCn[];
int code = (area << ) + pos;
int[] areacode = { , , , , , , , , , , , , , , , , , , , , , , , , , };
for (int i = ; i < ; i++)
{
int max = ;
if (i != ) max = areacode[i + ];
if (areacode[i] <= code && code < max)
{
return Encoding.Default.GetString(new byte[] { (byte)( + i) });
}
}
return result;
}
}
有时候需要对汉字获取其拼音首字母,可以用以上的方法。
最新文章
- 深入理解脚本化CSS系列第二篇——查询计算样式
- bzoj violet系列 (2708~2725)
- 转 Microsoft&#39;s Objective-C tech started on BlackBerryOS, Tizen
- 这些HTML、CSS知识点,面试和平时开发都需要 No5-No7
- aperm方法
- 使用J2SE API读取Properties文件的六种方法
- springmvc(1)--配置
- IIS中的Application.CommonAppDataPath
- .net Web.Config配置文件 转
- video标签 拖动 转自w3school
- An internal error occurred during: ";Retrieving archetypes:";. GC overhead limit exceeded
- hashmap源码研究
- vs 2017 打开 iis express问题
- 使用eclipse创建web项目的项目图文步骤
- AD 16 下绘图的几个技巧
- 会话(Session)与cookies
- deep learning自学知识积累笔记
- (1.1)mysql 选择合适的数据类型
- C#线程篇---解答线程之惑(2)
- Vuex深入理解