Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# Adding punctuation marks

So I have an assigment for c# in which I need to work with text files, separate words at commas and other punctuation marks. I choose to do it like this:

string Book1 = "@\\..\\Knyga1.txt";
string punctuation = " ,.?!;:\"";
string Read1 = File.ReadAllText(Book1);
string[] FirstFileWords = Read1.Split(punctuation.ToCharArray());

But I've run into a problem... My text files are supposed to be like books, so obviously there's going to be multiple lines... is there any way to add "the enter key" or whatever we shall call the thing to make a new line (sorry for my bad english) one of the punctuation marks? Because when working with individual words later, for example printing out the longest words, words which are at the start of line 2 3 and so on take up two lines in the console.

like image 413
BligenN Avatar asked Jul 23 '26 02:07

BligenN


2 Answers

Just add \r\n to the list. That's the "enter key" -- i.e. "new line" -- in Windows OS', and it's what is returned by Environment.NewLine.

string punctuation = " ,.?!;:\"\r\n";

\r stands for "carriage return" and \n stands for "line feed" which, when used together, are called a "new line" (as explained on the above MSDN page and other places like this SO answer).

Additionally, there are other not-so-common "vertical whitespace" characters (see my question here for reference). So, to be complete, I would do this to include "vertical tab", "form feed", "next line", "line separator", and "paragraph separator":

string punctuation = " ,.?!;:\"\r\n\v\f\u0085\u2028\u2029";

Here's a Wikipedia article that describes all these and other whitespace chars.

like image 152
rory.ap Avatar answered Jul 24 '26 18:07

rory.ap


To add new lines to your group you need to use the new line and carriage return characters:

" ,.?!;:\"\r\n";
like image 22
TheLethalCoder Avatar answered Jul 24 '26 18:07

TheLethalCoder



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!