0

I need help with my code.
I have a ListBox which contains lines of text like this:

"hello my friends, how r u?","today is good","hey"
"I'm fine","and you","doing"
"have a nice day","thanks","man"

I want to remove sub-strings using SubString() (or another method, it doesn't matter) for this ListBox items.
I want to see this output in my ListBox (same ListBox, not a new) when I compile my code.

hello my friends, how r u?
I'm fine
have a nice day

Note: I wanted to share my code but I couldn't produce any, sorry.

3
  • 3
    I'm really trying to understand what you want to do, can you maybe elaborate a bit more and just try to post some code. Do you want to iterate through the ListBox? Commented Jan 11, 2019 at 7:43
  • The SubString() method doesn't do what I think you think it does. It takes a string, e.g "Hello World" and makes a separate string out of it with 2 indexes. So "Hello World".SubString(0, 3); would return the characters from index 0 up to but excluding index 3, in this case "Hel". See more here Commented Jan 11, 2019 at 7:46
  • I have a listbox and it has items like this: "hello my friends, how r u?","today is good","hey" "I'm fine","and you","doing" "have a nice day","thanks","man" But I want to see this output in my listbox when I compile to code: hello my friends, how r u? I'm fine have a nice day I want to obtain between to first and second quotes for each line. Commented Jan 11, 2019 at 7:49

1 Answer 1

1

Iterate the ListBox Items collection, split the resulting strings and take just the first element, trimming the now useless quotes in the end.

for (int item = 0; item < listBox1.Items.Count; item++)
{
    listBox1.Items[item] = listBox1.Items[item].ToString()
            .Split(new[] { "\",\"" }, StringSplitOptions.None)[0].TrimStart('"');
}

Or something like this:

int i = -1;
listBox1.Items.OfType<string>().ToList().ForEach((s) => {
    listBox1.Items[++i] = s.Split(new[] { "\",\"" }, StringSplitOptions.None)[0].TrimStart('"');
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.