我的问题包括如何分割这样的字符串:
""List of devices attached\r\n9887bc314\tdevice\r\n12n1n2nj1jn2
\tdevice\r\n\r\n"
进入:
[n9887bc314,n12n1n2nj1jn2]
我已经尝试过了,但是它抛出错误“参数1:无法从'string'转换为'char'”
string[] delimiterChars = new string[] {"\\","r","n","tdevice"};
string y = output.Substring(z+1);
string[] words;
words = y.Split(delimiterChars, StringSplitOptions.None);
我想知道我是否做错了什么,因为我在C#领域还很新。
非常感谢
最佳答案
用单词device分割整个字符串,然后从中删除制表符和换行。方法如下:
var wholeString = "List of devices attached\r\n9887bc314\tdevice\r\n12n1n2nj1jn2\tdevice\r\n\r\n";
var splits = wholeString.Split(new[] { "device" }, StringSplitOptions.RemoveEmptyEntries);
var device1 = splits[1].Substring(splits[1].IndexOf("\n") + 1).Replace("\t", "");
var device2 = splits[2].Substring(splits[2].IndexOf("\n") + 1).Replace("\t", "");