Hi i have a string array, such as:

string[] test = {"one@test.com","@test.com","@contoso.com"}

And I want to modify values only where the address has no first portion that is "@domain.com", so the example return would be:

string[] test2 = {"one@test.com","*@test.com","*@contoso.com"}

ANy ideas how to do this please?

This is the code which populates the textBox with only wanted items (the ones which dont have the name infront of sing @):

private void PopulatingListView()
        {
            string[] array = new string[] { "one@test.com", "@test.com", "@contoso.com" };
            for (int i = 0; i < array.Length; i++)
            {
                string value = array[i];
                string[] seperate = value.Split(new string[] { "@" }, StringSplitOptions.None);
                if (seperate[0] == "")
                    this.listBox1.Items.Add(seperate[1]);
            }
        }

I did a bit more of a code, to show you how its done.
You only have to put these controls on your form:
- 2 listboxes
- 1 textBox
- 1 button

Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Nov28Exercise
{
    public partial class Form1 : Form
    {
        List<string> list;
        public Form1()
        {
            InitializeComponent();
            list = new List<string>();
            PopulatingTextBox1();
        }

        private void PopulatingTextBox1()
        {
            string[] array = new string[] { "one@test.com", "@test.com", "@contoso.com" };
            for (int i = 0; i < array.Length; i++)
            {
                string value = array[i];
                string[] seperate = value.Split(new string[] { "@" }, StringSplitOptions.None);
                if (seperate[0] == "")
                    this.listBox1.Items.Add(value);
            }
        }

        private void PopulatingTextBox2(string value)
        {
            bool bInserting = true;
            foreach (string item in list)
            {
                if (item == value)
                {
                    bInserting = false;
                    MessageBox.Show("The e-mail \"" + value + "\" is already in the list\nIt will not be inserted!");
                    break;
                }
            }
            if (bInserting)
            {
                this.listBox2.Items.Add(value);
                list.Add(value);
            }
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.listBox1.SelectedIndex > -1)
            {
                string value = this.listBox1.SelectedItem.ToString();
                this.textBox1.Text = value;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string value = this.textBox1.Text;
            string[] array = value.Split(new string[] { "@" }, StringSplitOptions.None);
            if (array[0] != "")
                PopulatingTextBox2(value);
            else
                MessageBox.Show("Please correct the e-mail address before saving it into the list.");
        }
    }
}

Edited 6 Years Ago by Mitja Bonca: EDIT: duplicated emails are not allowed to insert!

thanks for the reply, but i don't think that solves the problem. basically what i'm doing is that i have a console app, and i want to pass an array as a parameter for something, just by the variable name which should be fine.

All values of this array is basically an internet address, but some internet addresses have no first portion and only '@domain.com'. I want to loop through only and all the '@domain.com' format values and change them to '*@domain.com'.

Does that make sense?.

Thanks
Steve

Use the StartWith method of the string class.
Perhaps something like this?

class Program
    {
        static void Main(string[] args)
        {
            string[] test = {"one@test.com","@test.com","@contoso.com"}; // your string

            // make a new list of strings to contain all the strings + all the modifications
            List<string> changetest = new List<string>(); 

            // print out the initial array of strings
            foreach (string s in test)
                Console.WriteLine(s);

            Console.WriteLine();

            // now change the string if it starts with @
            foreach (string s in test)
                if (s.StartsWith("@"))
                    changetest.Add(s.Insert(0, "*")); // insert a *
                else
                    changetest.Add(s); // else just add


            // print out the modified list of strings
            foreach (string s in changetest)
                Console.WriteLine(s);
            
            Console.ReadKey();
        }
Comments
Solved :)

I had tried something similar to what you've mentioned before and had problems making a Replace after the StartsWith, or in your case a Add(s.Insert..) work correctly, it's strange but it doesn't seem to work. it can return a true so it passes through the if statement obvously but doesn't action it. i'll keep playing aroudn to figure it out. SHould be simple you know!

THanks

cool go it, i was being stupid, after modifying the value i was outputting the foreach results against the original string array and not the string list!

Thanks,duh!

thanks again, i do have one more question, sorry it is annoying. in simple terms i want to run a foreach statement with two arrays. in words it would be something like:

forach(string s in array01 and string d in array 02)
{
bla bla bla
}


Would LINQ sort this out best?

is there a good method for this sort of thing?

Thanks

foreach(string a in array1)
{
    foreach(string b in array2)
    {

    }
}

But it depends, why you will use this kind of array - its better to tell more about why do you need such arrays.
YOu have to know, that 1st string from 1st array, which is in value "a", will go throug all other array, and when will finsih the second array, code will come back to 1st array and selcet the 2nd value from 1sr array and this value will go again through all 2nd array, and so on...

Its better to tell more, or your code will get duplicated, or even worse.

Mitja

Edited 6 Years Ago by Mitja Bonca: n/a

So the problem is that i have a file, example in the text format of:

column01;column02;t01,t02,t03;s01,s02,s03

I want to run a read loop on this:

