I separated “A320-789-890” according to “-” and i get a list below:
"A101C", "B7CL", "E7CL", "D7CL"
Everything is ok. My result set above it is my solution result. But i have 2 question:
- how can I do that with regex?
- if I can do that with regex, can I use regex with linq?
-
which is more effective according to performance like my method below, or regex?
namespace engRegex1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}private void Form1_Load(object sender, EventArgs e) { Engineering eng = new Engineering(); string[] engSplitList = new string[eng.engList.Count()]; List<string> firstitem = new List<string>(); foreach (string item in eng.engList) { engSplitList = item.Split('-'); firstitem.Add(engSplitList[0]); } foreach (string item in firstitem) listBox1.Items.Add(item); } } public class Engineering { public List<string> engList = new List<string>() { "A101C-234-456", "B7CL-567-789", "E7CL-567-789", "D7CL-567-789" }; }}
As long as you need to extract just the first part I’m suggesting IndexOf + Subsstring:
It would be the fastest (and probably the easiest) way.
//EDIT
with LINQ it would something like that:
with RegEX
with RegEX and LINQ: