Remove a linebreak in c# - c#

I have the following lines of text:
W&BL 15&384&320&214&1&S235JR&&&&&&&&&&S&&0.267&&&&4&&
N&214.nc
A&214&1&&15
W&BL 15&384&320&215&1&S235JR&&&&&&&&&&S&&0.267&&&&4&&
N&215.nc
A&213&2&&14
I want to remove the linebreaks so the outcome will be like this:
A&213&2&&14W&BL 15&384&320&214&1&S235JR&&&&&&&&&&S&&0.267&&&&4&&N&214.nc
A&214&1&&15W&BL 15&384&320&215&1&S235JR&&&&&&&&&&S&&0.267&&&&4&&N&215.nc
I do this because I need to format these lines and I'm putting the whole textfile in a reader per line. When I filter this with linebreaks I can't properly search through the lines. Since I need to delete everything after the S235JR, replace the & with ; and start the line with the BL code.
If someone knows a smarter/better solution to filter these lines, you will be my hero of the day.
Edit for clarification:
This is a example and how it needs to be formatted:
H&HEA100&1712&&1001&2&S235JR&&&HEA100 - 1712&&&&&&&S&&0.96&&&&2&&&1.7&0.2&0.2
N&1001.nc
W&BL 15&384&320&215&1&S235JR&&&&&&&&&&S&&0.267&&&&4&&
N&215.ncA&214&1&&15
H&L80X8&375&&1010&1&S275JR&&&L80X8 - 375&&&&&&&S&&0.117&&&&4&&&0.4&0.1&0.1
N&1010.nc
After formatting:
H;HEA100;1712;;1001;2;S235JR;
BL 15;384;320;215;1;S235JR;
L80X8;375;;1010;1;S275JR;
The input is a text file imported with a StreamReader. The H, BL 15 and L80X8 are determined after 6 & characters. The program was originally written in DOS and I need to convert it into C#. I'm sorry for the confusion.

x.Replace(Environment.NewLine, String.Empty);
where x is string.

From the example in your question, it looks like you want to remove line breaks, but keep every third line break.
You can use a regular expression that matches three lines, and remove the two line breaks between them:
text = Regex.Replace(text, #"(.+)\r\n(.+)\r\n(.+)", "$1$2$3");

x = x.Replace("\r\n", "");
x is your string Object;

string x = x.Replace("&", ";");
string x1 = x.Substring(x.IndexOf("H;"), x.IndexOf("S235JR;", x.IndexOf("H;")) - x.IndexOf("H;")+7);
string x2 = x.Substring(x.IndexOf("BL 15;"), x.IndexOf("S235JR;", x.IndexOf("BL 15;")) - x.IndexOf("BL 15;")+7);
string x3 = x.Substring(x.IndexOf("L80X8;"), x.IndexOf("S275JR;", x.IndexOf("L80X8;")) - x.IndexOf("L80X8;")+7);
string result = x1 + "\r\n" + x2 + "\r\n" + x3;

Related

Concatenation issue when using label in WinForms C#

There is a code part in my program. Lets say buf.Substring(0, 4) is a string which is 326 for that moment in the loop. String buf.Substring(0, 4) is updated in a for loop.
if (buf.Substring(0, 4).Equals("GG:"))
{
label22.Text = buf.Substring(4) + "Z" ;
}
This outputs on label22 as:
326
Z
If you replace it as
label22.Text = "Z" + buf.Substring(4);
then it concatenates properly as:
Z326
But I want the output to be:
326Z
Whatever I tried it dodn't work. I tried concatenating different ways or sizing label width very long ect. What could be the problem here?
You could try trimming the output of buf.Substring(4) like this
String output = buf.Substring(4).Trim(new char[] {'\r','\n'});
Or just plain Trim() like so
String output = buf.Substring(4).Trim();

Word count not working when entering new line

I've been looking at other stack overflow articles regarding similar issues when it comes to word count in C#, but none have helped me when it comes to the pickle I've encountered.
I have a textbox that inputs text from a text file. The text is split into three lines by me pressing the enter button to create a new line in a text file. The text reads:
It's a test to see "if" the
application_for Top Image Systems
actually work. Hopefully it does work.
Now as you can see there should be 17 words, however my word count only says 15. I have realized after a bit of trial and error that the issue must be the fact it's in a new line. Every time it goes to a new line, it thinks the last word of the previous line and the first word of the new line are together as a word (or that's what I think the program is thinking).
My question is with the code I have below, how can I get to recognize that if there is a new line, that it should split the words like a space?
Below is my code:
string nl = System.Environment.NewLine;
//Missing Code to read text file which I don't need to include in this example
do
{
textLine = textLine + txtReader.ReadLine();
}
//Read line until there is no more characters
while (txtReader.Peek() != -1);
//seperate certain characters in order to find words
char[] seperator = (" " + nl).ToCharArray();
//number of words
int numberOfWords = textLine.Split(seperator, StringSplitOptions.RemoveEmptyEntries).Length;
txtReader.ReadLine(); strips your newline away.
From the msdn:
The string that is returned does not contain the terminating carriage return or line feed.
so you have to add it manually (or just add a space)
textLine = textLine + txtReader.ReadLine() + " ";
consider using the StringBuilder class for repeated concatination of strings.
Edit:
To get the character count as if the spaces were never added, do:
int charCount = textLine.Length - lineCount;
where lineCount an integer that you increment every time you add a space in your do-while loop:
int lineCount = 0;
do
{
textLine = textLine + txtReader.ReadLine();
lineCount++;
}
I'm a bit of a beginner myself, sorry if this is not a great answer, but I've just done a bunch of text stuff in c# and I'd probably approach by replacing the line breaks which will show up as "\n" or "\r" in your original string with a space, " " - something like:
nl = nl.Replace("\r", " ");

