public static class FieldLength
public static int FirstName {get; set;} = 15;
public static int LastName {get; set;} = 15;
public static void Main()
string imie = "andrzejus waldemar bąk";
var bb = SplitByFullWordsIntoFirstAndLastName(imie);
Console.WriteLine(bb.firstNameSqueezed);
Console.WriteLine(bb.lastNameSqueezed);
public static (string firstNameSqueezed, string lastNameSqueezed) SqueezeFirstAndLastName(string firstName, string lastName)
if (firstName == null && lastName == null)
firstName = string.Empty;
LengthStatus status = SetLengthStatus(firstName, lastName);
Console.WriteLine(status.ToString());
case LengthStatus.BOTH_OK:
return (firstName, lastName);
case LengthStatus.FirstName_TOO_LONG:
int toCopy = FieldLength.LastName - lastName.Length - 1;
Console.WriteLine($"toCopy {toCopy}");
int toCut = firstName.Length - FieldLength.FirstName > toCopy ? toCopy : firstName.Length - FieldLength.FirstName;
Console.WriteLine($"toCut {toCut}");
string firstNamePart = firstName.Substring(FieldLength.FirstName, toCut );
Console.WriteLine($"firstNamePart {firstNamePart}");
string resultLastName = $"{firstNamePart} {lastName}";
string resultFirstName = firstName.Substring(0, FieldLength.FirstName);
return (resultFirstName, resultLastName);
case LengthStatus.LastName_TOO_LONG:
int toCopy = FieldLength.FirstName - firstName.Length - 1;
string lastNamePart = lastName.Substring(0, toCopy);
string resultFirstName = $"{firstName} {lastNamePart}";
int toCut = lastName.Length - toCopy > FieldLength.LastName ? FieldLength.LastName : lastName.Length - toCopy;
string resultLastName = lastName.Substring(lastNamePart.Length, toCut);
return (resultFirstName, resultLastName);
case LengthStatus.BOTH_TOO_LONG:
return (firstName.Substring(0,FieldLength.FirstName), lastName.Substring(0,FieldLength.LastName));
public static (string firstNameSqueezed, string lastNameSqueezed) SplitIntoFirstAndLastName(string input)
if (input.Length <= FieldLength.FirstName)
return (input, string.Empty);
string firstPart = input.Substring(0, FieldLength.FirstName);
Console.WriteLine($"firstPart {firstPart}");
int toCut = input.Length - firstPart.Length >= FieldLength.LastName ? FieldLength.FirstName : input.Length - firstPart.Length;
Console.WriteLine($"toCut {toCut}");
string secoundPart = input.Substring(FieldLength.FirstName, toCut);
return (firstPart, secoundPart);
public static (string firstNameSqueezed, string lastNameSqueezed) SplitByFullWordsIntoFirstAndLastName(string input)
if (input.Length <= FieldLength.FirstName)
return (input, string.Empty);
string[] words = input.Split(' ');
StringBuilder first = new StringBuilder();
StringBuilder last = new StringBuilder();
Console.WriteLine(first);
bool firstNameFull = false;
foreach (var word in words)
if (word.Length <= FieldLength.FirstName - first.Length && !firstNameFull)
if (first.Length <= FieldLength.FirstName - 1)
if (last.Length <= FieldLength.LastName - 1)
return (first.ToString(), last.ToString().Substring(0, last.Length >= FieldLength.LastName ? FieldLength.LastName : last.Length));
private static LengthStatus SetLengthStatus(string firstName, string lastName)
if (firstName.Length <= FieldLength.FirstName && lastName.Length <= FieldLength.LastName)
return LengthStatus.BOTH_OK;
if ((firstName.Length > FieldLength.FirstName && lastName.Length >= FieldLength.LastName) ||
(firstName.Length >= FieldLength.FirstName && lastName.Length > FieldLength.LastName))
return LengthStatus.BOTH_TOO_LONG;
if (firstName.Length > FieldLength.FirstName)
return LengthStatus.FirstName_TOO_LONG;
return LengthStatus.LastName_TOO_LONG;