如何在 C# 中解析这种输出
How to parse this kind of output in c#
我有以下格式的输出
Image Name PID Services
========================= ======== ============================================
System Idle Process 0 N/A
services.exe 436 N/A
svchost.exe 500 BrokerInfrastructure, DcomLaunch, LSM,
PlugPlay, Power, SystemEventsBroker
vnetd.exe 18504 NetBackup Legacy Network Service
我想将输出存储在这样的数组中:
ar[0]=System Idle Process
ar[1]=0
ar[2]=N/A
我尝试根据空格拆分字符串,但没有成功。谁能建议如何拆分它并在 c#
中获得所需的输出
use substring on the basis of padding
formatedop[0] = item.Substring(0, 25);
formatedop[1] = item.Substring(25, 10);
formatedop[2] = item.Substring(35, 40);
it will give the result
看起来您收到的信息具有固定宽度输出,因此,您可以使用 string.Substring
每次都根据需要获取字符串的一部分。
您可以像这样阅读输入中的项目:
public static IEnumerable<ProcessItem> GetItemsFromText(string text) {
var lines = text.Split( new [] { "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries );
ProcessItem current = null;
Console.WriteLine( "Lines found: {0}", lines.Length );
// skip first 2 lines (header and = signs)
for (int i = 2; i < lines.Length; i++) {
var name = lines[i].Substring( 0, 25 ).Trim();
var pid = lines[i].Substring( 26, 8 ).Trim();
var services = lines[i].Substring( 35 ).Trim();
if (!string.IsNullOrWhiteSpace( name ) ) {
if (current != null) {
yield return current;
}
current = new ProcessItem {
Name = name,
Pid = pid,
Services = services
};
} else {
current.Services += ' ' + services;
}
}
if (current != null) {
yield return current;
}
}
此版本还会确认您有多个订单项,并会发回自定义 class ProcessItem
,如下所示
public class ProcessItem {
public string Name { get; set; }
public string Pid { get;set; }
public string Services { get;set; }
}
您可以在 .netfiddle
中找到代码的 运行 版本
我有以下格式的输出
Image Name PID Services
========================= ======== ============================================
System Idle Process 0 N/A
services.exe 436 N/A
svchost.exe 500 BrokerInfrastructure, DcomLaunch, LSM,
PlugPlay, Power, SystemEventsBroker
vnetd.exe 18504 NetBackup Legacy Network Service
我想将输出存储在这样的数组中:
ar[0]=System Idle Process
ar[1]=0
ar[2]=N/A
我尝试根据空格拆分字符串,但没有成功。谁能建议如何拆分它并在 c#
中获得所需的输出use substring on the basis of padding
formatedop[0] = item.Substring(0, 25);
formatedop[1] = item.Substring(25, 10);
formatedop[2] = item.Substring(35, 40);
it will give the result
看起来您收到的信息具有固定宽度输出,因此,您可以使用 string.Substring
每次都根据需要获取字符串的一部分。
您可以像这样阅读输入中的项目:
public static IEnumerable<ProcessItem> GetItemsFromText(string text) {
var lines = text.Split( new [] { "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries );
ProcessItem current = null;
Console.WriteLine( "Lines found: {0}", lines.Length );
// skip first 2 lines (header and = signs)
for (int i = 2; i < lines.Length; i++) {
var name = lines[i].Substring( 0, 25 ).Trim();
var pid = lines[i].Substring( 26, 8 ).Trim();
var services = lines[i].Substring( 35 ).Trim();
if (!string.IsNullOrWhiteSpace( name ) ) {
if (current != null) {
yield return current;
}
current = new ProcessItem {
Name = name,
Pid = pid,
Services = services
};
} else {
current.Services += ' ' + services;
}
}
if (current != null) {
yield return current;
}
}
此版本还会确认您有多个订单项,并会发回自定义 class ProcessItem
,如下所示
public class ProcessItem {
public string Name { get; set; }
public string Pid { get;set; }
public string Services { get;set; }
}
您可以在 .netfiddle
中找到代码的 运行 版本