Regex.Split and string.Split not working as expected

I am attempting to split strings using '?' as the delimiter. My code reads data from a CSV file, and certain symbols (like fractions) are not recognized by C#, so I am trying to replace them with a relevant piece of data (bond coupon in this case). I have print statements in the following code (which is embedded in a loop with index variable i) to test the output:
string[] l = lines[i][1].Split('?');
//string[] l = Regex.Split(lines[i][1], #"\?");
System.Console.WriteLine("L IS " + l.Length.ToString() + " LONG");
for (int j = 0; j < l.Length; j++)
System.Console.WriteLine("L["+ j.ToString() + "] IS " + l[j]);
if (l.Length > 1)
{
double cpn = Convert.ToDouble(lines[i][12]);
string couponFrac = (cpn - Math.Floor(cpn)).ToString().Remove(0,1);
lines[i][1] = l[0].Remove(l[0].Length-1) + couponFrac + l[1]; // Recombine, replacing '?' with CPN
}
The issue is that both split methods (string.Split() and Regex.Split() ) produce inconsistent results with some of the string elements in lines splitting correctly and the others not splitting at all (and thus the question mark is still in the string).
Any thoughts? I've looked at similar posts on split methods and they haven't been too helpful.
I had no problem using String.Split. Could you post your input and output?
If at all you could probably use String.Replace to replace your desired '?' with a character that does not occur in the string and then use String.Split on that character to split the resultant string for the same effect. (just a try)
I didn't have any trouble parsing the following.
var qsv = "now?is?the?time";
var keywords = qsv.Split('?');
keywords.Dump();
screenshot of code and output...
UPDATE:
There doesn't appear to be any problem with Split. There is a problem somewhere else because in this small scale test it works just fine. I would suggest you use LinqPad to test out these kinds of scenarios small scale.
var qsv = "TII 0 ? 04/15/15";
var keywords = qsv.Split('?');
keywords.Dump();
qsv = "TII 0 ? 01/15/22";
keywords = qsv.Split('?');
keywords.Dump();
New updated output:

c# Using Substring and Regular Expressions to edit a large string

The string, when displayed looks like: value1, value2, value3, value4, value5 etc..
What I want the string to do once I display it is (removing spaces and commas, i assume I can use index + 2 or something to get past the comma):
value1
value2
etc...
lastKnownIndexPos = 0;
foreach (System.Text.RegularExpressions.Match m in System.Text.RegularExpressions.Regex.Matches(unformatedList, ",?")) //Currently is ',' can I use ', '?
{
list += unformatedList.Substring(lastKnownIndexPos, m.Index - 1) + "\n\n"; //-1 to grab just the first value.
lastIndex = m.Index + 2; //to skip the comma and the space to get to the first letter of the next word.
//lastIndex++; //used this to count how many times it was found, maxed at 17 (have over 100):(
}
//MessageBox.Show(Convert.ToString(lastIndex)); //used to display total times each was found.
MessageBox.Show(list);
At the moment the message box does not show any text, but using the lastIndex I get a value of 17 so I know it works for part of it :P
That's easy (I'm using System.Linq here):
var formatted = string.Join("\n\n", unformatedList.Split(',').Select(x => x.Trim()));
MessageBox.Show(formatted);
An alternative approach, as swannee pointed out, would be the following:
var formatted = Regex.Replace(unformatedList, #"\s*,\s*", "\n\n").Trim();
Edit:
To make the above examples work regardless of how you use the result string, you should use Environment.NewLine instead of "\n".
One way is to simply replace the ", " with a newline.
MessageBox.Show( unformatedList.Replace(", ", "\n") );
Or heck, why not just use string.Replace?
var formatted = unformattedList.Replace(", ", "\n\n");

Split values in arrays

I have a Long string from that I want to store the keyword in array or collection, the format of my string is like below:
Title: My Test Page Title.
Desc: My page description.
Keywords: Bessel function, legendre function, Differential Equations, Bessel, Legendre, Homogenous, Assignment & Maths Homework Help.
Bessel & Legendre Function:
Homogenous Equations of the second order of the type
+ x + ( - )y = 0, v [0, ), x [0, )………………….(1)
(1 - ) - 2x + n (n + 1)y = 0, n = 1, 2 ……, x (-1, 1)…………………(2)
In this String I want to store all Keywords in Array/collection split from comma.
My problem is that How I can find out the starting and ending point to split the keywords, I can get the Starting point from Keywords: but what should be my ending point to store the keyword in array/collection, there is no any fix format,
there is only one fix format which is there will be a Para after ending the Keyword section.
any one can suggest me regular expression for this.
there will be a Para
Seems like you should first split the string into lines.
And then the line that starts with Keywords: holds your keywords.
You can use the string.Split() method to split into lines as well as for breaking out the keywords.
It also looks like the Keywords section ends with a fullstop. So you could find the next fullstop ie IndexOf(".") after the "Keywords:" ....
I think this should do:
string afterKeywords = data.Substring(data.IndexOf("Keywords:") + 9);
string beforeNextPara = afterKeywords.Substring(0, afterKeywords.IndexOf(Environment.NewLine + Environment.NewLine));
var dataWeNeed = beforeNextPara.Split(',');

Categories