如何从每行删除三个以上的空格并在那里结束字符串以使用c#查看右边的行?
[Example1]
PO BOX XXX OVERDUE - PAY NOW
then transform to
PO BOX XXX
[Example2]
ClientB AMOUNT CARRI
then transform to
ClientB
[Example3]
PO BOX 400 FORWARD TO N
then transform to
PO BOX 400
答案 0 :(得分:7)
var firstColumn = origString.SubString(0, origString.IndexOf(" "));
答案 1 :(得分:4)
input = "PO BOX XXX OVERDUE - PAY NOW ";
input = input.Remove(input.IndexOf(" "));
indexOf paranthesis中有3个空格
如果您不知道是否有标签或空格,您可以进行拆分 -
input = input.Split(new char[] {' ', '\t'}, StringSplitOptions.RemoveEmptyEntries)[0];
答案 2 :(得分:2)
你可以这样做:
var input = new string[3] { "PO BOX XXX OVERDUE - PAY NOW ",
"ClientB AMOUNT CARRI",
"PO BOX 400 FORWARD TO N "
};
for (int x = 0, len = input.Length; x != len; x++)
{
input[x] = Regex.Replace(input[x], @"\s{3}[^\n]+", string.Empty);
}
//input is ["PO BOX XXX","ClientB","PO BOX 400"]
使用linq:
var output = input.Select(str => Regex.Replace(str, @"\s{3}[^\r\n]+$", string.Empty));
如果你从文件中读取这个字符串,你可以这样做:
var file = @"D:\file.txt";
var lines = File.ReadAllLines(file);
var output = lines.Select(str => Regex.Replace(str, @"\s{3}[^\n]+$", string.Empty)); // is ["PO BOX XXX","ClientB","PO BOX 400"]
答案 3 :(得分:1)
您可以使用string.Split方法生成字符串[]。根据数组计算,您可以获取所需的元素。
string base string = "PO BOX XXX OVERDUE - PAY NOW";
string[] delimittedStringArray = baseString.Split(' ');
if(delimittedStringArray.Length > 3)
{
// Take the data from array
}
else
{
// Do what ever
}
//我不确定if条件下是长度还是计数。