StringReader formatted string - c#

I have followed this Wordpress tutorial which works great. I have used a listview and when i try to format the string it doesn't recognise the \t (but does recognise \n). It also won't recognise String.Format etc.
Is there anyway that I can format the string using tabs or something similar?
Cheers
EDIT
for( i = 0; i < lstView.Items.Count;i++)
{
name = lstView.Items[i].Text;
state = lstView.Items[i].SubItems[1].Text;
country = lstView.Items[i].SubItems[2].Text;
line += name + "\t" + state + "\t" + country + "\n";
}
StringReader reader = new StringReader(line);
When line is used to print the string is joined together so the \t doesn't work. The \n for a new line does work though. Does anyone know any way that I can format the string without using spaces.
The result is like this
NameStateCountry
LongernameStateCountry
anotherNameAnotherStateAnotherCountry
Where I would like them lined up (like in a table) with name one column, state another column and country then third
Any suggestions greatly appreciated

Well, it is a bit odd that tabs are lost, but on the other hand, tabs will probably be problematic if the individual string elements (name, state etc.) varies in length.
What you could do instead is use string.Format() and use fixed column widths.
To get nice visual output this would include a parse step to determine the correct column width.
When this is done, use something like this to use spaces instead of tabs.
string line = string.Format("{0,-20}{1,-20}{2,-20}", "name", "state", "country");
EDIT: Saw that you did not want to use spaces.
In this case, you will probably need to handle this in the printing algorithm itself. You could still separate items with tabs, then for each line split it on tabs, creating an array of items (columns) per line.
For each item, print it using Graphics.DrawString() with a suitable X-position offset.
See the documentation for Graphics.DrawString.

Related

Unable to match on first column name after parsing CSV document

