我的常用代码集

.Net

/// <summary>
/// 获取汉字字符串的首拼音字母字符串
/// </summary>
/// <param name="text">需要转换的字符串</param>
/// <param name="halfChar">半角字符替换符(*不替换)</param>
/// <param name="fullChar">全角字符替换符(*不替换)</param>
public static string GetCnSpell(string text, string halfChar = "", string fullChar = "")
{
    int[] areacode = { 45217, 45253, 45761, 46318, 46826, 47010, 47297, 47614, 48119, 48119, 49062, 49324, 49896, 50371, 50614, 50622, 50906, 51387, 51446, 52218, 52698, 52698, 52698, 52980, 53689, 54481, 55290 };

    string result = "";
    foreach (char item in text)
    {
    byte[] arrCN = System.Text.Encoding.Default.GetBytes(item + "");
    if (arrCN.Length > 1)
    {
        int code = (arrCN[0] << 8) + arrCN[1];
        for (int i = 0; i < 26; i++)
        {
        if (code >= areacode[i] && code < areacode[i + 1])
        {
            result += (char)(i += 65);
        }
        }
        if (code < areacode[0] || code >= areacode[areacode.Length - 1])
        result += fullChar.Replace('*', item); //全角字符替换
    }
    else result += halfChar.Replace('*', item); //半角字符替换
    }
    return result;
}
获取汉字字符串的首拼音
//获取对象Display Name
Func<object, string, string> getName = (obj, name) =>{
    if (obj.GetType().GetProperty(name) == null) return "null";
    var attr = obj.GetType().GetProperty(name).GetCustomAttributes(typeof(DisplayAttribute), true).FirstOrDefault();
    return (attr != null) ? (attr as DisplayAttribute).Name : "null";
};
获取对象Display Name

 

 

 

JavaScript

//JS 多行文本格式化
Function.prototype.getMultiline = function( strFormat ){ 
    var lines = new String(this);
    lines = lines.substring(lines.indexOf("/*") + 4, lines.lastIndexOf("*/"));  
    if(strFormat){
        var strs = lines.split("\r\n"), lines = "";
        for( i in strs ){
            if(strs[i]) lines += strFormat.replace(/\{0\}/g,strs[i]);
        }
    }
    window.clipboardData.setData("Text",lines);
    return lines; 
}   
var str = function() {  
/*
ProductID
FinanceOrgID
ProductName
ProductLimit
*/}.getMultiline('{"{0}",null},\n');
多行文本格式化

 

posted @ 2016-09-26 18:17  Hanf  阅读(262)  评论(0编辑  收藏  举报
Copyright © 2008-2016 Hanf All Rights Reserved