c# - String.Split()删除最后一项

本文关键字:一项 最后 删除 Split String | 更新日期: 2023-09-27 18:03:22

我有一个RTB,它加载了一个文件,看起来像这样:

J6      INT-00113G  227.905  174.994  180  SOIC8    
J3      INT-00113G  227.905  203.244  180  SOIC8     
U13     EXCLUDES    242.210  181.294  180  QFP128    
U3      IC-00276G   236.135  198.644  90   BGA48     
U12     IC-00270G   250.610  201.594  0    SOP8      
J1      INT-00112G  269.665  179.894  180  SOIC16    
J2      INT-00112G  269.665  198.144  180  SOIC16    

我想使用string.Split()方法删除最后一列。

到目前为止,我有:

// Splits the lines in the rich text boxes
string[] lines = richTextBox2.Text.Split(''n');
foreach (var newLine in lines)
{
     newLine.Split(' ');
     line = line[0] + line[1] + line[2] + line[3] + line[4];  #This is the code that does not work.
}

然而,这不起作用…有人知道这个问题,以及如何正确地做到这一点,使文件看起来像这样吗?:

J6      INT-00113G  227.905  174.994  180      
J3      INT-00113G  227.905  203.244  180       
U13     EXCLUDES    242.210  181.294  180     
U3      IC-00276G   236.135  198.644  90      
U12     IC-00270G   250.610  201.594  0         
J1      INT-00112G  269.665  179.894  180     
J2      INT-00112G  269.665  198.144  180    

编辑:我也认为我需要string.Split(' ')每一行已经分裂?

c# - String.Split()删除最后一项

这可能有效(未经测试):

string[] lines = richTextBox2.Text.Split(''n');
for( int i = 0; i < lines.Length; i ++ )
{
    lines[i] = lines[i].Trim(); //remove white space
    lines[i] = lines[i].substring(0, lines[i].LastIndexOf(' ');
}
string masterLine = String.Join(Environment.NewLine, lines);

这是一个固定宽度的布局,所以你可以简单地通过切断第五列右边的所有内容来完成你想要的:

string[] lines = File.ReadAllLines(path);
for (int i = 0; i < lines.Length; ++i)
{
    lines[i] = lines[i].Substring(0, 43);
}

输入文件示例:

J6      INT-00113G  227.905  174.994  180  SOIC8    
J3      INT-00113G  227.905  203.244  180  SOIC8     
U13     EXCLUDES    242.210  181.294  180  QFP128    
U3      IC-00276G   236.135  198.644  90   BGA48     
U12     IC-00270G   250.610  201.594  0    SOP8      
J1      INT-00112G  269.665  179.894  180  SOIC16    
J2      INT-00112G  269.665  198.144  180  SOIC16    
输出:

J6      INT-00113G  227.905  174.994  180  
J3      INT-00113G  227.905  203.244  180  
U13     EXCLUDES    242.210  181.294  180  
U3      IC-00276G   236.135  198.644  90   
U12     IC-00270G   250.610  201.594  0    
J1      INT-00112G  269.665  179.894  180  
J2      INT-00112G  269.665  198.144  180  

你不是在说不工作,使用Lines属性你可以这样做:

richTextBox2.Lines = richTextBox2.Lines
                                 .Select( l => string.Join(" ", l.Split(' ')
                                                     .Take(5)))
                                 .ToArray();

这当然只在空格作为列之间的分隔符时才有效。

这个很容易读....

        string data = "J6      INT-00113G  227.905  174.994  180  SOIC8'r'nJ3      INT-00113G  227.905  203.244  180  SOIC8'r'nU13     EXCLUDES    242.210  181.294  180  QFP128'r'nU3      IC-00276G   236.135  198.644  90   BGA48'r'nU12     IC-00270G   250.610  201.594  0    SOP8'r'nJ1      INT-00112G  269.665  179.894  180  SOIC16'r'nJ2      INT-00112G  269.665  198.144  180  SOIC16'r'n";
        // Split on new line
        string[] lines = data.Split(new string[] {"'r'n"}, int.MaxValue, StringSplitOptions.RemoveEmptyEntries);
        StringBuilder result = new StringBuilder();
        foreach (string line in lines)
        {
            // Find the last space in the line
            int lastSpace = line.LastIndexOf(' ');
            // delete the end of the string from the last space
            string newLine = line.Remove(lastSpace);
            // rebuild string using stringBuilder
            result.AppendLine(newLine);
        }
        Console.WriteLine("Old List:");
        Console.Write(data);
        Console.WriteLine("New List:");
        Console.Write(result);
    }

这个可能接近于O(n):

        StringBuilder result = new StringBuilder();
        string data = "J6      INT-00113G  227.905  174.994  180  SOIC8'r'nJ3      INT-00113G  227.905  203.244  180  SOIC8'r'nU13     EXCLUDES    242.210  181.294  180  QFP128'r'nU3      IC-00276G   236.135  198.644  90   BGA48'r'nU12     IC-00270G   250.610  201.594  0    SOP8'r'nJ1      INT-00112G  269.665  179.894  180  SOIC16'r'nJ2      INT-00112G  269.665  198.144  180  SOIC16'r'n";
        // Split on new line
        int startchar = 0;
        int lastspace = 0;
        for(int i = 0; i < data.Length - 1; i++)
        {
            char current = data[i];
            if (current == ' ')
            {
                // remember last space
                lastspace = i;
            }
            else if (current == ''n')
            {
                result.AppendLine(data.Substring(startchar, lastspace - startchar));
                if(i != data.Length - 1)
                {
                    startchar = i + 1;
                    lastspace = startchar;
                }
            }
        }

        Console.Write(result.ToString());

你是唯一知道问题所在的人。不幸的是,你决定不告诉我们是什么。(奇怪。)

我要做的是解析每行,然后使用LastIndexOf()找到最后一个空格,并在那里修剪字符串。可选地,您可以将结果传递给Trim()以删除任何尾随空格。

假设你的行总是相同的宽度,那么:

string parsed = richTextBox2.Text.Split(''n').
         Select(l => l.Substring(41) + Environment.NewLine);

您应该首先使用readline()逐行读取文件,然后使用split将每行分开。

var myString = "I want to remove the last item";
var mySplitResult = myString.split(" ");
var lastitem =  mySplitResult[mySplitResult.length-1]