hello, let's say I have a string which contains:

string s="111\n222\n333";

and I wanna add something like:

foreach(Line ln in s)
{
    if(ln="111")
        {
            MessageBox.Show("Found the 111 number");
        }
}

Can u help me do that "foreach line"?
thanks in advance :D

Here is one way.

using System;
using System.Linq;

namespace ForEach
{
    class Program
    {
        static void Main(string[] args)
        {
            const string s = "111\n222\n333";

            foreach (var line in s.Split('\n').ToArray())
            {
                if (line == "333")
                {
                    Console.WriteLine("Found the 333 number");
                }
            }

            Console.ReadKey();

        }
    }
}

aha thanks a lot :D that solved my problem

Youre welcome.

You don't actually need that ToArray as Split returns an array already, don't know why I put that on.

foreach (var line in s.Split('\n'))

ok can u help me delete an item from this:

string ss="item1\nitem2\nitem3";

how to convert that string into a listbox, then delete item2, then convert it back to a string?
I really need it that way, because I'm making a huge project and I'm stuck in the middle of it.. I have to convert it to a listbox first

The following should guide you with some ideas. Its is not a solution to your problem but an example of what you can do. It should be enough to get you going.

In terms of converting a string into a ListBox, you cannot. They are not compatible. Its like saying convert some paint into a car. You can paint a car with paint but you cannot turn one into the other.

You cannot turn a string into a ListBox. A ListBox is a comlex object with many properties, methods and events. You can however populate a ListBox with items in your string.

Which UI are you using - Winforms?

using System;
using System.Globalization;
using System.Linq;

namespace ForEach
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Test("111\n222\n333", "222");
                Test("111\n222\n333", "111");
                Test("111\n222\n333", "444");
                Test("111\t222\t333", "111");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);    
            }

            Console.ReadKey();
        }

        private static void Test(string s, string removeString)
        {
            var filteredString = RemoveItem(s, "222", '\n');

            foreach (var line in filteredString.Split('\n'))
            {
                if (line == "333")
                {
                    Console.WriteLine("Found the 333 number");
                }
            }
        }

        static string RemoveItem(string s, string removeString, char seperator)
        {
            if (!s.Contains(seperator))
            {
                throw new ArgumentException("The target string does not contain any instances of the supplied seperator");
            }

            if (String.IsNullOrEmpty(s))
            {
                return s;
            }

            var cleanedString = String.Join(seperator.ToString(CultureInfo.InvariantCulture), s.Split(seperator).Where(i => i != removeString));

            return cleanedString;
        }
    }
}

thanks a lot!
The removeitem is exactly what I wanted!
thank you very much sir :D

Youre welcome.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.