I have a listbox that has contents that look like this:
C44 EXCLUDES 237.910 193.469 0 0603_5
C45 EXCLUDES 244.102 193.387 0 0603_5
R47 EXCLUDES 226.935 179.519 90 0402_2
C18 CAP-00129G 230.960 190.619 0 0402_4
C17 CAP-00129G 250.085 198.569 90 0402_3
C25 CAP-00130G 255.635 189.669 90 0402_3
C56 EXCLUDES 229.430 189.374 0 0402_4
R42 EXCLUDES 241.010 192.194 90 TANT3216
R10 EXCLUDES 246.560 203.894 0 0402_9
On the click of a button, I would like to "REMOVE ENDINGS" and re-upload it to the ListBox. So the new ListBox would look like this:
C44 EXCLUDES 237.910 193.469 0
C45 EXCLUDES 244.102 193.387 0
R47 EXCLUDES 226.935 179.519 90
C18 CAP-00129G 230.960 190.619 0
C17 CAP-00129G 250.085 198.569 90
C25 CAP-00130G 255.635 189.669 90
C56 EXCLUDES 229.430 189.374 0
R42 EXCLUDES 241.010 192.194 90
R10 EXCLUDES 246.560 203.894 0
All of the possibly endings are in this REGEX:
Regex placementOneRegex = new Regex(@"(RES|0402|0201|0603|0805|1206|1306|1608|3216|2551|1913|1313|2513|5125|2525|5619|3813|1508|6431|2512|1505|2208|1005|1010|2010|0505|0705|1020|1812|2225|5764|4532|1210|0816|0363|SOT)");
CODE:
private void removeEndingsButton_Click(object sender, EventArgs e)
{
string[] items;
System.Windows.Forms.ListBox.ObjectCollection contents = placementOneListBox.Items;
foreach (string str in contents)
{
Regex placementOneRegex = new Regex(@"(RES|0402|0201|0603|0805|1206|1306|1608|3216|2551"
+ @"|1913|1313|2513|5125|2525|5619|3813|1508|6431|2512|1505|2208|1005|1010|2010|0505|0705"
+ @"|1020|1812|2225|5764|4532|1210|0816|0363|SOT)");
items = str.Split(' ');
items[5].Replace(placementOneRegex.ToString(), "");
placementOneListBox.Items.Equals(items);
}
As you can probably tell.. this is not the best away to go about it. I think it would be easiest to string.Split()
and then just join all the split
's except the split[5]
... except I do not know how to go about doing this..
- My code does not work
QUESTIONS:
- How do I grab each line in the listbox and remove the last piece of data (using
string.Split()
)? - Is there an easier way to do this?
Looking at your data I'm thinking that you could use a string position to remove the endings. It looks like you want to truncate each entry after the 33rd position.
Are all the lines in this format?
Not perfect, but it does what you are looking for essentially. You have to tweak the Join to make it output how you want.
EDIT: Put this code in it's own class as is.
EDIT: Don't use my previous one, I had to fix the extension method, it wasn't working when the specified index to be removed was 0 or the length of the array.
Will something like this work? I didn't test it.
if those are tabs you might want to try '\t' in the split method.