views:

87

answers:

4

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:

  1. how can I do that with regex?
  2. if I can do that with regex, can I use regex with linq?
  3. 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" };
    }
    

    }

A: 

This is an roudabout way to do it, use AddRange instead, also bind the list to the listbox that way you would get a better performance and more compact code.

Judging from your level of expertise a regex is a bad fix for you, regex is hard and easy to do wrong, grab a book on regular expresions and then use.

Remember than compact notation is a tradeoff for readability

Regards

serguey123
@serguey123 - no need to be rude. BTW - I did not downvote you.
Oded
Very rude indeed.
Timwi
This is both rude and not that informative. No help given on how one should use AddRange (or even where) or on how to do the listbox binding. All round an answer that would have been better off not shared.
Chris
Oded I appreciate the candor, thanks, perhaps I used the wrong words, English as a language is a bit harsh sometimes (not my first language)However I feel that all that positive reinforcement does more harm than good. Bruises to your ego will build character and I'm tired of bad snippets of code and question that could be answered reading the documentation.I heard that this was a good place from people I respect so I decided to give it a try but to no avail. This website hates me, it thinks I'm a script and feeds me captchas non stop, so until I become a fat cylon, au revoir. :)
serguey123
Chris on the usefulness, I agree that is not that useful, but there where post that already posted code, (something I don't do) so I gave hints on alternatives on how to do this, vages I agree but I don't have that much time and thought that other will build on that (they did). I did not think anybody would need help with databinding, sorry about that.I personally disagree on a policy of feeding code to people because this only acomplishes a copy/paste (they don't learn at all) and it becomes a nightmare for the people who has to review the code before going to production.Regards
serguey123
+2  A: 
  1. You can use the static RegEx.Split method that takes two strings:

    string[] parts = RegEx.Split("A320-789-890", "-");

  2. You can use Linq with this. You can also use Linq with the results from string.Split - these two things are not really connected. What do you want to do with Linq?

  3. You will need to test according to your own situation. I suggest you do a micro benchmark with representative data.

I would add that string.Split is simple and optimized. If it works you should just use it instead of looking for other solutions. Fix it if it becomes a bottleneck and a problem - chances are that if you profile your application you will find that the issues are elsewhere.

Oded
Why has this been downvoted?
Oded
This is not a good way to do what the question poster asked, and in particular, it is not performance-efficient as requested. The `IndexOf`+`Substring` answer is closer to being the right answer.
Timwi
@Timwi - If you read the question, it is about: 1. Can RegEx be used? 2. Can it be used with linq? 3. Which is more performant - his way or RegEx. In no place is he asking for the best performing code possible.
Oded
@Oded: Normally I wouldn’t respond to this, but... WTF? You asked why your answer was downvoted. I can’t speak for others but I explained why *I* downvoted it. What’s the point in arguing?
Timwi
@Timwi: I can see Oded's point of view. He answered the questions (though I suspect Q3 probably does have a definite answer rather than a go and test) and got downvoted for it. His answer doesn't look wrong to me... Just thought I'd give an impartial view on it...
Chris
With a sufficiently complicated regular expression, the answer given becomes more efficient and more readable. Not the best solution to the example given, but good for someone to learn, as long as they also learn why it isn't the best case here.
Jon Hanna
+1  A: 

As long as you need to extract just the first part I'm suggesting IndexOf + Subsstring:

foreach (string item in eng.engList)
{
    listBox1.Items.Add(item.Subsstring(0, item.IndexOf('-'));
}

It would be the fastest (and probably the easiest) way.

//EDIT
with LINQ it would something like that:

listBox1.Items.AddRange(from item in eng.engList select item.Subsstring(0, item.IndexOf('-')));

with RegEX

foreach (string item in eng.engList)
{
    listBox1.Items.Add(RegEx.Match(item, "[^-]*").ToString());
}

with RegEX and LINQ:

listBox1.Items.AddRange(from item in eng.engList select RegEx.Match(item, "[^-]*").ToString())
adf88
+1  A: 

You could also just use Linq and split to do what you want...

  var result = from s in engList
               let first =  s.Split('-').First()
               select first;
Tim Jarvis