使用控制字符展开C#字符串

时间:2014-02-06 14:12:57

标签: c# string substring stringbuilder

我需要在条形码扫描中扩展控制字符(不可打印)。

这就是我所拥有的。目前,我对如何从输入字符串的子字符串中获取整数索引感到兴奋。

    // A method for expanding ASCII Control Character into Human Readable format
    string ExpandCtrlString(string inStr)
    {
        // String Builder Capacity may need to be lengthened...
        StringBuilder outStr = new StringBuilder(128);
        // 0 Based Array representing the expansion of ASCII Control Characters
        string[] CtrlChars = new string[]
        {
            "<nul>",
            "<soh>",
            "<stx>",
            "<etx>",
            "<eot>",
            "<enq>",
            "<ack>",
            "<bel>",
            "<bs>",
            "<tab>",
            "<lf>",
            "<vt>",
            "<ff>",
            "<cr>",
            "<so>",
            "<si>",
            "<dle>",
            "<dc1>",
            "<dc2>",
            "<dc3>",
            "<dc4>",
            "<nak>",
            "<syn>",
            "<etb>",
            "<can>",
            "<em>",
            "<sub>",
            "<esc>",
            "<fs>",
            "<gs>",
            "<rs>",
            "<us>"
        };

        for (int n = 0; n < inStr.Length; n++)
        {
            if (Char.IsControl(inStr, n))
            {
                //char q = inStr.Substring(n, 1);
                int x = (int)inStr[n] ();
                outStr.Append(CtrlChars[x]);
            }
            else
            {
                outStr.Append(inStr.Substring(n, 1));
            }
        }
        return outStr.ToString();
    }

编辑: 当我沉思时,想到了......

我对子串进行了双重渲染,并且有效...

那就是演员......:)

    for (int n = 0; n < inStr.Length; n++)
    {
        if (Char.IsControl(inStr, n))
        {
            int x = (int)(char)inStr[n];
            outStr.Append(CtrlChars[x]);
        }
        else
        {
            outStr.Append(inStr.Substring(n, 1));
        }
    }
    return outStr.ToString();

并且,它适用于Windows Mobile 5中的CF NET 2.0

我曾经考虑过使用foreach,但是,这对于一个老的VB6家伙来说存在其他问题。 :)

2 个答案:

答案 0 :(得分:0)

char可以像数字一样使用。这是一个更简单的循环:

foreach (char c in inStr)
{
    // Check to see if c (as a number here) is less than the CtrlChars array size
    // If it is, then print the CtrlChars text
    if (c < CtrlChars.Length)   
    {
        outStr.Append(CtrlChars[c]);
    }
    else
    {
        outStr.Append(c);
    }
}

答案 1 :(得分:0)

我可以看到你的方式来自你的方法,但我也可以看到你想要做的事情的一些缺陷。

首先......我在没有测试的情况下这样做..所以买家要小心!

其次,来自Microsoft的Char.IsControl Method (String, Int32)The Unicode standard assigns code points from \U0000 to \U001F, \U007F, and from \U0080 to \U009F to control characters.您的数组并未覆盖所有这些范围,因此可能(取决于您的字符串的来源)您的代码实际上会像您一样爆炸提供超出数组范围的索引。这可以通过使用字典从数字映射到字符串而不是数组来解决。

dictionary<int, string> mapping = new dictionary<int, string>() {
  { 0, "<nul>" },
  { 1, "<soh>"  },

  .. etc filled with every possible control char
}

请注意,这个字典确实应该在包含ExpandCtrlString方法的中定义/构建,而不是在方法本身中。

class expand
{
   private dictionary<int, string> mapping …

   public string ExpandCtrlString(string inStr)
   {
     ...
    }

}

要获得你的信息,你可以做到:

for (int n = 0; n < inStr.Length; n++)   
{
    char ch = instr[n];
    ...
}

或者如果你想更加现代化,你可以直接从字符串中获取字符:

foreach(var ch in instr)
{
    ...
}

然后检查char是否是控件char,如果是,则在字典中

if (Char.IsControl(ch))
{
    // The value is not needed until you know its a control char
    int val = (int)Char.GetNumericValue(ch);

    if (mapping.ContainsKey(val))
    {
        // You know what the control char is, so output its name
        outStr.Append(mapping[val]);
    }
    else
    {
       // You might have missed a control char in your dictionary of names
       // So by default output its hex value instead
       outStr.AppendFormat("<{0}>", val.ToString("X"));  
    }
}
else
{
    // Char is not a control char
    outStr.Append(ch);
}

然后最后返回你的outStr。