0

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.

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by shahid00704
0

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.