Home > Back-end >  String text split
String text split

Time:01-10

I have to split this: string text = "John.Davidson/Belgrade Michael.Barton/Krakow Ivan.Perkinson/Moscow".

A logic must be created that will separately extract data from this record:

first name; last name; place of birth.

In other words, the displayed String must be edited using the method of the String class and each person's data must be extracted separately. The main method to use is to classify Strings on multiple parts.

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

string[] textArray = text.Split('"', ' ');
Console.WriteLine("Date: ");

foreach (string str in textArray)
{
    for (int i = 0; i < textArray.Length; i  )
    {
        string[] FirstName = textArray[i].Split(' ');                  
        string[] LastName = textArray[i].Split('.');
        string[] BirthPlace = textArray[i].Split('/');
        Console.WriteLine($"First name: {FirstName} Last Name: {LastName} BirthPlace: {BirthPlace}");
    }
}

CodePudding user response:

using System.Text.RegularExpressions;

string pattern = @" *(?<FirstName>\w )\.(?<LastName>\w )/(?<BirthPlace>\w )";
string input = " John.Davidson/Belgrade Michael.Barton/Krakow Ivan.Perkinson/Moscow";
MatchCollection m = Regex.Matches(input, pattern, RegexOptions.IgnoreCase);
foreach (Match match in m)
{
    Console.WriteLine($"First name: {match.Groups["FirstName"]} Last Name: {match.Groups["LastName"]} BirthPlace: {match.Groups["BirthPlace"]}");
}

CodePudding user response:

You're not using the split method properly. Also would be nice to structure the parsing in a Try method which handles errors also. Here's an example:

static bool TryParseRecord(string record, out string firstName, out string lastName, out string birthPlace)
{
    firstName = lastName = birthPlace = "";
    record = record.Trim();

    if (string.IsNullOrEmpty(record))
    {
        return false;
    }

    // parse for birth place
    var stringSplit = record.Split('/');
    if (stringSplit.Length == 2)
    {
        record = stringSplit[0];
        birthPlace = stringSplit[1];
    }
    else
    {
        return false;
    }

    // parse for names
    stringSplit = record.Split('.');
    if (stringSplit.Length == 2)
    {
        firstName = stringSplit[0];
        lastName = stringSplit[1];
    }
    else
    {
        return false;
    }

    return !string.IsNullOrEmpty(birthPlace) && !string.IsNullOrEmpty(firstName) && !string.IsNullOrEmpty(lastName);
}

With this, your main would become something like:

static void Main()
{
    var text = " John.Davidson/Belgrade Michael.Barton/Krakow Ivan.Perkinson/Moscow";

    var textArray = text.Split(' ');
    Console.WriteLine("Date: ");

    foreach (var entry in textArray)
    {
        if (TryParseRecord(entry, out var FirstName, out var LastName, out var BirthPlace))
        {
            Console.WriteLine($"First name: {FirstName}, Last Name: {LastName}, Birth place: {BirthPlace}");
        }
    }
}

CodePudding user response:

For some reason you were iterating your array twice.

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

    string[] textArray = text.Trim().Split(" ");
    Console.WriteLine($"Date: {DateTime.Now} ");


    String[] delimiters = { ".", "/" };


    for (int i = 0; i < textArray.Length; i  )
    {
        String[] parts = textArray[i].Split(delimiters,StringSplitOptions.None);
                
        Console.WriteLine($"First name: {parts[0]} Last Name: {parts[1]} BirthPlace: {parts[2]}");
    }
}

CodePudding user response:

we do it in python like this:

text = " John.Davidson/Belgrade Michael.Barton/Krakow Ivan.Perkinson/Moscow"
for record in text.split(" "):
    if record != "":
        temp, city = record.split("/")
        name, surname = temp.split(".")
        print(f"First name: {name} Last Name: {surname} BirthPlace: {city}")
  • Related