提问人:RᗅNDY̳ XD 提问时间:12/9/2022 最后编辑:RᗅNDY̳ XD 更新时间:12/9/2022 访问量:86
获取字符串中的字符串
Get a string inside a string
问:
string a = "100-0-6-7-6-10-8-" //and so on
/////////////////////////////////////////////
//my solution
char[] delimiterChars = {'-'};
string solution = "100-0-6-7-6-10-8-";
string[] words = solution.Split(delimiterChars,System.StringSplitOptions.RemoveEmptyEntries);
//ok so i encounter some problems when trying to overwrite 0s, and 10s,100s etc
//so when writing data to the "solution" string it should be
//"-100--0--6--7--6--10--8-" instead of "100-0-6-7-6-10-8-"
基本上,我想将每个数字分开并将其放在列表或数组中
答:
1赞
RᗅNDY̳ XD
12/9/2022
#1
我想我明白了
char[] delimiterChars = {'-'};
string text = "100-0-6-7-6-10-8-";
string[] words = text.Split(delimiterChars,System.StringSplitOptions.RemoveEmptyEntries);
0赞
RoninEngineer
12/9/2022
#2
使用 Linq 的此解决方案也有效。
var text="100-0-6-7-6-10-8-";
var words=text.Split("-").Where(x => !string.IsNullOrEmpty(x)).ToArray();
评论
var result = a.Split("-");
?