But, my split method cannot function well. I want split my text file content from horizontal to vertical. But, my output always got problem.
or example
my text file content is like this..
eg.
1 2 3 4 5
2 3 4 5
my output wants like this
1 2
2 3
3 4
4 5
5
but my output always give me like this...this is not i want.
1
2
3
4
5
2
3
4
5
So, how to solve it? And how to show it in the textbox?? Who can help me..?
C# Syntax (Toggle Plain Text)
StringBuilder sb = new StringBuilder(); string line; string Temp = " "; if (File.Exists(fileOpen.FileName)) { StreamReader file = null; try { file = new StreamReader(fileOpen.FileName); while ((line = file.ReadLine()) != null) { sb.AppendLine(line); } // textBox1.Text = sb.ToString(); foreach (string lines in File.ReadAllLines(fileOpen.FileName)) { string[] Split = lines.Split(new string[] { " ", "\n", "\r", "\t" }, StringSplitOptions.RemoveEmptyEntries); foreach (string Splits in Split) { Temp = Temp + "\n" + Split; } //show line by line after split MessageBox.Show(Temp, "read by line"); } }
Jaganathan BantheswaranPosted Feb 26, 2011, 1:49 AM
Its working fine.
private void MakeVerticalToHorizontal()
{
char delimiter = ' ';
string fileName = @"c:\sample.txt";
string ss = string.Empty;
StringBuilder sb;
StreamReader sr = new StreamReader(fileName);
try
{
while (sr.Peek() >= 0)
{
string r = sr.ReadLine();
MessageBox.Show(r);
string[] items = r.Split(delimiter);
sb = new StringBuilder();
foreach (string ff in items)
{
sb.Append(string.Concat(ff, "\r\n"));
}
ss = sb.ToString();
MessageBox.Show(ss);
}
}
finally
{
using (StreamWriter outfile =
new StreamWriter(@"c:\sample1.txt"))
{
outfile.Write(ss.ToString());
}
sr.Close();
}
}
Use this logic and make some changes if you need.
Sam HobbsPosted Feb 26, 2011, 5:55 PM
I posted a reply more than 12 hours ago but I do not see it. I will try again.
The following works. It might be a little difficult to understand but please try to understand it. The purpose of the second and third "for" loops is to write out data that does not have a matching number on the other line. The value of n should be the number of numbers that have a matching number on the other line.
string[] first, second;
using (StreamReader sr = new StreamReader(fileName))
{
first = sr.ReadLine().Split();
second = sr.ReadLine().Split();
int n = first.Length < second.Length ? first.Length : second.Length;
for (int i = 0; i < n; ++i)
MessageBox.Show(string.Format("{0} {1}", first[i], second[i]));
for (int i = n; i < first.Length; ++i)
MessageBox.Show(string.Format(first[i]));
for (int i = n; i < second.Length; ++i)
MessageBox.Show(string.Format(second[i]));
}
FroglegPosted Feb 26, 2011, 11:46 AM
private void button1_Click(object sender, EventArgs e)
{
loadText();
}
public void loadText()
{
using (StreamReader sr = new StreamReader(@"test.txt"))
{
string line;
while ((line = sr.ReadLine()) != null)
{
samList.Add(line);
}
}
transformText();
}
public void transformText()
{
foreach (string str in samList)
{
char[] strChar = str.ToCharArray();
addToListbox(strChar);
}
}
public void addToListbox(char[] ss)
{
int chLen = ss.Length;
int count = 0;
while (count < chLen)
{
if (count + 1 < chLen)
{
listBox1.Items.Add(ss[count] + " " + ss[count + 1]);
}
else
{
listBox1.Items.Add(ss[count]+ " " + "end line" );
}
count = count + 2;
}
}