LizG
LizG

Reputation: 2530

Splitting data from an array and pulling specific data

I think what i have to do is split and pull data to do the following:

I have created a class, with the following array and method getContactList. I need to create a method getFirstNames() and return all the first names from the address book into the variable firstNames in a Test class and display them on the console.

class ContactList
{
    public String[] contactList = 
    { 
    "John, Smith, [email protected], (506) 555-1234", 
    "Frank, Sinatra, [email protected], (506) 696-1234", 
    "Joan, Rivers, [email protected], (506) 696-5678", 
    "Freddy, Mercury, [email protected], (506) 653-1234", 
    "Freddy, Kruger, [email protected], (506) 658-1234"
    };

       public String[] getContactList()
    {

        return contactList;
    }


    public String getLastNames()
    {
        string lastnames = "";

        return lastnames;
    }


}


class Program
{
    static void Main(string[] args)
    {
        ContactList firstNames = new ContactList();
        Console.WriteLine(firstNames.getFirstNames());

        Console.WriteLine();
        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();

    } 

Upvotes: 1

Views: 96

Answers (3)

Piotr Dajlido
Piotr Dajlido

Reputation: 2030

It's working I checked :) good luck with your development

using System.IO;
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{

    static void Main()
    {
        // Just to hold data
        string[] data = new String[]{"John, Smith, [email protected], (506) 555-1234","Frank, Sinatra, [email protected], (506) 696-1234","Joan, Rivers, [email protected], (506) 696-5678","Freddy, Mercury, [email protected], (506) 653-1234","Freddy, Kruger, [email protected], (506) 658-1234"};

        // Create new contact list object
        ContactList contacts = new ContactList(data);

        // Call our method
        contacts.PrintLastNames();

    }
}


public class ContactList
{
    // Declare properties
    // google C# getter setter
    private List<string> cList;
    public List<string> CList{
        get{ return cList; }
    }

    // Constructor
    public ContactList(string[] _contactList)
    {
        // When creating new instance, take array of contacts and put into list    
        this.cList = _contactList.ToList();
    }

    // This will print out the names
    public void PrintLastNames()
    {
        // Google lambda expression C# for more info on iteration
        // With each string in cList, split by comas and use the first element 
        this.cList.ForEach( x => Console.WriteLine(x.Split(',')[0]));

        // Use this for last names
        //this.cList.ForEach( x => Console.WriteLine(x.Split(',')[1]));
    }

    // This will return names in list
    public List<string> GetLastNames()
    {
        // Google List Collection Generic C# for more info
        List<string> namesList = new List<string>();
        this.cList.ForEach( x => namesList.Add( x.Split(',')[0] ));

        return namesList;
    }

}

Upvotes: 0

Failed Scientist
Failed Scientist

Reputation: 2027

Better design it this way:

class ContactList
{
string firstName, lastName, eMail, ContactNo;
//Properties (getters/setters for above attributes/fields
}

class ContactListHandler
{

public List<string> GetFirstNames(string[] contactsText)
{

List<string> stringList = new List<string>();

foreach (string s in contactsText)

{
var x = contactsText.split(',');
stringList.Add(x[0]);
}

return stringList;
}

//and other functions
}

Main()
{
ContactListHandler cHandler = new ContactListHandler();
List<string> contactsString = cHandler.GetFirstNames(//your string variable containing the contacts);

foreach(string s in contactsString)
{
Console.WriteLine(s);
}
}

Yes it has increased code but only for once, now you can use it in an Object-oriented way for any string,etc.

Upvotes: 0

Saverio Terracciano
Saverio Terracciano

Reputation: 3915

Yours isn't exactly the best approach, but to achieve what you want... (assuming you want to separate the names by , )

public string getFirstNames(){
   StringBuilder sb=new StringBuilder();
   foreach(var x in contactList){
       var tmp=x.Split(',');
       sb.Append(tmp[0]);
       sb.Append(",");
   }
   return sb.ToString();
}

Upvotes: 1

Related Questions