I am working on CSV import where I take a file (with headings as the first row) and parse the document into DataTable structure.
When I try to organise the data into a collection for some reason (unknown to me), my state machine fails to match on the very first column heading. It should be fairly straight forward, no magic involved.
foreach (DataRow row in dt.Rows)
{
foreach (DataColumn col in row.Table.Columns)
{
switch(col.ColumnName)
{
default:
// debug: Exceptions.LogException(new Exception(" csv {ColumnName:'" + col.ColumnName + "',Length:" + col.ColumnName.Length + ",Test:" + string.Equals(col.ColumnName, "Name") + "}"));
break;
case "Name":
// doesn't get picked up
break:
My debug line(s) return the following: csv {ColumnName:'Name',Length:5,Test:False}
Interestingly enough, if I add a dummy column to the file in front of Name column then my case: "Name" works fine.
Any ideas what could be causing an issue like that?
Great comments and suggestions
Reproducible code example - was going to make one today but it looks like we have a different problem
Leading/trailing spaces - checked for those before posting
Name being reserved - tried a different column name didn't make a difference
Weird characters - checked CSV in Notepad, Sublime (fancy Notepad) before posting for strange characters. But after JAZ suggested to check the length s/he was right on the money (see above).
Pursuing the issue of weird characters
So far it doesn't seem to be any of the usual suspects: space, tab, newline, carriage return (or combination of both). But one thing is for sure it's at the begging of the string as suggested by debugging log.
sb.Append("{Col:'" + col.ColumnName.Substring(0, 3) + "',Len:" + col.ColumnName.Length + "}");
Returning {Col:'vN',Len:6} where first column is vName.
Culprit/Solution
Finally found the culprit U+FEFF aka BYTE ORDER MARK character which appears at the start of text stream (but can also appear in the middle ZERO WIDTH NO-BREAK SPACE) and indicates the type of encoding (UTF-8, UTF-16, UTF-32, etc).
Found by converting a string of characters into Unicode as follows:
col.ColumnName.Select(t => string.Format("U+{0:X4}", (ushort)t)).ToList()
Producing the following output for vName string:
U+FEFF = byte order mark
U+0076 = v
U+004E = N
U+0061 = a
U+006D = m
U+0065 = e
Handy to know
Just wanted to share that you can quickly check the type of encoding and line break used by opening the file in Notepad. Would have been handy to know this when I was posting my question. Below are three different CSV files which use a different encoding.
Probably a line feed or some weird character in the data - What is the length of the string when it fails? That would tell you if there are too many chars.
Start with the CSV data and check it in a real editor, not Excel, see if there is something in the data.
Use Notepad++ and change the encoding of the text file to see the extra characters. Don't think Windows Notepad will show them.

How a can split file line with many spaces in C# asp?

I need your help. I have a txt file with many lines of information.
The headers of the file are
Date ReferenceNumber Description
13/06/2013 00000081985 TRF DESDE OTRO BCO 00000000000000972353
0105
Mount Money +50.000,00 344.514,74
Between Description and Mount are many spaces
Here's a image of the file
I need split this line to get all the attributes by separate.
I need, Date = 13/06/2013, ReferenceNumber = 00000081985, ....
I'm trying to use split C# function to separate by (' ') but i only can get the 2 first attributes =(
I hope you can help me! Thanks a lot.
You may want to look to see what the length of each field is because it does look like fixed length data. If so use the String.Substring Method, using the starting position and the max length of each field as inputs.
This looks like you're trying to deal with a fixed length file, which is essentially a file with data that is split based on its physical location in the file (each piece of data is expected to occupy a specific number of characters). Seems to be one of those lesser known functions, but check out TextFieldParser. It's a .NET class specifically made for this sort of thing.
Specifically, check out the property TextFieldType, which can be set to FixedWidth and given a width of each of those fields. Should do exactly what you want.
You can try to do something like this:
StreamReader sr = new StreamReader("path to text file");
string s = sr.ReadToEnd();
s = s.Replace(' ', '!'); //change the space sign with other sign
List<string> strList = s.Split('!').ToList();
strList.RemoveAll(t => t == "");
I know the solution isn't best but I hope it will help you.

C# - Tab Delimited String not working if column has empty characters/spaces

I have the following text in a .txt file:
hello 123 example info
mello 456 xample text
yello maple syrup
I am doing:
string FILENAME = Server.MapPath(".") + #"\example.txt";
string[] allLines = File.ReadAllLines(FILENAME);
string[] items = allLines[i].Split('\t');
Now, for the last row, I am getting two consecutive empty columns instead of just skipping the second one (items[1]) and giving me the third one.
My final result looks like:
hello 123 example info
mello 456 xample text
yello
I am actually inserting the information in a database but I don't want to go into much detail because I know that the problem is in the split itself.
My final result should include the third column information in the third row.
You are splitting the line by '\t' which is correct as long as the blank space uses tabs to get there. If they had used tabs properly, you should have ended up with exactly what you wanted. If they had used spaces, instead, you would have ended up with all the text in your first column, just very long. What you got instead, is nothing. The most likely solution is that there were actually EXTRA tabs, that the output array was longer than expected, and the third column is now, for instance, in the items[3] slot instead of items[2]. It is difficult to tell you how you can control for this except that instead of checking items[2] you could test for items[items.length-1].

Regex to select all commas up to a specific character

I am having a terrible time with regular expressions. Its terrible for me to admit, but I just don't use them enough to be really good when I need to be. Basically due to the way our application runs, I have the contents of a .csv file pulled out into a string. I need to essentially insert a new row above and below what already exists. The amount of columns can change depending on the report. What I would like to do is grab all commas without any other characters (including whitespace) up to the first set of \r\n in the string. This way I have all the columns and I can insert a blank row up top and populate the columns with what I need. Here is an example of the .csv text:
"Date, Account Code, Description, Amount\r\n23-Apr-13,12345,Account1,$12345\r\n"
What I would like the regex to grab:
",,," or ",,,\r\n"
I just cannot seem to get this. Thank you.
You don't need a regex for this.
string firstLine = file.ReadLines().First();
int numCommas = firstLine.Count(c => c == ',');
string commaString = new String(',', numCommas);
If you don't have access to file.ReadLines() method, you can use the following from this link:
string firstline = test.Substring(0, test.IndexOf(Environment.NewLine));
You actually don't need to complicate your code with Regular Epressions to accomplish what you want: to count the columns.
Here's an extremely simple method:
String textline = csvtext.Substring(0, csvtext.IndexOfAny(Environment.NewLine.ToCharArray()));
int columns = textline.Split(',').Length;
Now the columns variable has your total number of columns.
The first line grabs just the first line out of the CSV text. The second line splits that text into an array separated by commas (,), and returns the total number.
you can make use the below regex
(?<=[\d\w\s])(\r|\n|,)(?=[\d\w\s\W])
to match , and new line characters,
Use can make use of Regex.Replace("inputstring","regexpattern","replacechar", RegexOptions.IgnoreCase)
This can be done by string operations itself
string[] strs= inputstr.split(new string[]{"\n","\r",","}, StringSplitOptions.RemoveEmptyEntries);
foreach(string str in strs)
{
// do you part
}

Replacing part of text in richtextbox

I need to compare a value in a string to what user typed in a richtextbox.
For example: if a richtextbox holds string rtbText = "aaaka" and I compare this to another variable string comparable = "ka"(I want it to compare backwards). I want the last 2 letters from rtbText (comparable has only 2 letters) to be replaced with something that was predetermined(doesn't really matter what).
So rtbText should look like this:
rtbText = "aaa(something)"
This doesn't really have to be compared it can just count letters in comparable and based on that it can remove 2 letters from rtbText and replace them with something else.
UPDATE:
Here is what I have:
int coLen = comparable.Length;
comparable = null;
TextPointer caretBack = rtb.CaretPosition.GetPositionAtOffset(coLen, LogicalDirection.Backward);
TextRange rtbText = new TextRange(rtb.CaretPosition, caretBack);
string text = rtbText.Text;
rtbText returns an empty string or I get an error for everything longer than 3 characters. What am I doing wrong?
Let me elaborate it a little bit further. I have a listbox that holds replacements for values that user types in rtb. The values(replacements) are coming from there, meaning that I don't really need to go through the whole text to check values. I just need to check the values right before caret. I am comparing these values to what I have stored in another variable (comparable).
Please let me know if you don't understand something.
I did my best to explain what needs to be done.
Thank you
You could use Regex.Replace.
// this replaces all occurances of "ka" with "Replacement"
Regex replace = new Regex("ka");
string result = replace.Replace("aaaka","Replacemenet");
gumenimeda, I had similar problems few weeks ago. I found my self doing the following (I asume you will have more than one occurance in the RichTextBox that you will need to change), note that I did it for Windows Forms where I have access directly to the Rtf text of the control, not quite sure if it will work well in your scenario:
I find all the occurancies of the string (using IndexOf for example) and store them in a List for example.
Sort the list in descending order (max index goes first, the one before him second, etc)
Start replacing the occurancies directly in the RichTextBox, by removing the characters I don't need and appending the characters I need.
The sorting in step 2 is necessary as we always want to start from the last occurance going up to the first. Starting from the first occurance or any other and going down will have an unpleasant surprise - if the length of the chunk you want to remove and the length of the chunk you want to append are different in length, the string will be modified and all other occurancies will be invalid (for example if the second occurance was in at position 12 and your new string is 2 characters longer than the original, it will become 14th). This is not an issue if we go from the last to the first occurance as the change in string will not affect the next occurance in the list).
Ofcourse I can not be sure that this is the fastest way that can be used to achieve the desired result. It's just what I came up with and what worked for me.
Good luck!

Categories