I want to write a message in Hebrew to a text file. I tried the following:
using (StreamWriter sw = File.CreateText(path + FileName))
{
sw.WriteLine(sms);
}
However, the message comes out left to right. How can I write right to left?
A string is an Array of Chars so you can
Char[] tempArray = sms.ToCharArray()
Array.Reverse(tempArray)
sms = new String(tempArray)
This should reverse the array therefore when you write it will read right to left.
well, it doesn't really matter how you write them in.
Here's a little example:
void Main()
{
string lines = "First line.\r\nSecond line.\r\nThird line.";
var = new System.IO.StreamWriter("c:\\test_eng.txt");
file.WriteLine(lines);
file.Close();
string hebrew = #"מספרים רצים מימין לשמאל ?";
file = new System.IO.StreamWriter("c:\\test_heb.txt");
file.WriteLine(hebrew);
file.Close();
}
Upon opening the files, you'll find that hebrew is hebrew, and display from right to left.
Are you experiencing any funny behavior?
You can also try defining your string culture to hebrew:
// Creates and initializes the CultureInfo which uses the international sort.
CultureInfo myCIintl = new CultureInfo( "he", false );
More on that here (MSDN).
Some more thinking -> I doubt the above will change much for the display, it'll be more useful for dates for example. But atm, I'm not able to see hebrew on my console output ... What are you outputting to? Just txt, HTML, or something else?
Related
I'm building a website in ASP.net and, in this case, I'm simply trying to read a .txt file and output it into a textbox whenever that page loads. I wrote this code so I could execute my idea:
protected void Page_Load(object sender, EventArgs e)
{
foreach (string line in System.IO.File.ReadLines(#"file_path"))
{
TextBox1.Text = line;
}
}
However, this is not working, as when I execute the website on this page, nothing loads. There is no exception that is being called or any error.
What can I do?
If you need more details or information, please don't refrain from asking.
Edit: I think it's important to say that I've tested doing
TextBox1.Text = "Hello World";
And that worked properly.
Well, to read the text, this works:
string sFile = #"C:\TEST2\T.txt";
string sTextData = File.ReadAllText(sFile);
TextBox1.Text = sTextData;
And if some some reason, you LEFT OUT some HUGE MASSIVE WHOPPER detail, say like you only want the first line of text from the file?
Then this:
string sFile = #"C:\TEST2\T.txt";
string sTextData = File.ReadAllText(sFile);
string strFirstLine = sTextData.Split(System.Environment.NewLine.ToCharArray())[0];
TextBox1.Text = strFirstLine;
FYI:
Don't forget to set the text box to multiline if you need/want to have more then one line of text display in that box.
As pointed out, we can use this:
File.ReadAllLines(sFile).FirstOrDefault();
however, the above still reads the whole text file.
If the text file is to be large, and we still want the first line, or wish to read + process line by line, then this is better:
StreamReader sR = new StreamReader(sFile);
int i = 0;
while (!sR.EndOfStream)
{
i++;
Debug.Print(i.ToString() + "->" + sR.ReadLine());
if (i >= 1)
break;
}
sR.Close();
So, above would read line by line, and if the text file was large, or we wish to only pull + read the first line from the file? Then of course the above example is better, since only the first line (or several lines if i condition is changed) is better from a performance + io point of view.
To read the first line, you can simply write this File.ReadLines(“YourFileName”).FirstOrDefault;
You need to use the method File.ReadAllText. It reruns the string value. And you can then assign to Text property.
File.ReadLines returns Ienumerable<string> not string .
So I am trying to extract from the PDF file certain content. So it is an invoice, I want to be able to search the PDF file for the word "Invoice Number:" and then "First Name" and extract them in the
Console.WriteLine();
So at the moment this is what I got and I need to figure out how to move further.
using iTextSharp.text.pdf;
using System.IO;
using iTextSharp.text.pdf.parser;
using System;
namespace PdfProperties
{
class Program
{
static void Main(string[] args)
{
PdfReader reader = new PdfReader("C:/PDF/invoiceDetail.pdf");
PdfReaderContentParser parser = new PdfReaderContentParser(reader);
FileStream fs = new FileStream("C:/PDF/result0.txt", FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
SimpleTextExtractionStrategy strategy;
string text = "";
for (int i = 1; i <= reader.NumberOfPages; i++)
{
strategy = parser.ProcessContent(i, new SimpleTextExtractionStrategy());
sw.WriteLine(strategy.GetResultantText());
text = strategy.GetResultantText();
String[] splitText = text.Split(new char[] {'.' });
Console.WriteLine("Test");
Console.WriteLine(text);
}
sw.Flush();
sw.Close();
}
}
}
Any help would be greatly appreciated
Hy
you could try this:
String[] splitText = text.Split(".");
for(int i =0; i<splitText.Lenght;i++)
{
if(splitText[i].toString() =="Invoice Number:")
(
// we have Invoice Number
// now we search for First Name
if(splitText[i].toString() == "First Name")
(
// now we have also First Name
)
)
}
There are 2 ways of going about this:
You can try to process the invoice yourself. That means handling structure, and dealing with edge-cases. What if the content isn't always aligned in the same way? What if the template of the invoice changes? What if some text in the invoice is variable and you can't really rely on the precise text being extracted? ..
This is, in short, not a trivial problem to solve.
Use pdf2Data. It was specifically designed to handle documents that are rich in structure. Like invoices. It uses a concept called "selectors" that allow you to define where you expect certain content to be. Either by position (somewhere in the rectangle defined by coordinates ..) or by structural blocks (row .. from this table) etc.
Even though the add-on is closed source, you can always try it out by using a trial-license. After evaluating pdf2Data, you can at least make a more informed decision about which route you're willing to take to tackle this problem.
Check out itextpdf.com/itext7/pdf2Data for more information
Thank you very much for all the help till now.
I have the following problem:
static void Main(string[] args)
{
Dictionary<string, string> dict = new Dictionary<string, string>();
using (StreamReader reader = new StreamReader(#"C:\test.csv"))
{
string line;
while ((line = reader.ReadLine()) != null)
{
string[] parts = line.Split(',');
dict.Add(parts[0],parts[1]);
}
}
Console.WriteLine("enter name:");
string name = Console.ReadLine();
if (dict.ContainsKey(name))
{
//
string value = dict[name];
Console.WriteLine(dict[name]);//value);
}
Console.ReadLine();
}
}
And this is my Outout:
enter name:
ILIA
????
This is my CSV file content
ILIA,ИЛИЯ
Can you please help me out clear this ???? and get the correct symbols in Cyrillic?
Thank in advance for any help!
Ilia
Try :
StreamReader reader = new streamReader(#"C:\test.csv",Encoding.GetEncoding(1251)) ;
I noticed this unpleasant issue while reading files with Cyrillics some time ago. The easiest solution - open the file in Notepad++ or any similar text editor that can change encoding and choose from the menu Encoding -> Convert to UTF-8 for example. Then your file will be processed correctly with your code
The output encoding of your console is likely set to ASCII. Try adding the following command before writing to the console:
Console.OutputEncoding = System.Text.Encoding.Unicode;
It's also possible that the the font that you chose for the console does not support Unicode. To fix this, open the the command console, click on the icon at the top left corner of the window that says "C:\". Select properties. Select Font. Try selecting a different font.
I need to be able to take a text file with unknown encoding (e.g., UTF-8, UTF-16, ...) and copy it line by line, making specific changes as I go. In this example, I am changing the encoding, however there are other uses for this kind of processing.
What I can't figure out is how to determine if the last line has a newline! Some programs care about the difference between a file with these records:
Rec1<newline>
Rec2<newline>
And a file with these:
Rec1<newline>
Rec2
How can I tell the difference in my code so that I can take appropriate action?
using (StreamReader reader = new StreamReader(sourcePath))
using (StreamWriter writer = new StreamWriter(destinationPath, false, outputEncoding))
{
bool isFirstLine = true;
while (!reader.EndOfStream)
{
string line = reader.ReadLine();
if (isFirstLine)
{
writer.Write(line);
isFirstLine = false;
}
else
{
writer.Write("\r\n" + line);
}
}
//if (LastLineHasNewline)
//{
// writer.Write("\n");
//}
writer.Flush();
}
The commented out code is what I want to be able to do, but I can't figure out how to set the condition lastInputLineHadNewline! Remember, I have no a priori knowledge of the input file encoding.
Remember, I have no a priori knowledge of the input file encoding.
That's the fundamental problem to solve.
If the file could be using any encoding, then there is no concept of reading "line by line" as you can't possibly tell what the line ending is.
I suggest you first address this part, and the rest will be easy. Now, without knowing the context it's hard to say whether that means you should be asking the user for the encoding, or detecting it heuristically, or something else - but I wouldn't start trying to use the data before you can fully understand it.
As often happens, the moment you go to ask for help, the answer comes to the surface. The commented out code becomes:
if (LastLineHasNewline(reader))
{
writer.Write("\n");
}
And the function looks like this:
private static bool LastLineHasNewline(StreamReader reader)
{
byte[] newlineBytes = reader.CurrentEncoding.GetBytes("\n");
int newlineByteCount = newlineBytes.Length;
reader.BaseStream.Seek(-newlineByteCount, SeekOrigin.End);
byte[] inputBytes = new byte[newlineByteCount];
reader.BaseStream.Read(inputBytes, 0, newlineByteCount);
for (int i = 0; i < newlineByteCount; i++)
{
if (newlineBytes[i] != inputBytes[i])
return false;
}
return true;
}
this is my first post, and I am new to C# so please be gentle :P.
I am writing application that is reading some txt files and then displaying them in gridview (I am later doing some more things with it, but it is not important right now). Almost all of my files have some established system of characters (there are always three columns that are separated with coma) and here I have no problem with reading them putting in list and then bind into gridview.
But I also have some files that don't have same system of characters, and I have problem with dealing with them. Can You help me to somehow format them to this original system of characters (I mean this one with commas). Bellow is my function for loading everything into list and then into gridview. I also posted example of file with good system of characters, and this one which is not formated.
29-01-2013 03:49:31.629,Some text ghs(23).asv1, more text
17-07-2011 12:12:32.643,Some text also dsad(1), more text
31-01-2013 08:14:08.473,Some text sdfsdfsd[2], more text
Not formated text has some spaces at the beggining, then some number, (dot), and another space. After this I would like to start reading and get rid of everything that was before. Moreover between my proper data it does not have commas but spaces. It is sth like this.
Please guys can You help me??
23-05-2009 12:12:45.675 Some text fsdf1 some more text
13-02-2003 11:12:45.454 Some text sdfsdfS(1) some more text
private void button7_Click(object sender, EventArgs e)
{
List<MyColumns> list = new List<MyColumns>();
OpenFileDialog openFile1 = new OpenFileDialog();
openFile1.Multiselect = true;
if (openFile1.ShowDialog() != DialogResult.Cancel)
{
foreach (string filename in openFile1.FileNames)
{
using (StreamReader sr = new StreamReader(filename))
{
string line;
while ((line = sr.ReadLine()) != null)
{
string[] _columns = line.Split(",".ToCharArray());
MyColumns mc = new MyColumns();
mc.Time = _columns[0];
mc.System_Description = _columns[1];
mc.User_Description = _columns[2];
list.Add(mc);
}
}
}
DataTable ListAsDataTable = BuildDataTable<MyColumns>(list);
DataView ListAsDataView = ListAsDataTable.DefaultView;
this.dataGridView1.DataSource = view = ListAsDataView;
this.dataGridView1.AllowUserToAddRows = false;
dataGridView1.ClearSelection();
}
textBox1.Text = this.dataGridView1.Rows.Count.ToString();
}
I was thinking also about using reg expresions, format of my text would look like this. Field1>(6)Field2>(23)Field3>(2)Field4>(50+) field 3 are spaces that work as delimiters, is it possible to change them to commas, while working with text??
If it is fixed-width columns, then one way would be using string.Substring and then trimming the results for each column with string.Trim.
Is it a CSV File - are all Fields delimeted with ",". If so do yourself a favor and do not attempt to parse the csv on your own - doing it right can be quite tricky.
I had greadt sucess with KBCSV a free C# library which can read csv files into datatables easily.