0

My string :

enter image description here

it would be possible to split this string to two elements

first element

[email protected];

and second elments

 [email protected]; [email protected];

For insert

[email protected];

Into table_1 ?

and insert

 [email protected]; [email protected];

into table_2 ?

the number of element of part one will always be one

the number of elements of part two is variable, it could be one but also 10/100/500

I have tried this code without success

enter image description here

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        string value = "[email protected]; [email protected]; [email protected]";

        List<string> values = value.Split(';', StringSplitOptions.RemoveEmptyEntries).ToList();
        string Table1 = values[0];
        values.RemoveAt(0);
        string Table2 = string.Join(';', values).Trim();

        Console.WriteLine(Table1);
        Console.WriteLine(Table2);
    }
}

need more information?

4
  • 1
    Is this beautiful gif related to question or not? Commented May 14, 2020 at 13:48
  • @Sinatr if not necessary and not appreciated I can eliminate it, no problem. Commented May 14, 2020 at 13:50
  • I like the coloration of this code. but for some reason I can't select and copy past in order to craft an answer. We live in a time where screen shoot is easier that copy past, so I have to type it? ;) Commented May 14, 2020 at 14:29
  • Yes, please remove unnecessary parts of the question, they are just noise. See how to ask. Commented May 14, 2020 at 14:39

1 Answer 1

5

There is an overload of Split that takes a maximum number of substrings - just set that to 2:

value = "[email protected]; [email protected]; [email protected];"
string[] values = value.Split(new [] {';'}, 2)
first = values[0]; //[email protected]
rest = values[1];  //[email protected]; [email protected];

From there you can trim spaces and/or add semicolons as necessary.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.