汉字截取类

下午封装了一个字符或汉字获取第一个单词的类 

 

using  System;
using  System.Collections.Generic;
using  System.Text;
using  System.Text.RegularExpressions;

namespace  Nimeux.MyCOM
{
    
public class WordTransform
    
{
        
public WordTransform()
        
{ }
        
/// <summary>
        
/// 输入一个中文字,返回拼音的第一个小写字母
        
/// </summary>
        
/// <param name="cn">输入的中文字</param>
        
/// <returns></returns>

        static public string WordSpell(string cn)
        
{
            
byte[] arrCN = Encoding.Default.GetBytes(cn);
            
if (arrCN.Length > 1)
            
{
                
int firstWord = (short)arrCN[0];
                
int secondWord = (short)arrCN[1];
                
int code = (firstWord << 8+ secondWord;
                
int[] areacode = 4521745253457614631846826470104729747614481194811949062493244989650371506145062250906513875144652218526985269852698529805368954481 };
                
for (int i = 0; i < 26; i++)
                
{
                    
int max = 55290;
                    
if (i != 25)
                        max 
= areacode[i + 1];
                    
if (areacode[i] <= code && code < max)
                    
{
                        
//获取大写字母
                        return Encoding.Default.GetString(new byte[] { (byte)(97 + i) });
                    }

                }

                
return "*";
            }

            
else
                
return cn;
        }

        
/// <summary>
        
/// 输入一组中文,输出每个中文字拼音的小写
        
/// </summary>
        
/// <param name="cns">一组中文</param>
        
/// <returns></returns>

        static public string WordsSpell(string cns)
        
{
            
int num = cns.Length;
            
string result = "";
            
for (int i = 0; i < num; i++)
            
{
                result 
+= WordSpell(cns.Substring(i, 1));
            }

            
return result;
        }

        
/// <summary>
        
/// 判断输入的字符串是否是汉字
        
/// </summary>
        
/// <param name="str">输入的字符串</param>
        
/// <returns></returns>

        static public bool MatchCN(string str)
        
{
            
string flag = @"[一-龥]";
            Regex rg 
= new Regex(flag);
            
if (rg.IsMatch(str))
            
{
                
return true;
            }

            
else
            
{
                
return false;
            }

        }

        
/// <summary>
        
/// 提取输入字符串的第一个字母,如果是中文则提取拼音的第一个字母
        
/// </summary>
        
/// <param name="input">输入字符串</param>
        
/// <returns></returns>

        static public string Transform(string input)
        
{
            
string temp = string.Format(input);
            
if (MatchCN(temp))
            
{
                
return WordSpell(temp);
            }

            
else
            
{
                
string result = "";
                
for (int i = 0; i < 26; i++)
                
{                   
                    
if (temp.Substring(01== Encoding.Default.GetString(new byte[] { (byte)(65 + i) }))
                    
{
                        result 
= Encoding.Default.GetString(new byte[] { (byte)(97 + i) });
                        
break;
                    }

                    
else
                    
{
                        result 
= temp.Substring(01);
                    }

                }

                
return result;
            }

        }

    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值