zoukankan      html  css  js  c++  java
  • 在C#中截取指定长度的中文字符串

    private static string SubString(string stringToSub, int length) {
                Regex regex 
    = new Regex("[\u4e00-\u9fa5]+", RegexOptions.Compiled);
                
    char[] stringChar = stringToSub.ToCharArray();
                StringBuilder sb 
    = new StringBuilder();
                
    int nLength = 0;

                
    for(int i = 0; i < stringChar.Length; i++{
                    
    if (regex.IsMatch((stringChar[i]).ToString())) {
                        sb.Append(stringChar[i]);
                        nLength 
    += 2;
                    }

                    
    else {
                        sb.Append(stringChar[i]);
                        nLength 
    = nLength + 1;
                    }


                    
    if (nLength > length)
                        
    break;
                }


                
    return sb.ToString();
            }
  • 相关阅读:
    RocketMQ Message hasn't been sent. Caused by No route info of this topic, test-topic
    Barrier
    WPF之资源
    WPF之命令
    WPF之事件
    WPF之属性
    多路Binding
    Binding的数据转换
    Binding的数据校验
    为Binding指定源的方法
  • 原文地址:https://www.cnblogs.com/Luna/p/57027.html
Copyright © 2011-2022 走看看