第 5 章 crifanLib.cs之String

目录

5.1. 格式化字符串中间对齐左右填充:formatstring
5.2. 初始化null的字符串位空字符串"":emptyStringArray
5.3. 将感叹号"!"强制编码为"%21":encodeExclamationMark
5.4. 将"%21"解码为感叹号"!":decodeExclamationMark
5.5. 从字符串中提取单个的子字符串:extractSingleStr
5.6. 组合参数列表(变成&xxx=yyy):quoteParas
5.7. 去除文件名或路径中非法字符:removeInvChrInPath
5.8. 把\xXX转换为对应的字符:filterEscapeSequence
5.9. 从文件的URL地址中提取文件名:extractFilenameFromUrl

此处是和字符串(string等)有关的函数

5.1. 格式化字符串中间对齐左右填充:formatstring


    //input: [4] Valid: B0009IQZFM
    //output: ============================ [4] Valid: B0009IQZFM =============================
    public string formatString(string strToFormat, char cPaddingChar = '*', int iTotalWidth = 80)
    {
        //auto added space
        strToFormat = " " + strToFormat + " "; //" [4] Valid: B0009IQZFM "

        //1. padding left
        int iPaddingLen = (iTotalWidth - strToFormat.Length)/2;
        int iLefTotalLen = iPaddingLen + strToFormat.Length;
        string strLefPadded = strToFormat.PadLeft(iLefTotalLen, cPaddingChar); //"============================ [4] Valid: B0009IQZFM "
        //2. padding right
        string strFormatted = strLefPadded.PadRight(iTotalWidth, cPaddingChar); //"============================ [4] Valid: B0009IQZFM ============================="
        
        return strFormatted;
    }

    

例 5.1. formatstring 的使用范例


            string strFullCategoryName = String.Format("FullCategoryName={0}", curFullCategoryName);
            string strFormattedFullCategoryName = crl.formatString(strFullCategoryName, '=');