Satya Satya - 3 years ago 147
C# Question

Extract and format a part of String in C#

I am wondering if someone could help me with either a regex or substring options.

I have a string:

Line1 ("Application", "People", "people DB", "PP01", 1, X, "Y")]

From the above, would like to extract and display :
PP01 - People

Answer Source

Assuming that all your lines are structured the same ..

Line1("Application", "People", "people DB", "PP01", 1, X, "Y")]
Line2("Application", "People", "people DB", "PP02", 2, X, "Y")]
Line3("Application", "People", "people DB", "PP03", 3, X, "Y")]

.. you could also make use of the string methods .Replace() and .Split():

string str = "\"Application\", \"People\", \"people DB\", \"PP01\", 1, X, \"Y\"";
string[] parts = str.Replace("\"", string.Empty)
                    .Split(new string[] { ", " }, StringSplitOptions.None);

Console.Write(parts[3] + " - " + parts[1]);

Output would be: PP01 - People

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download