所以我要比其他帖子更好地解释自己。我有两个例句:
NEIGHBORHOOD X ADIDAS CONSORTIUM SUPERSTAR 80 10TH ANNIVERSARY -- BLACKGREY 21
和
ADIDAS STAN SMITH - RED 22
我需要一个代码来获取产品名称,数量(又名第一个数字)和个人价格(又名第二个数字)。我试过子串和长度,但我无法做到。 感谢所有的帮助:)
答案 0 :(得分:1)
如果您确定字符串的格式是相同的,并且这两个整数将始终存在,我建议您使用String.Split与char分隔符' '
和linq as如下(附说明):
int[] integersInMyString = myString .Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries) //splitting all the parts by a space (' ') and removing empty entries
.Where(part => int.TryParse(part, out value)) //selecting only the substrnigs that are integers
.Select(int.Parse) //converting the strings to integers, since we've already filtered the ones we can convert
.ToArray(); //getting the array from the ienumerable
之后你可以使用:
简单地访问你需要的两个整数integersInMyString[0] //the first integer a.k.a the quantity
integersInMyString[1] //the first integer a.k.a the individual price
根据产品名称,我无法理解究竟应该是哪一部分。如果您知道名称后面的内容,可以使用String.Substring String.IndexOf的实例。
你可以更具体地回答你的问题,我很乐意帮助你。
P.S。:没有足够的声誉评论,请原谅我,如果我的答案不够好......试着帮忙!
答案 1 :(得分:0)
用这段代码和每个循环一起完成它。谢谢你的帮助人:)
tests