First: I know how to split string by length. Task is not about to split text just by length.
I have a text who comes from three database columns, let's call them data fields:
- Company name
- company registration number aka VAT
- Company address
I need to print this data via cash register on recipe. I have some limitations (set by cash register software/driver):
- I have 4 fields (4 lines), to print customer info
- each field/line length is 20 symbols
- company registration number always is one word, example: AB0123456789
To send customer data to cash register, I must specify file in following format:
1,"row 1","row 2","row 3","row 4",0,0.00
Here is description of format:
1
- Customer number/ID, integer, max 16 numbersrow 1/2/3/4
- string, max 20 symbols each, can be empty0
- discount number0.00
- customer discount (%)
I must place text from database to four fields with some rules:
- if possible do not split VAT number
I tried various things to split all text to 4 fields, but not so successful.
There can be case when total data length is more than 80 symbols, in this case user can modify each field.
Maybe someone have some (better) suggestions how to split text... Of course this can be achieved with lot of if
s. But that is ugly.
Example data: input: [MyCompany LTD] [AB0123456789] [Not so long address in country] Output:
MyCompany LTD
AB0123456789
Not so long
address in country
Example 2: [My investment properties LTD] [AB0123456789] [street 24 mycity 2233 country] output:
My investment proper
ties LTD
AB0123456789 street
24 mycity 2233 count
If split VAT number, then it is possible to fit all information, but it looks ugly:
My investment proper
ties LTD AB012345678
9 street 24 mycity
2233 country
If possible, do not split vat number at all.
My variant:
class ChdCustomer
{
private List<string> outputLines = new List<string>();
private bool allFit = true;
private string tmpLine = "";
public string Line1()
{
if (outputLines.Count >= 1) return outputLines[0]; else return "";
}
public string Line2()
{
if (outputLines.Count >= 2) return outputLines[1]; else return "";
}
public string Line3()
{
if (outputLines.Count >= 3) return outputLines[2]; else return "";
}
public string Line4()
{
if (outputLines.Count >= 4) return outputLines[3]; else return "";
}
public ChdCustomer()
{
}
public ChdCustomer(string name, string vat, string address)
{
AddNameAndVat(name, vat);
AddAddress(address);
}
public bool FitAll()
{
return allFit;
}
public void AddAddress(string text)
{
if (!allFit)
return;
int fc = outputLines.Count * 20;
if (fc == 60)
{
fc -= 20 - outputLines[2].Length;
}
if (fc < text.Length || fc > 80)
{
allFit = false;
return;
}
fc = (80 - fc) / 20; // free lines
tmpLine = "";
string[] k = text.Split(' ');
foreach (string s in k)
{
if ((tmpLine + s).Length <= 20)
{
tmpLine += s + " ";
}
else
{
if (fc > 0)
outputLines.Add(tmpLine.Trim());
tmpLine = s + " ";
fc--;
}
}
if (!outputLines[outputLines.Count - 1].Contains(tmpLine))
{
outputLines.Add(tmpLine);
}
if (outputLines.Count > 4)
{
allFit = false;
}
}
public void AddNameAndVat(string name, string vat)
{
if ((name + vat).Length < 20)
{
outputLines.Add(name + " " + vat);
}
else
{
if (name.Length <= 20)
{
outputLines.Add(name);
outputLines.Add(vat);
}
else
{
if (name.Length <= 40)
{
string[] k = name.Split(' ');
foreach (string s in k)
{
if ((tmpLine + s).Length <= 20)
{
tmpLine += s + " ";
}
else
{
tmpLine = tmpLine.Trim();
string remaining = name.Substring(tmpLine.Length).Trim();
if (remaining.Length <= 20)
{
outputLines.Add(tmpLine);
outputLines.Add(remaining);
outputLines.Add(vat);
break;
}
else
{
outputLines.Add(name.Substring(0, 20));
outputLines.Add(name.Substring(20));
outputLines.Add(vat);
break;
}
}
}
}
else
{
allFit = false;
}
}
}
}
}