C# 将文本拆分为长度为20个符号的4个字段
首先:我知道如何按长度分割字符串。任务不会仅按长度拆分文本C# 将文本拆分为长度为20个符号的4个字段,c#,C#,首先:我知道如何按长度分割字符串。任务不会仅按长度拆分文本 我有一个文本来自三个数据库列,我们称它们为数据字段: 公司名称 公司注册号又名VAT 公司地址 我需要通过收银机将这些数据打印到配方上。我有一些限制(由收银机软件/驱动程序设置): 我有4个字段(4行),用于打印客户信息 每个字段/行长度为20个符号 公司注册号始终为一个字,例如:AB0123456789 要将客户数据发送到收银机,我必须指定以下格式的文件: 1,“第1行”、“第2行”、“第3行”、“第4行”,0,0.00 以下是
我有一个文本来自三个数据库列,我们称它们为数据字段:
- 我有4个字段(4行),用于打印客户信息
- 每个字段/行长度为20个符号
- 公司注册号始终为一个字,例如:AB0123456789
1,“第1行”、“第2行”、“第3行”、“第4行”,0,0.00
以下是格式说明:
-客户编号/ID,整数,最多16个数字1
-字符串,每个最多20个符号,可以为空第1/2/3/4行
-折扣编号0
-客户 折扣(%)0.00
- 如果可能,不要拆分增值税编号
if
s来实现。但这很难看
示例数据:输入:[MyCompany LTD][AB0123456789][国家/地区地址不太长]
输出:
MyCompany有限公司
AB0123456789
没多久
国家/地区的地址
示例2:[我的投资房地产有限公司][AB0123456789][24街我的城市2233国家]
输出:
我的投资是正确的
领带有限公司
AB0123456789街
24 mycity 2233计数
如果拆分增值税编号,则可以匹配所有信息,但看起来很难看:
我的投资是正确的
领带有限公司AB012345678
mycity 24号街9号
2233国家/地区
如果可能,根本不要拆分增值税编号
我的变体:
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;
}
}
}
}
}
class客户
{
私有列表输出行=新列表();
private bool allFit=true;
专用字符串tmpLine=“”;
公共字符串第1行()
{
if(outputLines.Count>=1)返回outputLines[0];否则返回“”;
}
公共字符串行2()
{
if(outputLines.Count>=2)返回outputLines[1];否则返回“”;
}
公共字符串第3行()
{
if(outputLines.Count>=3)返回outputLines[2];否则返回“”;
}
公共字符串第4行()
{
if(outputLines.Count>=4)返回outputLines[3];否则返回“”;
}
公共客户()
{
}
公共客户(字符串名称、字符串增值税、字符串地址)
{
地址名称和增值税(名称、增值税);
地址;
}
公共图书馆
{
返回allFit;
}
公共无效地址(字符串文本)
{
如果(!allFit)
返回;
int fc=输出线。计数*20;
如果(fc==60)
{
fc-=20-输出线[2]。长度;
}
如果(fc80)
{
allFit=false;
返回;
}
fc=(80-fc)/20;//自由行
tmpLine=“”;
字符串[]k=text.Split(“”);
foreach(k中的字符串s)
{
如果((t采样线+s).长度0)
Add(tmpLine.Trim());
tmpLine=s+“”;
fc--;
}
}
如果(!outputLines[outputLines.Count-1]。包含(tmpLine))
{
输出线。添加(tmpLine);
}
如果(outputLines.Count>4)
{
allFit=false;
}
}
public void addname和vat(字符串名称,字符串vat)
{
如果((名称+增值税)。长度<20)
{
输出行。添加(名称+“”+增值税);
}
其他的
{
如果(name.Length以下是我的解决方案:
static void Main(string[] args)
{
string[] inputs = {
"[MyCompany LTD] [AB0123456789] [Not so long address in country]",
"[My investment properties LTD] [AB0123456789] [street 24 mycity 2233 country]"
};
foreach (string input in inputs)
{
Console.WriteLine("Parsing : '{0}'", input);
string[] splitArray = input.Split(new char[] { '[', ']' }, StringSplitOptions.RemoveEmptyEntries);
string remainder = "";
int index = 0;
string line = "";
do
{
if (index < splitArray.Length)
{
line = remainder + splitArray[index];
}
else
{
line = remainder;
}
remainder = string.Empty;
if (line.Trim().Length > 0)
{
if (line.Length > 20)
{
remainder = line.Substring(20);
line = line.Substring(0, 20);
}
Console.WriteLine("\t{0}", line);
}
if (index < splitArray.Length) index++;
} while ((index <= 4) && ((index < splitArray.Length) || (remainder.Length > 0)));
}
Console.ReadLine();
}
static void Main(字符串[]args)
{
字符串[]输入={
“[MyCompany LTD][AB0123456789][在国内地址不长]”,
“[My investment properties LTD][AB0123456789][mycity 2233 country 24号街]”
};
foreach(输入中的字符串输入)
{
WriteLine(“解析:{0}',输入);
string[]splitArray=input.Split(新字符[]{'[',']'},StringSplitOptions.RemoveEmptyEntries);
字符串余数=”;
int指数=0;
字符串行=”;
做
{
if(索引0)
{
如果(直线长度>20)
{
余数=行子串(20);
行=行。子字符串(0,20);
}
Console.WriteLine(“\t{0}”,第行);
}
if(index
有很多实用程序,比如,已经在这样做了。为一个配方创建一个类,并将所有数据放入该类。编写一个输出方法来写入所有属性。这是否回答了您的问题?@Josh,不。我想要一个更智能的“拆分器”,而不仅仅是按位置。当然可以,但是