我有一个列表框,其内容如下所示:
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
点击按钮,我想“删除结尾”并重新上传到ListBox。 所以新的ListBox看起来像这样:
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
所有可能的结局都在本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);
}
正如你可能会说的那样......这不是最好的选择。我认为string.Split()
最简单,然后加入除了split
之外的所有split[5]
...除了我不知道怎么去这样做..
的问题: 的
string.Split()
)?答案 0 :(得分:0)
这样的事情会起作用吗?我没有测试它。
private void removeEndingsButton_Click(object sender, EventArgs e)
{
string[] items;
System.Windows.Forms.ListBox.ObjectCollection contents = placementOneListBox.Items;
foreach (string str in contents)
{
List<string> list = str.Split(' ').ToList();
if(list.Count == 6)
{
string remove = list[5];
list.Remove(remove);
placementOneListBox.Items.Equals(list.ToArray());
}
}
}
如果这些是标签,你可能想在分割方法中尝试'\ t'。
答案 1 :(得分:0)
private void removeEndingsButton_Click(object sender, EventArgs e)
{
string[] items;
char[] splitChars = new char[] {'\t', ' '};
int fieldCount;
int lineCount = 0;
System.Windows.Forms.ListBox.ObjectCollection contents = placementOneListBox.Items;
foreach (string str in contents)
{
lineCount++;
items = str.Split(splitChars, StringSplitOptions.RemoveEmptyEntries);
fieldCount = items.Length;
placementOneListBox.Items[lineCount] = string.Join(" ", items.Take(fieldCount - 1).ToArray());
}
}
答案 2 :(得分:0)
查看您的数据我认为您可以使用字符串位置来删除结尾。看起来你想在第33个位置之后截断每个条目。
这种格式的所有行都是吗?
答案 3 :(得分:0)
不完美,但它实际上是你正在寻找的。你必须调整Join才能输出你想要的结果。
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Remove_Click(object sender, EventArgs e)
{
ListBox.ObjectCollection collection = placementOneListBox.Items;
// New collection to store our modified data
List<string> newCollection = new List<string>();
// We need regex to account for 0 or more spaces between items in the array.
Regex r = new Regex(" +");
foreach (string item in collection)
{
// use the regex we created to split the item into an array, it splits it based on any number of spaces.
string[] splitItem = r.Split(item);
// We had to write an extension method that "removes" at a particular index.
splitItem = splitItem.RemoveAt(5);
// Put it back together again. might have to mess with this to make it output correctly
string updatedItem = String.Join(" ", splitItem);
// add new item to collection
newCollection.Add(updatedItem);
}
// clear the existing stuff in the listbox collection
placementOneListBox.Items.Clear();
// we can use a List<T> as a data source so that's what were doing here.
placementOneListBox.DataSource = newCollection;
}
}
编辑:将此代码按原样放入其自己的类中。
public static class ExtensionMethods
{
// An extension method to remove an index from an array, it works with any value type
public static T[] RemoveAt<T>(this T[] source, int index)
{
if (source == null)
throw new ArgumentNullException();
if (index < 0 || index > source.Length)
throw new IndexOutOfRangeException();
T[] dest = new T[source.Length - 1];
if (index != 0)
Array.Copy(source, 0, dest, 0, index - 1);
if (index != source.Length)
Array.Copy(source, index + 1, dest, index, source.Length - index - 1);
return dest;
}
}
编辑:不要使用我以前的那个,我必须修复扩展方法,当要删除的指定索引是0或数组的长度时,它无法正常工作。