Home > Net >  c# StringSplitOptions.RemoveEmptyEntries not work
c# StringSplitOptions.RemoveEmptyEntries not work

Time:06-16

I want to split the namelist with spaces and remove the spaces but the space is not deleted.Also, random numbers always sound the same like in the photo.

form1.cs button click codes

            string[] cells;
            char[] ayraclar = { ' ' };
            foreach (string row in createdMailList.Items)
            {
                cells = row.Split(ayraclar, StringSplitOptions.RemoveEmptyEntries);
                string[] isimler = new string[cells.Length];

                for (int i = 0; i < cells.Length; i  )
                {
                    string[] chars = new string[cells[i].Length];
                    isimler[i] = cells[i].ToString();
                    listBox1.Items.Add(helper.anlamliMail(isimler));          

                }
            }

helper.cs class codes

    public static string anlamliMail(string[] items)
    {
        string kelime ="";
        var rnd = new Random();
        
        foreach (var row in items)
        {
            if (!string.IsNullOrEmpty(row))
            {
                kelime = items[rnd.Next(0, items.Length)]   rnd.Next(0, 9999);
            }
        }
        
        return kelime   "@yahoo.com";

    }

createdMailList content enter image description here

result : enter image description here

I split the list of names with spaces, but the space is coming. that's why there are only numbers of mails

CodePudding user response:

you have to create a random object once only , not each time when you call anlamliMail

               var rnd = new Random();
                for (int i = 0; i < cells.Length; i  )
                   isimler[i] = cells[i].ToString();
           
               foreach (var item in isimler)
                   listBox1.Items.Add(helper.anlamliMail(rnd,isimler));
                
           

and fix anlamliMai

public static string anlamliMail(Random rnd, string[] items)
    {
             string kelime ="";
              do
               {
               kelime =  items[rnd.Next(0, items.Length);
               }
               while (! string.IsNullOrEmpty(kelime));

        return kelime   rnd.Next(0, 9999).ToString()   "@yahoo.com";

    }
  •  Tags:  
  • c#
  • Related