0

I am new to C# but am working on a project at work. The task is to copy an array of values from one application to the clipboard and then to store those values in a list of objects in C#. I have created private properties for each field represented by the columns in the copied data, but i cannot figure out how to parse the data from the clipboard and store it in each property. Hopefully this makes sense. I'm having trouble even figuring out where to begin despite extensive searching. Any help would be greatly appreciated.

Rob Yocum
  • 15
  • 5
  • It sounds like you could use the `String.Split` method: https://msdn.microsoft.com/en-us/library/system.string.split(v=vs.110).aspx . Could you give an example of the data from the clipboard and the class you want to copy it into? – Jack A. Sep 18 '15 at 00:05
  • @JackA. Thanks. The clipboard data is just several columns and several hundred rows of data. The class so far is very simple - it just has properties representing each column. – Rob Yocum Sep 18 '15 at 14:58
  • Hit enter too soon... I would post the data, but much of the information is sensitive. I can successfully break down the clipboard contents using string.split (thanks), but am unsure how to populate a list with each row of the clipboard data representing an object. – Rob Yocum Sep 18 '15 at 15:03

2 Answers2

2

As suggested by Jack A. in the comments, use String.Split().

Something like...

        if (Clipboard.ContainsText())
        {
            string str = Clipboard.GetText();
            string[] values = str.Split("\t".ToCharArray());
            // ... do something with "values" ...
            xxx.aaa = values[0];
            xxx.bbb = values[1];
            xxx.ccc = values[2];
            // etc...
        }
        else
        {
            MessageBox.Show("No Text in the Clipboard!");
        }

You should probably check to make sure the right number of columns were present after splitting, though:

        if (values.Length == 5)
        {
            // ... do something with "values" ...
        }
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
0

I used this recently in another project: http://joshclose.github.io/CsvHelper/ Although you aren't using a CSV per se, you are using delimited data and it allows you to specify the delimeter. If you convert your text into a text reader similar to this post: In C#, how can I create a TextReader object from a string (without writing to disk), it will easily transfer the data to an object of your choosing once you have mapped it