Hello,

I have three different string strings
string attach1,attach2,attach3;
string attachment="text1.txt,document.doc,text2.txt";

i want the above string should be split and each string after splitting it should store into three different strings

ex: attach1="text1.txt";
attach2="document.doc";
attach3="text2.text";

there should be a check how many string are coming after splitting the comma and then it should store into the attach1,attach2,attach3.

if
string attachment="text1.txt,document.doc";
here only 2 strings i will get after splitting...

then i must store it only in attach1 and attach2. since there is no string for attach3... it should be ignored.

please reply.

Hi,

You can do like this;

string attach1, attach2, attach3;
            string attachment = "text1.txt,document.doc,text2.txt";
            string[] strs = attachment.Split(',');

            switch (strs.Length)
            {
                case 1:
                    attach1 = strs[0];
                    break;
                case 2:
                    attach1 = strs[0];
                    attach2 = strs[1];
                    break;
                case 3:
                    attach1 = strs[0];
                    attach2 = strs[1];
                    attach3 = strs[2];
                    break;
            }

Instead of having individual variables like attach1, attach2, attach3 you can also have "arraylist" variable. So it can string n number of values in the string.

The above code can be rewritten if we use arraylist means,

string[] strs = attachment.Split(',');
            ArrayList alStr = new ArrayList();
            foreach (string ss in strs)
            {
                alStr.Add(ss);
            }

Good luck.

Thanks Sampath.. It worked fine for me.
Thanks Again.:)

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.