例如:文本文件中显示为:a b c d
d e k l
变为:
a d
b e
c k
d l
然后写入到一个文本文件中。
4 个解决方案
#1
string str = string.Empty;
string a = "abcddekl";
char[] arr = a.ToCharArray();
for (int i = 0; i < arr.Length / 2; i++)
{
str += arr[i].ToString() +arr[i + arr.Length / 2].ToString() + "<br/>";
}
Response.Write(str);
//这只是个思路,你自己写的时候要考虑,arr数组是否能被整除等
#2
他的里面有些空格啊,不好处理。
用流的方式不知道能否处理。
#3
若楼主的字符无规律该怎么办呢?
#4
private void button1_Click(object sender, EventArgs e)
{
string path = string.Format("{0}\\aaa.txt",Application.StartupPath);
StreamReader sr = new StreamReader(new FileStream(path, FileMode.Open));
string read = sr.ReadToEnd();
sr.Close();
string[] split = read.Split(new char[] { '\r','\n' }, StringSplitOptions.RemoveEmptyEntries);
string[] result = ConvertToVerticalText(split);
path = string.Format("{0}\\result.txt",Application.StartupPath);
StreamWriter sw = new StreamWriter(new FileStream(path, FileMode.CreateNew));
foreach (string s in result)
{
sw.WriteLine(s);
}
sw.Close();
}
string[] ConvertToVerticalText(string[] input)
{
int max = 0;
for (int i = 0; i < input.Length; i++)
{
max = input[i].Length > max ? input[i].Length : max;
}
string[] result = new string[max];
for (int i = 0; i < input.Length; i++)
{
for (int inputIndex = 0; inputIndex < max; inputIndex++)
{
if (inputIndex < input[i].Length)
result[inputIndex] += input[i][inputIndex];
else
result[inputIndex] += " ";
}
}
return result;
}
input file:
123456
abcde
ghija
aac
bbb
result file:
1agab
2bhab
3cicb
4dj
5ea
6
请用.....
#1
string str = string.Empty;
string a = "abcddekl";
char[] arr = a.ToCharArray();
for (int i = 0; i < arr.Length / 2; i++)
{
str += arr[i].ToString() +arr[i + arr.Length / 2].ToString() + "<br/>";
}
Response.Write(str);
//这只是个思路,你自己写的时候要考虑,arr数组是否能被整除等
#2
他的里面有些空格啊,不好处理。
用流的方式不知道能否处理。
#3
若楼主的字符无规律该怎么办呢?
#4
private void button1_Click(object sender, EventArgs e)
{
string path = string.Format("{0}\\aaa.txt",Application.StartupPath);
StreamReader sr = new StreamReader(new FileStream(path, FileMode.Open));
string read = sr.ReadToEnd();
sr.Close();
string[] split = read.Split(new char[] { '\r','\n' }, StringSplitOptions.RemoveEmptyEntries);
string[] result = ConvertToVerticalText(split);
path = string.Format("{0}\\result.txt",Application.StartupPath);
StreamWriter sw = new StreamWriter(new FileStream(path, FileMode.CreateNew));
foreach (string s in result)
{
sw.WriteLine(s);
}
sw.Close();
}
string[] ConvertToVerticalText(string[] input)
{
int max = 0;
for (int i = 0; i < input.Length; i++)
{
max = input[i].Length > max ? input[i].Length : max;
}
string[] result = new string[max];
for (int i = 0; i < input.Length; i++)
{
for (int inputIndex = 0; inputIndex < max; inputIndex++)
{
if (inputIndex < input[i].Length)
result[inputIndex] += input[i][inputIndex];
else
result[inputIndex] += " ";
}
}
return result;
}
input file:
123456
abcde
ghija
aac
bbb
result file:
1agab
2bhab
3cicb
4dj
5ea
6
请用.....