Reputation: 1636
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?
Upvotes: 3
Views: 15627
Reputation: 467
var myString = "I want to remove the last item";
var mySplitResult = myString.split(" ");
var lastitem = mySplitResult[mySplitResult.length-1]
Upvotes: 0
Reputation: 6611
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());
Upvotes: 1
Reputation: 8866
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);
Upvotes: 7
Reputation: 160852
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.
Upvotes: 1
Reputation: 6591
You should probably read your file line by line in the first place using readline() then split each line with split.
Upvotes: 0
Reputation: 128317
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
Upvotes: 3
Reputation: 31394
Assuming that your lines are always the same width then:
string parsed = richTextBox2.Text.Split('\n').
Select(l => l.Substring(41) + Environment.NewLine);
Upvotes: 0
Reputation: 67185
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.
Upvotes: 0