Reputation: 39
So I'm going to try to explain myself better than my other post. I have two example sentences:
NEIGHBORHOOD X ADIDAS CONSORTIUM SUPERSTAR 80 10TH ANNIVERSARY -- BLACKGREY 21
and
ADIDAS STAN SMITH - RED 22
I need a code to get out the product name , the quantity (aka 1st number) and the individual price (aka 2nd number). I tried with substring and length but I couldnt make it. Thanks for all the help :)
Upvotes: 0
Views: 50
Reputation: 39
Done it with this snippet of code together with a for each loop.Thank you for the help man :)
string[] caracteresnastring = item.Split(new char[] { ',' }.ToArray());
string code = caracteresnastring[0];
string name = caracteresnastring[1];
string price = caracteresnastring[2];
string quantity = caracteresnastring[3];
Upvotes: 0
Reputation: 1167
If you're sure the format of your string will alwyas be the same and those two integers will always be there, I'd suggest you could use String.Split with char separator ' '
and linq as follows(with description):
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
After that you could simply access your two needed integers by using:
integersInMyString[0] //the first integer a.k.a the quantity
integersInMyString[1] //the first integer a.k.a the individual price
According to the product name, I couldn't understand which part exactly should it be. Maybe you could use String.Substring with an instance of String.IndexOf if you know what's coming right after the name.
You could be more specific with your question, I'll be glad to help you out.
P.S.: Not enough reputation to comment, excuse me if my answer is not good enough... Trying to help!
Upvotes: 1