Reputation: 131
I'm switching from VB.NET to C#.
I'm trying to copy the clipboard content to a ListBox
.
I use the code below in VB.NET:
Dim getClipboard As String() = Split(Clipboard.GetText, vbNewLine)
lstTarget.Items.AddRange(getClipboard)
I couldn't find what to use to split "\n"
.
So far I was able to get clipboard text with
(Clipboard.GetText(TextDataFormat.Text)
I tried working with string[]
and List<string>
but I messed up with index or lengths and couldn't figure out what to do.
Upvotes: 1
Views: 3388
Reputation: 11340
You use .Split()
to split strings
Clipboard.GetText().Split('\n').ToList().ForEach(line => lstTarget.Items.Add(line));
or
lstTarget.AddRange(Clipboard.GetText().Split('\n');
Upvotes: 1
Reputation: 66449
You can split the text and add it directly to the list:
lstTarget.AddRange(Clipboard.GetText(TextDataFormat.Text)
.Split(new[] { Environment.NewLine },
StringSplitOptions.RemoveEmptyEntries));
Upvotes: 1
Reputation: 4567
string text = Clipboard.GetText(TextDataFormat.Text);
lstTarget.Items.AddRange(text.Split("\n")));
Upvotes: 1