拆分列表框项目和拆分没有选择的项目
Split listbox items and split without selected item
为了解释我想要做什么,下面是我的 listbox
中可能包含的内容的示例(文本的三个列表项):
listbox
----------------------
| hello my friends |
| how r u today? |
| i'm here |
----------------------
我想将我的 listbox
项(在有 space 的地方拆分)分成 2 个数组。第一个数组将是我的 selected 项目(假设我们 select "hello my friends",这只是一个例子;也许第二个或第三个项目可以 selected)拆分和第二个数组将是我的 unselected items 数组。像这样;
string[] firstArray = {"hello", "my", "friends"}
string[] secondArray = {"how", "r", "u", "today?", "i'm", "here"}
但我不知道我该怎么做...
这是我的代码:
string[] LBI = lb2.Items.OfType<string>().ToArray();
string[] selectedItemSplit=lb2.SelectedItem.ToString().Split(' ');
string jo = string.Join(" ", LBI);
string[] sp = jo.Split(new char[] { ' ' });
感谢您的回答...
您可以使用 lb2.SelectedItem
抓取所选项目并将其拆分,然后取出其余项目(通过使用 [ 过滤掉索引为 lb2.SelectedIndex
的项目 Where
子句),然后对结果执行 SelectMany
,将每个结果拆分为 space 字符:
var nonSelected = lb2.Items.OfType<string>()
.Where((item, index) => index != lb2.SelectedIndex);
var first = lb2.SelectedItem.ToString().Split(' ');
var rest = nonSelected.SelectMany(others => others.Split(' ')).ToArray();
- 确认至少有一项被选中,以避免出现异常。
- 在第一个数组中插入当前选定的 ListBox 项的内容,使用 String.Split() 拆分它(因为我们在白色 space 上拆分,不需要指定分隔符:它是默认值)。
- 取所有未选中的Items(
.Where
the Item index is not the current) and use SelectMany
将每个Item的内容拆分生成的数组展平。
int currentIndex = listBox1.SelectedIndex;
if (currentIndex < 0) return;
string[] firstArray = listBox1.GetItemText(listBox1.Items[currentIndex]).Split();
string[] secondArray = listBox1.Items.OfType<string>().Where((item, idx) => idx != currentIndex)
.SelectMany(item => item.Split()).ToArray();
为了解释我想要做什么,下面是我的 listbox
中可能包含的内容的示例(文本的三个列表项):
listbox
----------------------
| hello my friends |
| how r u today? |
| i'm here |
----------------------
我想将我的 listbox
项(在有 space 的地方拆分)分成 2 个数组。第一个数组将是我的 selected 项目(假设我们 select "hello my friends",这只是一个例子;也许第二个或第三个项目可以 selected)拆分和第二个数组将是我的 unselected items 数组。像这样;
string[] firstArray = {"hello", "my", "friends"}
string[] secondArray = {"how", "r", "u", "today?", "i'm", "here"}
但我不知道我该怎么做... 这是我的代码:
string[] LBI = lb2.Items.OfType<string>().ToArray();
string[] selectedItemSplit=lb2.SelectedItem.ToString().Split(' ');
string jo = string.Join(" ", LBI);
string[] sp = jo.Split(new char[] { ' ' });
感谢您的回答...
您可以使用 lb2.SelectedItem
抓取所选项目并将其拆分,然后取出其余项目(通过使用 [ 过滤掉索引为 lb2.SelectedIndex
的项目 Where
子句),然后对结果执行 SelectMany
,将每个结果拆分为 space 字符:
var nonSelected = lb2.Items.OfType<string>()
.Where((item, index) => index != lb2.SelectedIndex);
var first = lb2.SelectedItem.ToString().Split(' ');
var rest = nonSelected.SelectMany(others => others.Split(' ')).ToArray();
- 确认至少有一项被选中,以避免出现异常。
- 在第一个数组中插入当前选定的 ListBox 项的内容,使用 String.Split() 拆分它(因为我们在白色 space 上拆分,不需要指定分隔符:它是默认值)。
- 取所有未选中的Items(
.Where
the Item index is not the current) and useSelectMany
将每个Item的内容拆分生成的数组展平。
int currentIndex = listBox1.SelectedIndex;
if (currentIndex < 0) return;
string[] firstArray = listBox1.GetItemText(listBox1.Items[currentIndex]).Split();
string[] secondArray = listBox1.Items.OfType<string>().Where((item, idx) => idx != currentIndex)
.SelectMany(item => item.Split()).ToArray();