我有一个班级" DataCls"从其他一些来源我得到"消息"字符串" propertyName:value"格式。
public class DataCls
{
public string Message { get; set; }
public string Priority { get; set; }
public DateTime Time { get; set; }
public string Tag { get; set; }
}
dataCls.Message = "[AE][1W] Message:Console Station is not available Priority:Info Time:Sep 21 2016 1:13PM Tag:/System Components/R431ESV/Stations/Console Stations/CStn01";
现在我需要拆分这个字符串并更新class" DataCls"的每个属性。具有以上价值。请建议!!!
我尝试过以下,但没有工作,
static void Main(string[] args)
{
DataCls dataCls = new DataCls();
dataCls.Message = "[AE][1W] Message:sample message Priority:Info Time:Sep 21 2016 1:13PM Tag:/abc/pqr/xyz";
dataCls.Message = dataCls.Message.Split(':')[1].ToString();
dataCls.Priority = dataCls.Message.Split(':')[2].ToString();//error
}
答案 0 :(得分:0)
您可以在DataCls中添加方法
public class DataCls
{
public string Message { get; set; }
public string Priority { get; set; }
public DateTime Time { get; set; }
public string Tag { get; set; }
public void ProcessMessage(string message)
{
var indexMessage = message.IndexOf("Message");
var indexPriority = message.IndexOf("Priority");
var indexTime = message.IndexOf("Time");
var indexTag = message.IndexOf("Tag");
this.Message = message.Substring(indexMessage + 8, indexPriority - indexMessage - 9);
this.Priority = message.Substring(indexPriority + 9, indexTime - indexPriority - 10);
var time = message.Substring(indexTime + 5, indexTag - indexTime - 6);
this.Time = DateTime.Parse(time);
this.Tag = message.Substring(indexTag + 4, message.Length - indexTag-4);
}
}
答案 1 :(得分:0)
您可以使用消息中的固定单词来获取它之间的部分:
static void Main()
{
DataCls ci = new DataCls();
ci.Parse( "[AE][1W] Message:Console Station is not available Priority:Info Time:Sep 21 2016 1:13PM Tag:/System Components/R431ESV/Stations/Console Stations/CStn01" );
}
public class DataCls
{
public string Message { get; set; }
public string Priority { get; set; }
public DateTime Time { get; set; }
public string Tag { get; set; }
public void Parse( string parseStr )
{
const string MESSAGE_MARKER = "Message:";
const string PRIORITY_MARKER = "Priority:";
const string TIME_MARKER = "Time:";
const string TAG_MARKER = "Tag:";
this.Message = GetTextPart( parseStr, MESSAGE_MARKER, PRIORITY_MARKER );
this.Priority = GetTextPart( parseStr, PRIORITY_MARKER, TIME_MARKER );
this.Time = DateTime.Parse( GetTextPart( parseStr, TIME_MARKER, TAG_MARKER ) );
this.Tag = GetTextPart( parseStr, TAG_MARKER, null );
}
private string GetTextPart( string text, string before, string after )
{
string result = null;
int posBefore = text.IndexOf( before );
if( after != null )
{
int posAfter = text.IndexOf( after );
result = text.Remove( posAfter ).Substring( posBefore + before.Length ).TrimEnd();
}
else
result = text.Substring( posBefore + before.Length );
return result;
}
}
答案 2 :(得分:0)
这是一个简单的技巧
Declare @Table table (ID int,Pt int,Title varchar(50))
Insert into @Table values (0,null,'Tags'),(1,0,'Transportation'),(2,1,'Boats'),(3,1,'Cars'),(4,1,'Planes'),(5,1,'Trains'),(6,0,'Technology'),(7,6,'FTP'),(8,6,'HTTP'),(9,0,'Finance'),(10,9,'FTP'),(11,9,'401K'),(12,2,'Sail'),(13,2,'Powered'),(14,6,'Internet'),(15,6,'Database'),(16,15,'SQL Server'),(17,15,'MySQL'),(18,15,'MS Access')
Declare @Top int = null --<< Sets top of Hier Try 9
Declare @Nest varchar(25) ='|-----' --<< Optional: Added for readability
;with cteHB as (
Select Seq = cast(1000+Row_Number() over (Order by Title) as varchar(500))
,ID
,Pt
,Lvl=1
,Title
From @Table
Where IsNull(@Top,-1) = case when @Top is null then isnull(Pt,-1) else ID end
Union All
Select Seq = cast(concat(cteHB.Seq,'.',1000+Row_Number() over (Order by cteCD.Title)) as varchar(500))
,cteCD.ID
,cteCD.Pt,cteHB.Lvl+1
,cteCD.Title
From @Table cteCD
Join cteHB on cteCD.Pt = cteHB.ID)
,cteR1 as (Select Seq,ID,R1=Row_Number() over (Order By Seq) From cteHB)
,cteR2 as (Select A.Seq,A.ID,R2=Max(B.R1) From cteR1 A Join cteR1 B on (B.Seq like A.Seq+'%') Group By A.Seq,A.ID )
Select B.R1
,C.R2
,A.ID
,A.Pt
,A.Lvl
,Title = Replicate(@Nest,A.Lvl-1) + A.Title
,A.Seq -- < Included for Illustration
From cteHB A
Join cteR1 B on A.ID=B.ID
Join cteR2 C on A.ID=C.ID
Order By B.R1
答案 3 :(得分:0)
一种圆形的方式,但它对我有用:
static void Main(string[] args)
{
DataCls dataCls = new DataCls();
string Message = "[AE][1W] Message:sample message Priority:Info Time:Sep 21 2016 1:13PM Tag:/abc/pqr/xyz";
dataCls.Message = Message.Substring(Message.IndexOf("Message:")+8);
dataCls.Message = dataCls.Message.Substring(0, dataCls.Message.LastIndexOf("Priority"));
dataCls.Priority = Message.Substring(Message.IndexOf("Priority:")+9);
dataCls.Priority = dataCls.Priority.Substring(0,dataCls.Priority.LastIndexOf("Time"));
string Time = Message.Substring(Message.IndexOf("Time"));
Time = Time.Substring(0, Time.LastIndexOf("Tag"));
dataCls.Time = Convert.ToDateTime(Time.Substring(Time.IndexOf(":")+1));
dataCls.Tag = Message.Substring(Message.IndexOf("Tag:")+4);
dataCls.Tag = dataCls.Tag.Substring(0);
}
答案 4 :(得分:-1)
您可以暂时将冒号更改为管道,拆分,然后替换
static void Main(string[] args)
{
DataCls dataCls = new DataCls();
dataCls.Message = "[AE][1W] Message:sample message Priority:Info Time:Sep 21 2016 1:13PM Tag:/abc/pqr/xyz";
dataCls.Message = dataCls.Message.Replace("me:",me|");
int index = dataCls.Message.IndexOf("Tag")-6;
dataCls.Message.SubString(index,1)="|";
string[] parts = dataCls.Message.Split(';');
dataCls.Message = parts[1];
dataCls.Priority = parts[3].Replace("|",":");
dataCls.Tag = parts[5];
}