字符串拆分不会拆分[0].属性由点组成

Eli*_*eth 0 c# string split

string[] array = indexAndProperty.Split(new char['.']); // [0].PreCondition
Run Code Online (Sandbox Code Playgroud)

为什么数组只有一个带字符串的元素"[0].PreCondition"

我预期由点到分割字符串,并获得2种元素"[0]""PreCondition".

p.s*_*w.g 8

new char['.']不会创建一个包含一个字符的数组'.'.相反,'.'它被强制转换为a int,并且整数等价于'.'46,因此它实际上创建了一个包含46个副本的数组'\0'.

试试这个:

string[] array = indexAndProperty.Split(new char[] { '.' });
Run Code Online (Sandbox Code Playgroud)

或者更好,因为separator参数Split是一个params数组,你可以这样做:

string[] array = indexAndProperty.Split('.');
Run Code Online (Sandbox Code Playgroud)