2

There are several stackoverflow question posted but none of them works for me.

I have a string say

   string str = "15.24\t7.28\t6.04\t5.0";

In App.Config, I define a key value

   <add key="delimiter" value="\t"/>

In the program, I have something like this

 string delimiter = ConfigurationManager.AppSettings["delimiter"]; 
 string[] parsedValues = str.Split(delimiter.ToCharArray());

While debugging I find that delimiter gets changed to "\t" and so it doesn't split the string.

I have tried changing App.Config to

 <add key="delimiter" value=@"\t"/> 

but this gives an error.

Even the following gives error.

 <add key="delimiter" value='\t' />

Any help greatly appreciated.

4

5 回答 5

6

&#09;尝试在您的配置文件中替换 \t 。在 XML 文件中,制表符由&#09;.

于 2013-08-08T03:28:14.857 回答
4

<add key="delimiter" value="&#x9;"/>

你可以做如下

string delimiter = ConfigurationManager.AppSettings["delimiter"]; 
string[] stringSeparators = new string[] {delimiter };
var result =str.Split(stringSeparators,StringSplitOptions.RemoveEmptyEntries);

结果 :

15.24 
7.28 
6.04 
5.0 
于 2013-08-08T03:27:37.967 回答
1
string[] parsedValues = str.Split(delimiter);

这就是你想要的。您不想将分隔符更改为字符数组。

希望这可以帮助。

于 2013-08-08T03:27:16.760 回答
0

我前段时间遇到过这个问题,我最终用它Regex.Unescape来解决这个问题

例子:

 string delimiter = Regex.Unescape(ConfigurationManager.AppSettings["delimiter"]); 
 string[] parsedValues = str.Split(delimiter.ToCharArray());
于 2013-08-08T03:40:03.410 回答
0
string[] parsedValues = str.Split(new[] { delimiter }, StringSplitOptions.None);

这段代码会得到你想要的。如果将 delimiter.ToCharArray() 传递给 string.Split 方法,它将使用 '\' 和 't' 来分割你的 str,这不是因为转义字符。

于 2013-08-08T03:40:04.007 回答