string cmd = "Static hostname: gatewayIcon name: computer - desktopChassis: desktopMachine ID: 706ecaf18cea4c919eb515d0e73c7fe4Boot ID: c2f95d12ab334e57ac7e6503f7d0cd87Operating System: CentOS Linux 8(Core)CPE OS Name: cpe:/ o:centos: centos: 8Kernel: Linux 4.18.0 - 147.3.1.el8_1.x86_64Architecture: x86 - 64Thu Dec 30 15:31:24 EST 2021total used free shared buff / cache availableMem: 7887756 455084 4067136 427084 3365536 6746868Swap: 8183804 0 8183804Filesystem Size Used Avail Use % Mounted ondevtmpfs 3.8G 0 3.8G 0 % / devtmpfs 3.8G 84K 3.8G 1 % / dev / shmtmpfs 3.8G 417M 3.4G 11 % / runtmpfs 3.8G 0 3.8G 0 % / sys / fs / cgroup/ dev / mapper / cl - root 50G 3.2G 47G 7 % // dev / sda2 976M 188M 721M 21 % / boot/ dev / sda1 599M 6.8M 593M 2 % / boot / efi/ dev / mapper / cl - home 53G 12G 42G 22 % / hometmpfs 771M 200K 771M 1 % / run / user / 1000total 120G 15G 105G 13 % -procs---------- - memory------------ - swap------ - io---- - system--------cpu---- -r b swpd free buff cache si so bi bo in cs us sy id wa st2 0 0 4067108 4884 3360652 0 0 0 2 1 3 37 2 61 0 0";
diagtext = cmd;
split = Regex.Split(diagtext, @"%");
foreach (var item in split)
{
diskmem = split[10];
}
So I have this long string called cmd and I'm using regex to display a particular string next to the percentage % (sign). I was able to display this string: / run / user / 1000total 120G 15G 105G 13 %, but I only need the 13 not the strings before 13.
How can I do this?
diskmemat spaces and take the last one of that - something likediskmem.Split(" ").Last()with using System.Linq should work - yourforeach (var item in split)is superfluous btw. it just creates work without accomplishing anything different fiom the first time you dodiskmem = split[10];