Jitesh Dammani Jitesh Dammani - 3 months ago 8
ASP.NET (C#) Question

Want a substring from a character till the end of the line

In the Sting, LOG, contains Status on the first line and Owned Group on the second line and Assigned Group on the third. So i want the sub-string to be After status till the end of the line. i.e. Assigned (1)

And they can be shuffled in order. Not necessary that the first one will be status, it can be owned group or assigned group.

string LOG = "Status: Assigned (1) \n Owned Group: Jitesh - poiuyt \n Assigned Group: Jitesh - QWERTY";

string tb = "Status: ";
string tb1 = "Assigned Group: ";

int lenght = tb.Length;
int a = LOG.IndexOf(tb) + lenght;

int lenght1 = tb1.Length;
int a1 = LOG.IndexOf(tb1) + lenght1;

var status = LOG.Substring(a,'\n');
var status1 = LOG.Substring(a1, '\n');


Expected Output :

status = Assigned (1)

status1 = Jitesh - QWERTY

Current Output :

status = Assigned (

status1 = Jitesh - Q

This is what i am expecting and this is what i am getting.

Answer

You can use Split twice:

  • with \n to get Status: Assigned (1) and Assigned Group: Jitesh - QWERTY, and then
  • with : and trim the values.

C# demo:

var LOG = "Status: Assigned (1) \n Assigned Group: Jitesh - QWERTY";
var LOG = "Status: Assigned (1) \n Owned Group: Jitesh - poiuyt \n Assigned Group: Jitesh - QWERTY";
var res = LOG.Split('\n')
    .Where(p => p.Trim().StartsWith("Status:") || p.Trim().StartsWith("Assigned Group:"))
    .Select(n => n.Split(':').LastOrDefault().Trim())
    .ToList();

var status = res[0];
var status1 = res[1];

You will have 2 values there, the first is status, the second one is status1.