English 中文(简体)
从清单箱项目分离
原标题:splitting string from listbox items
  • 时间:2011-05-18 14:30:29
  •  标签:
  • c#
  • listbox

i am listbox to store different strings which user gives as input. but i want to split those listbox items where i want to have the first word of every item as seperate string and rest as other string. i am iterating the listbox item as

foreach (ListItem item in lstboxColumnList.Items)
            {

                column_name = temp + "" "+item+""";
                temp = column_name + "," + Environment.NewLine;
            }

如何使分裂的扼杀

最佳回答

假设“追捕者”一词用一个空间结束,你可以使用以下一些东西:

string firsWord = sentence.SubString(0, sentence.IndexOf(   ));
string remainingSentence = sentence.SubString(sentence.IndexOf(   ), sentence.Length);
问题回答

I dont know your listbox item s format.. but I assumed that your listbox item have at least 2 word and separate by a space.. so, you can do the splitting using substring and index of..

string first = sentence.SubString(0, sentence.IndexOf(" "));
string second = sentence.SubString(sentence.IndexOf(" ") + 1);
    public void Test()
    {
        List<string> source = new List<string> {
            "key1 some data",
            "key2 some more data",
            "key3 yada..."};
        Dictionary<string, string> resultDictionary = source.ToDictionary(n => n.Split(   ).First, n => n.Substring(n.IndexOf(   )));
        List<string> resultStrings = source.Select(n => string.Format(""{0}",{1}", n.Split(   ).First, n.Substring(n.IndexOf(   )))).ToList;
    }

resultDictionary is a dictionary with the key set to the first word of each string in the source list.

第二点更接近贵问题的要求,即它按照你具体规定的格式编制了一份说明清单。

EDIT: 道歉,首次在VB公布。

检查:

 var parts = lstboxColumnList.Items.OfType<ListItem>().Select(i => new { 
                          Part1 = i.Text.Split(   ).FirstOrDefault(),
                          Part2 = i.Text.Substring(i.Text.IndexOf(   ))
                       });

 foreach (var part in parts)
 {
     var p1 = part.Part1;
     var p2 = part.Part2;

     // TODO: use p1, p2 in magic code!!
 }




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签