Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - String.Split() Remove last item

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  SOIC16    
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?

like image 904
theNoobGuy Avatar asked Feb 04 '26 06:02

theNoobGuy


2 Answers

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);
like image 147
Coeffect Avatar answered Feb 07 '26 18:02

Coeffect


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  
like image 30
Dan Tao Avatar answered Feb 07 '26 18:02

Dan Tao



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!