Home > other >  Turn a string into an array of words with C# [duplicate]
Turn a string into an array of words with C# [duplicate]

Time:10-02

I've been tasked with making a program that downloads prices for various tools that our company uses. The data is a simple JSON format with the tool number, price, etc. Usually looks like this:

"ReferenceItemId":"T0430",
"BaseUnitPrice":59.100000000,

With the tool number being 0430 and the price being $59.10. My program makes an entry in our database with the tool number and price. I've got it working on a basic level, but luckily for me, our tool supplier sometimes adds multiple tools for one entry.

"ReferenceItemId":"IMC63504 T0603 T0885 T3603 T0942 T3604 T3605 T6007 T1082 T0703",
"BaseUnitPrice":48.710000000,

So I need to figure out how to split this string up into an array. Once I have that I can make an entry in our database for each tool in the array. Also, the first word is garbage, so I need to delete that, and it isn't a consistent length (or removing it would be easy!). Any advice is appreciated! Thanks.

CodePudding user response:

You can use String.Split method like this

string s = "You win some. You lose some.";

string[] subs = s.Split(' ');

foreach (var sub in subs)
{
    Console.WriteLine($"Substring: {sub}");
}

// This example produces the following output:
//
// Substring: You
// Substring: win
// Substring: some.
// Substring: You
// Substring: lose
// Substring: some.
  • Related