while (!sr.EndOfStream)
            {
                x++;
                strline = sr.ReadLine();
                _values = strline.Split(';');
                string separator = ",";
                string strcolumn01 = _values[0];
                string strcolumn02 = _values[1];
                string strcolumn03 = _values[2];
                string strcolumn04 = _values[3];

                string[] strcolum03data = strcolumn03.Split(separator.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                string[] strcolum04data = strcolumn04.Split(separator.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);


//WHat I want here is not just the above in a foreach, I essentially want to output, from the file values above the following:

//column01,column02,strcolumn03[0],strcolumn04[0] and then loop through them all, so next would be:

//column01,column02,strcolumn03[1],strcolumn04[1] etc etc

               


        }

I hoep this makese sense!

Thanks again

Now I understand what are you trying to do. I did a method (custom), and inside of it I created 2 Lists, into 1st go values from 1st array, into 2nd array go values from 2nd array!
This way you have all seperated, and you can do wtih them what ever yozu can.
here` the code:

private void Reading()
        {
            List<string> list1 = new List<string>();
            List<string> list2 = new List<string>();
            using (System.IO.StreamReader sr = new System.IO.StreamReader(@"writePathHere"))
            {
                while (!sr.EndOfStream)
                {
                    string line = sr.ReadLine();
                    string[] arrayLine = line.Split(';');
                    for (int i = 0; i < arrayLine.Length; i++)
                    {
                        string value = arrayLine[i];
                        list1.Add(value);
                        string[] arrayValue = value.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                        for (int j = 0; j < arrayValue.Length; j++)
                        {
                            string item = arrayValue[j];
                            list2.Add(item);
                        }
                    }
                }
            }
        }

Thanks, I just want to be able to read both arrays in the same function, without having to duplicate code. So I only need them as strings.

Thanks

Ok, into strings. I seperted them with comma. You can do it your way:

string list1 = null;
            string list2 = null;
            using (System.IO.StreamReader sr = new System.IO.StreamReader(@"writePathHere"))
            {
                while (!sr.EndOfStream)
                {
                    string line = sr.ReadLine();
                    string[] arrayLine = line.Split(';');
                    for (int i = 0; i < arrayLine.Length; i++)
                    {
                        string value = arrayLine[i];
                        if (i < arrayLine.Length)
                            list1 += value + ", ";
                        else
                            list1 += value;
                        string[] arrayValue = value.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                        for (int j = 0; j < arrayValue.Length; j++)
                        {
                            string item = arrayValue[j];
                            if (j < arrayValue.Length)
                                list2 += item + ", ";
                            else
                                list2 += item;
                        }
                    }
                }
            }

I am new to this site really and im sorry if i haven't explained it correctly. Maybe i'm don't think the above does it. ignoring the fact that i have arrays or strings, if have a csv file with the following line, note that columns are separated by ';' and multi-valued strings as i'd call them are separated by a ',' character:

one;two;00,01,02;10,11,12

So on each readline return i want to output as separate strings like this, with no dupication of them:

Line 1: one+","+two+","+00+","+10
Line 2: one+","+two+","+01+","+11
Line 3: one+","+two+","+02+","+12

WIth these string i'm then running a function to basically to some server side stuff. So i really have a messed up csv file that i can't resolve with excel. Does that make sense?

thank you

Edited 6 Years Ago by stevetaylor15: n/a

Anyway, if you want as a result to get these kind of results:
Line 1: one+","+two+","+00+","+10
Line 2: one+","+two+","+01+","+11
Line 3: one+","+two+","+02+","+12

we neeed to implement an array to put all the lines inside (a list<T> is the best option). Anyway, you can have a string, but you have to use it before the code goes back to read a new line (your quote: string i'm then running a function to basically to some server side).

But I still dont understand you well, what exactly are the meanings of one;two;00,01,02;10,11,12.
I will try to display your columns with values inside:

| Column1: | Column2 | Column3 | Column4 |
| ONE | Two | 00,01,02 | 10,11,12 |

So you want values from each column, and with only 1 value from each column (with no repeating if them).

And all these is in one ReadLine method (in one rows) -> | ONE | Two | 00,01,02 | 10,11,12 |

Am I right?

Edited 6 Years Ago by Mitja Bonca: re-edit the post

Now I tried and did my best from by understanding of what you would like to have. This is what I have to offer you:

using (System.IO.StreamReader sr = new System.IO.StreamReader(@"C:\1\test.txt"))
            {
                if (!sr.EndOfStream)
                {                    
                    string line = sr.ReadLine();
                    string[] arrayLine = line.Split(';');

                    string col1 = arrayLine[0];
                    string col2 = arrayLine[1];
                    string col3 = arrayLine[2];
                    string col4 = arrayLine[3];

                    string[] arrayCol3 = col3.Split(',');
                    string[] arrayCol4 = col4.Split(',');

                    int counter = 0;
                    string[] newArray = new string[arrayCol3.Length]; //just for testing, here you will have all 3 lines saved!

                    while (counter < arrayCol3.Length)
                    {
                        string value = col1 + "," + col2 + "," + arrayCol3[counter] + "," + arrayCol4[counter];
                        newArray[counter] = value;
                        counter++;
                    }
                }
            }

Thanks for the reply, this is exactly what i've been wanting to do! A while condition, duh, i feel stupid now.

thanks again, you're far too helpful, thanks.

This article has been dead for over six months. Start a new discussion instead.