开发者

C# - String.Split() Remove last item

开发者 https://www.devze.com 2023-03-23 08:30 出处:网络
I have a RTB that is loaded with a file that looks like this: J6INT-00113G227.905174.994180SOIC8 J3INT-00113G227.905203.244180SOIC8

I have a RTB that is loaded with a file that looks like this:

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  SOIC开发者_如何学C16    
J2      INT-00112G  269.665  198.144  180  SOIC16    

I want to remove the last column using the string.Split() method.

So far I have:

// 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.
}

However this does not work... does anyone know the problem and how to do this properly so the file looks like this?:

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    

EDIT: I also think that I need to string.Split(' ') each line that is already split?


This might work (untested):

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);


This is a fixed-width layout, so you can accomplish what you want simply by cutting off all the content to the right of the fifth column:

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

Example input file:

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    

Output:

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  


You are not saying what doesn't work..using the Lines property you could do something like this:

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

This would only work of course if the space only occurs as separator between columns.


This ones fairly easy to read ....

        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);
    }

This one is probably close to 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());


You're the only one that "knows the problem". Unfortunately, you decided not to tell us what it is. (Strange.)

What I would do is parse each line as you are, and then use LastIndexOf() to find the last space, and just trim the string there. Optionally, you could pass the result to Trim() to remove any trailing spaces.


Assuming that your lines are always the same width then:

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


You should probably read your file line by line in the first place using readline() then split each line with split.


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号