拆分数组中的数据并提取特定数据

时间:2015-03-14 02:12:31

标签: c# string string-parsing

我认为我要做的就是分割和提取数据以执行以下操作:

我创建了一个类,使用以下数组和方法getContactList。我需要创建一个方法getFirstNames()并将地址簿中的所有名字返回到Test类中的变量firstNames并在控制台上显示它们。

class ContactList
{
    public String[] contactList = 
    { 
    "John, Smith, jsmith@nbcc.ca, (506) 555-1234", 
    "Frank, Sinatra, fsinatra@nbcc.ca, (506) 696-1234", 
    "Joan, Rivers, jrivers@nbcc.ca, (506) 696-5678", 
    "Freddy, Mercury, fmercury@nbcc.ca, (506) 653-1234", 
    "Freddy, Kruger, fkruger@nbcc.ca, (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();

    } 

3 个答案:

答案 0 :(得分:1)

你的并不是最好的方法,但要实现你想要的...... (假设您要将名称分开,)

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();
}

答案 1 :(得分:0)

更好地设计它:

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);
}
}

是的,它增加了代码但只有一次,现在你可以以面向对象的方式使用它来处理任何字符串等。

答案 2 :(得分:0)

它的工作我检查了:)祝你的发展好运

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, jsmith@nbcc.ca, (506) 555-1234","Frank, Sinatra, fsinatra@nbcc.ca, (506) 696-1234","Joan, Rivers, jrivers@nbcc.ca, (506) 696-5678","Freddy, Mercury, fmercury@nbcc.ca, (506) 653-1234","Freddy, Kruger, fkruger@nbcc.ca, (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;
    }

}