String splitting in c#

Viewed 51

So here is the thing. I have assignment in c# which says that i have string:

string text = "John.Davidson/Belgrade Michael.Barton/Krakow Ivan.Perkinson/Moscow".

I need to split this string to get name, surname and city... and then i need to create 3 objects type Person. That 3 objects I need to put in 1 Person[ ] array, and in the end i need to go through that array and print this peoples info. Result should look like this:

John Davidson Belgrade

Michael Barton Krakow

Ivan Perkinson Moscow

I tried to create a code that would work but i couldn't finish. I succeeded to some point but i dont know how to finish. I don't know if i did it right but if i did i dont know how to connect this splitted string with class which i wrote. Please help.

Here is the code:

    {
        string text = "John.Davidson/Belgrade Michael.Barton/Krakow Ivan.Perkinson/Moscow ";
        string[] strings = text.Split(new char[] {'.','/',' '});
        foreach(string s in strings)
        {
            Console.WriteLine(s);
        }

    }
    class Person
    {      
        public string Name;
        public string Surname;
        public string City;
        public Person(string name, string surname, string city)
        {
            Name = name;
            Surname = surname;
            City = city;
        }
    }
4 Answers

I would not split it like you did. The rules seem to be pretty clear:

  • each person is separated by an empty space
  • the first- and last-names are separated from the city by /
  • the first- and last-names are separated by .

So you need three String.Splits and a List<Person> that you can fill:

string text = "John.Davidson/Belgrade Michael.Barton/Krakow Ivan.Perkinson/Moscow ";
string[] persons = text.Split(' ', StringSplitOptions.RemoveEmptyEntries); // you can also `Trim` the string first
List<Person> personList = new List<Person>(persons.Length);
foreach(string p in persons)
{
    string[] nameCity = p.Split('/');
    string[] names = nameCity[0].Split('.');
    personList.Add(new Person(names[0],names.ElementAtOrDefault(1),nameCity.ElementAtOrDefault(1)));
}

You can do it with simple steps.

var text = "John.Davidson/Belgrade Michael.Barton/Krakow Ivan.Perkinson/Moscow ";
var names = text.Split(' '); // first split persons
var persons = new List<Person>();
            
foreach(var name in names) // for each person
{
    var country = name.Substring(name.IndexOf('/') + 1); // cut country by /
    var nameArr = name.Substring(0, name.IndexOf('/')).Split('.'); // cut and split first name and last name
    
    persons.Add(new Person(nameArr[0], nameArr[1], country));
}

Here is your solution

Person[] personArray = new Person[3];
            string text = "John.Davidson/Belgrade Michael.Barton/Krakow Ivan.Perkinson/Moscow ";
            string[] persons = text.Split(new char[] { ' ' });
            int i = 0;
            foreach (string ss in persons)
            {
                if (!string.IsNullOrEmpty(ss))
                {
                    string[] detailofPerson = ss.Split(new char[] { '.', '/' });
                    Person person = new Person(detailofPerson[0], detailofPerson[1], detailofPerson[2]);
                    personArray[i] = person;
                    i++;
                    Console.WriteLine(ss);
                }
            }

I tell you this. If you went this way of splitting all at once, and you guarantee names with no spaces, missing cities, etc, use math

string text = "John.Davidson/Belgrade Michael.Barton/Krakow Ivan.Perkinson/Moscow ";
string[] people = text.Split(new char[] {'.','/',' '});
for(int i = 0; i < people.Length - 3; i += 3)
{
    Console.WriteLine(people[i]);
    Console.WriteLine(people[i + 1]);
    Console.WriteLine(people[i + 2]);
    Console.WriteLine("---------");

    var person = new Person(people[i], people[i + 1], people[i + 2]);
    . . . . . 
}

or use named tuple to test. Or just directly plunge people[x] into person

for(int i = 0; i < people.Length - 3; i += 3)
{
    (string Name, string LName, string City) tempPerson = (people[i], people[i + 1], people[i + 2]);
    Console.WriteLine(tempPerson); // test

    var person = new Person(tempPerson.Name, tempPerson.LName, tempPerson.City);

}
Related