根据特定要求拆分我的字符串

时间:2018-08-23 05:22:54

标签: c# parsing

我写了一个小的方法,可以从字符串中提取某些信息。这样的字符串的一个例子是

"Author: rajdra1 Subject: Sticky Note  Date: 09-08-2018 11:06:56" 

现在我需要从此字符串到我的字符串数组的某些信息,例如作者,主题,日期。

我的方法-

Public void SplitString()
{
  string input = "Author: rajdra1 Subject: Sticky Note  Date: 09-08-2018 
                  11:06:56";

  string[] result = input.Split(new string[] {": "}, 

StringSplitOptions.RemoveEmptyEntries);

  foreach (var value in result)
  {
    Console.WriteLine(value);
    Console.ReadLine();
  }
}

我得到的结果是:

{"rajdra1 Subject",  "Sticky Note  Date", "09-08-2018 11:06:56"}

但是我的预期输出是:

{"rajdra1", "Sticky Note", "09-08-2018 11:06:56"}

实际上,我是从.txt文件中获取此输入的,并且我总是会以以下格式输入

"Author: rajdra1 Subject: Sticky Note  Date: 09-08-2018 11:06:56"

我正在考虑将“作者”,“主题”和“日期”作为列标题 我想将预期的结果添加到特定的标题下。

2 个答案:

答案 0 :(得分:0)

可以使用正则表达式从该字符串中提取老师,主题和日期:

var myRegex = new Regex(@"Author:\s+(?<author>.*)\sSubject:\s+(?<subject>.*)\s+Date:\s+(?<date>.*)", RegexOptions.None);
var strTargetString = @"Author: rajdra1 Subject: Sticky Note  Date: 09-08-2018 11:06:56";

var str = new string[3];
foreach (Match myMatch in myRegex.Matches(strTargetString))
{
      str[0] = myMatch.Groups["author"].Value;
      str[1] = myMatch.Groups["subject"].Value;
      str[2] = myMatch.Groups["date"].Value;
}

答案 1 :(得分:0)

一种比较长但仍然相当简单和安全的方法是为要查找的每个部分明确搜索并拆分字符串:

var input = "Author: rajdra1 Subject: Sticky Note  Date: 09-08-2018 11:06:56" ;
// For the sake of example; this will also work:
//var input = "Author: Someone Else Subject: Another note.  Date: 09-08-2018 11:06:56" ;

var datePos = input.IndexOf("Date:");
// get: Date: 09-08-2018 11:06:56
var datePart = input.Substring(datePos);

var subjectPos = input.IndexOf("Subject:");
var subjectPartLength = datePos - subjectPos;
// get: Subject: Sticky Note  
var subjectPart = input.Substring(subjectPos, subjectPartLength);

var authorPos = 0;
var authorLength = subjectPos;
// get: Author: rajdra1
var authorPart = input.Substring(authorPos, authorLength);

// Add each of the above, but skipping over the first part of each:
var result = new string[]{
                    authorPart.Substring(8), 
                    subjectPart.Substring(9), 
                    datePart.Substring(6) 
              };

这假设每个“列标题”后面都有一个空格。如果那是错误的,只需更改最后一行中传递给Substring()的数字即可。