如果数据量比较大,可以用stringbuilder和stringreader来做,效率会好一点可以排中英文混合,外加分段的,且左右对齐都行 private string MakeItLooksLikeAncientChinese(string origin, int rowCount, char dividedSymbol) { StringReader strReader = new StringReader(origin); StringBuilder[] strBuilder = new StringBuilder[rowCount]; for (int i = 0; i < rowCount; i++) { strBuilder[i] = new StringBuilder(); } string dsymbol = dividedSymbol.ToString(); char[] aLineChar; string aLine; int columnCount; while (true) { //逐行读入 aLine = strReader.ReadLine(); if (aLine != null) { aLineChar = aLine.ToCharArray(); columnCount = (aLine.Length - 1) / rowCount + 1; for (int i = 0; i < columnCount * rowCount; i++) { if (i < aLine.Length) { if ((int)aLineChar[i] > 255) { //如果是中文直接竖排 strBuilder[i % rowCount].Insert(0, dsymbol + aLineChar[i]); } else { //如果是英文符号,添加空格 strBuilder[i % rowCount].Insert(0, dsymbol + aLineChar[i] + " "); } } else { //补齐后面行的空格 strBuilder[i % rowCount].Insert(0,dsymbol + " "); } } } else { break; } } strBuilder[0].AppendLine(); for (int i = 1; i < rowCount; i++) { strBuilder[0].AppendLine(strBuilder[i].ToString()); } return strBuilder[0].ToString(); }
static void Main() { Console.WriteLine(Rotate("It's a test这是一个测试", 4)); Console.WriteLine(Rotate("中华人民共和国 china", 5)); Console.WriteLine(Rotate("鸟夜啼 黄云城边乌欲栖归飞哑哑枝上啼机中织锦秦川女碧纱如烟隔窗语停梭怅然忆远人独宿孤房泪如雨", 7)); Console.ReadKey(); }static string Rotate(string text, int height) { text = text.Replace(' ', ' '); text = Regex.Replace(text, "[\x21-\x7e]", delegate(Match m) { return ((char)(m.Value[0] + 65248)).ToString(); }); int width = (int)Math.Ceiling(1.0 * text.Length / height); char[] chars = new String(' ', width * height).ToCharArray(); for (int i = 0; i < text.Length; i++) { chars[((i % height) * width) + (i / height)] = text[i]; } string result = Regex.Replace(new String(chars), ".{" + width + "}(?!$)", "$0\r\n"); result = Regex.Replace(result, @".(?!($|[\r\n]))", "$0┆"); result = Regex.Replace(result, ".{" + (width * 2 - 1) + "}", "│$0│"); string top = "", bottom = ""; for (int i = 0; i < width; i++) { top += "┬─"; bottom += "┴─"; } top = top.Remove(0, 1); bottom = bottom.Remove(0, 1); result = "┏" + top + "┓\r\n" + result + "\r\n┗" + bottom + "┛"; return result; }// 输出: /* ┏─┬─┬─┬─┬─┓ │I┆ ┆t┆这┆测│ │t┆a┆e┆是┆试│ │'┆ ┆s┆一┆ │ │s┆ ┆t┆个┆ │ ┗─┴─┴─┴─┴─┛ ┏─┬─┬─┓ │中┆和┆c│ │华┆国┆h│ │人┆ ┆i│ │民┆ ┆n│ │共┆ ┆a│ ┗─┴─┴─┛ ┏─┬─┬─┬─┬─┬─┬─┓ │鸟┆黄┆归┆机┆碧┆停┆独│ │夜┆云┆飞┆中┆纱┆梭┆宿│ │啼┆城┆哑┆织┆如┆怅┆孤│ │ ┆边┆哑┆锦┆烟┆然┆房│ │ ┆乌┆枝┆秦┆隔┆忆┆泪│ │ ┆欲┆上┆川┆窗┆远┆如│ │ ┆栖┆啼┆女┆语┆人┆雨│ ┗─┴─┴─┴─┴─┴─┴─┛ */
static void Main(string[] args) { string str = "静夜思\n李白\n床前明月光,\n疑是地上霜。\n举头望明月,\n低头思故乡。"; Console.WriteLine(PrickUp(str)); Console.ReadKey(); } static string PrickUp(string str) { string newstring = null; string[] st = str.Split('\n');//获取每句诗 int x = 0, y = 0, z;; while (true)//对每行每个字进行循环 { for (int i = st.Length-1 ; i >= 0; i--)//对每行的第X字进行循环 { if (st[i].Length > x) newstring += st[i][x].ToString(); else y++; } if (x == st.Length) break; newstring += "\n"; x++; y = 0; } return newstring; }郁闷结果是:低举疑床李静 头头是前白夜 思望地明思 故明上月 乡月霜光 。,。,硬要的话,只有在循环之前,填充空白,使得所有的字符串长度相同。
using System; using System.Collections.Generic;namespace 文字竖排 { class Program { static void Main(string[] args) { string str = "静夜思\n李白\n床前明月光,\n疑是地上霜。\n举头望明月,\n低头思故乡。\n床前明月光,\n疑是地上霜。\n举头望明月,\n低头思故乡。\n床前明月光,\n疑是地上霜。\n举头望明月,\n低头思故乡。"; Console.WriteLine(PrickUp(str)); Console.ReadKey(); } static string PrickUp(string str) { List<string> strs = new List<string>(); string[] st = str.Split('\n');//获取每句诗 int x = 0, y = 0, z; ; while (true)//对每行每个字进行循环 { string newstring = null; for (int i = st.Length - 1; i >= 0; i--)//对每行的第X字进行循环 if (st[i].Length > x) newstring += st[i][x].ToString() + "|"; else { y++; newstring += "|"; } if (y == st.Length) break;//放所有已浏览了所有字 while (newstring.Contains("||"))//X行的字比X+1的字还多,竖起便会靠边,应在中间加入空格 newstring = newstring.Replace("||", "| |"); if (newstring.StartsWith("|")) newstring = " " + newstring;//防止有一个非最后一句诗的句子的长度大于其他诗句 strs.Add(newstring); x++; y = 0; } string result = null; strs.ForEach(delegate(string s) { result += s + "\n"; }); return result; } } }至于英文字母,不再考虑当中……因为这个是诗歌……运行结果:低|举|疑|床|低|举|疑|床|低|举|疑|床|李|静| 头|头|是|前|头|头|是|前|头|头|是|前|白|夜| 是|望|地|明|思|望|地|明|思|望|地|明| |思| 地|是|上|月|故|明|上|月|故|是|上|月| | | 思|地|霜|光|乡|月|霜|光|乡|地|是|光| | | 故|是|。|,|。|,|。|,|。|明|地|,| | | 乡|地| | | | | | | |月|霜| | | | 。|是| | | | | | | |,|。| | | | |地| | | | | | | | | | | | | |明| | | | | | | | | | | | | |月| | | | | | | | | | | | | |,| | | | | | | | | | | | |
这种题最喜欢,Python来也:def main(offset=6): string = u'静夜思 李白床前明月光,疑似地上霜。举头望明月,低头思故乡。090131' a = [[' ']*offset for row in xrange(offset)] for i in xrange(offset): for j in xrange(offset): a[i][j] = string[j + i*offset] b = [[r[col] for r in a[::-1]] for col in xrange(len(a[0]))] print '\n'.join(['|'.join(unicode(c) for c in row)for row in b]) 0|低|举|疑|床|静 9|头|头|似|前|夜 0|思|望|地|明|思 1|故|明|上|月| 3|乡|月|霜|光|李 1|。|,|。|,|白
应该是 文字竖排,sorry
private string MakeItLooksLikeAncientChinese(string origin, int column, char DividedSymbols)
{
int row = (origin.Length - 1) / column;
string[] lines = new string[column];
char[] originChar = origin.ToCharArray();
string result = ""; for (int i = origin.Length - 1; i >= 0 ; i--)
{
if (i < row * column)
{
lines[i % column] += DividedSymbols.ToString() + originChar[i];
}
else
{
lines[i % column] += originChar[i];
}
} for (int i = 0; i < column; i++)
{
lines[i] += Environment.NewLine;
result += lines[i];
} return result;
}
private string MakeItLooksLikeAncientChinese(string origin, int rowCount, char DividedSymbols)
{
int columnCount = (origin.Length - 1) / rowCount;
string[] lines = new string[rowCount];
char[] originChar = origin.ToCharArray();
string result = ""; for (int i = origin.Length - 1; i >= 0 ; i--)
{
if (i < columnCount * rowCount)
{
lines[i % rowCount] += DividedSymbols.ToString();
} if ((int)originChar[i] > 255)
{
lines[i % rowCount] += originChar[i];
}
else
{
lines[i % rowCount] += originChar[i] + " ";
}
} for (int i = 0; i < rowCount; i++)
{
lines[i] += Environment.NewLine;
result += lines[i];
} return result;
}
┊ ┊ ┊头┊头┊是┊前┊李┊静┊
┊ ┊ ┊思┊望┊地┊明┊白┊夜┊
┊ ┊ ┊故┊明┊上┊月┊ ┊思┊
┊ ┊ ┊乡┊月┊霜┊光┊ ┊ ┊
┊ ┊ ┊.┊,┊.┊,┊ ┊ ┊
┊ ┊ ┊ ┊ ┊e┊a┊ ┊ ┊
┊ ┊ ┊ ┊ ┊f┊b┊ ┊ ┊
┊ ┊ ┊ ┊ ┊g┊c┊ ┊ ┊
┊ ┊ ┊ ┊ ┊ ┊ ┊ ┊ ┊SWPU 烟锁重楼
{
int length=origin.Length;
int row=origin.Length/column;
if(length%column!=0) row=row+1;
string result=DividedSymbols.ToString();
char ch;
for(int i=0;i<column;i++)
{
for(int j=0;j<row;j++)
{
int index=column*(row-j-1)+i;
if(index<length&&index>-1)
ch=origin[index];
else ch=' ';
if(ch>255)
result+=ch.ToString()+DividedSymbols;
else result+=ch.ToString()+" "+DividedSymbols;
}
if(i!=column-1)
result+="\r\n"+DividedSymbols;
}
return result;
}
private string MakeItLooksLikeAncientChinese(string origin, int rowCount, char dividedSymbol)
{
StringReader strReader = new StringReader(origin);
StringBuilder[] strBuilder = new StringBuilder[rowCount]; for (int i = 0; i < rowCount; i++)
{
strBuilder[i] = new StringBuilder();
} string dsymbol = dividedSymbol.ToString();
char[] aLineChar;
string aLine;
int columnCount; while (true)
{
//逐行读入
aLine = strReader.ReadLine();
if (aLine != null)
{
aLineChar = aLine.ToCharArray();
columnCount = (aLine.Length - 1) / rowCount + 1;
for (int i = 0; i < columnCount * rowCount; i++)
{
if (i < aLine.Length)
{
if ((int)aLineChar[i] > 255)
{
//如果是中文直接竖排
strBuilder[i % rowCount].Insert(0, dsymbol + aLineChar[i]);
}
else
{
//如果是英文符号,添加空格
strBuilder[i % rowCount].Insert(0, dsymbol + aLineChar[i] + " ");
}
}
else
{
//补齐后面行的空格
strBuilder[i % rowCount].Insert(0,dsymbol + " ");
}
}
}
else
{
break;
}
} strBuilder[0].AppendLine(); for (int i = 1; i < rowCount; i++)
{
strBuilder[0].AppendLine(strBuilder[i].ToString());
} return strBuilder[0].ToString();
}
static void Main()
{
Console.WriteLine(Rotate("It's a test这是一个测试", 4));
Console.WriteLine(Rotate("中华人民共和国 china", 5));
Console.WriteLine(Rotate("鸟夜啼 黄云城边乌欲栖归飞哑哑枝上啼机中织锦秦川女碧纱如烟隔窗语停梭怅然忆远人独宿孤房泪如雨", 7));
Console.ReadKey();
}static string Rotate(string text, int height)
{
text = text.Replace(' ', ' ');
text = Regex.Replace(text, "[\x21-\x7e]", delegate(Match m) { return ((char)(m.Value[0] + 65248)).ToString(); });
int width = (int)Math.Ceiling(1.0 * text.Length / height);
char[] chars = new String(' ', width * height).ToCharArray();
for (int i = 0; i < text.Length; i++)
{
chars[((i % height) * width) + (i / height)] = text[i];
}
string result = Regex.Replace(new String(chars), ".{" + width + "}(?!$)", "$0\r\n");
result = Regex.Replace(result, @".(?!($|[\r\n]))", "$0┆");
result = Regex.Replace(result, ".{" + (width * 2 - 1) + "}", "│$0│");
string top = "", bottom = "";
for (int i = 0; i < width; i++)
{
top += "┬─";
bottom += "┴─";
}
top = top.Remove(0, 1);
bottom = bottom.Remove(0, 1);
result = "┏" + top + "┓\r\n" + result + "\r\n┗" + bottom + "┛"; return result;
}// 输出:
/*
┏─┬─┬─┬─┬─┓
│I┆ ┆t┆这┆测│
│t┆a┆e┆是┆试│
│'┆ ┆s┆一┆ │
│s┆ ┆t┆个┆ │
┗─┴─┴─┴─┴─┛
┏─┬─┬─┓
│中┆和┆c│
│华┆国┆h│
│人┆ ┆i│
│民┆ ┆n│
│共┆ ┆a│
┗─┴─┴─┛
┏─┬─┬─┬─┬─┬─┬─┓
│鸟┆黄┆归┆机┆碧┆停┆独│
│夜┆云┆飞┆中┆纱┆梭┆宿│
│啼┆城┆哑┆织┆如┆怅┆孤│
│ ┆边┆哑┆锦┆烟┆然┆房│
│ ┆乌┆枝┆秦┆隔┆忆┆泪│
│ ┆欲┆上┆川┆窗┆远┆如│
│ ┆栖┆啼┆女┆语┆人┆雨│
┗─┴─┴─┴─┴─┴─┴─┛
*/
{
string str = "静夜思\n李白\n床前明月光,\n疑是地上霜。\n举头望明月,\n低头思故乡。";
Console.WriteLine(PrickUp(str));
Console.ReadKey();
} static string PrickUp(string str)
{
string newstring = null;
string[] st = str.Split('\n');//获取每句诗
int x = 0, y = 0, z;;
while (true)//对每行每个字进行循环
{
for (int i = st.Length-1 ; i >= 0; i--)//对每行的第X字进行循环
{
if (st[i].Length > x)
newstring += st[i][x].ToString();
else y++; }
if (x == st.Length) break;
newstring += "\n";
x++;
y = 0;
}
return newstring;
}郁闷结果是:低举疑床李静
头头是前白夜
思望地明思
故明上月
乡月霜光
。,。,硬要的话,只有在循环之前,填充空白,使得所有的字符串长度相同。
using System.Collections.Generic;namespace 文字竖排
{
class Program
{
static void Main(string[] args)
{
string str = "静夜思\n李白\n床前明月光,\n疑是地上霜。\n举头望明月,\n低头思故乡。\n床前明月光,\n疑是地上霜。\n举头望明月,\n低头思故乡。\n床前明月光,\n疑是地上霜。\n举头望明月,\n低头思故乡。";
Console.WriteLine(PrickUp(str));
Console.ReadKey();
} static string PrickUp(string str)
{ List<string> strs = new List<string>();
string[] st = str.Split('\n');//获取每句诗
int x = 0, y = 0, z; ;
while (true)//对每行每个字进行循环
{
string newstring = null;
for (int i = st.Length - 1; i >= 0; i--)//对每行的第X字进行循环
if (st[i].Length > x)
newstring += st[i][x].ToString() + "|";
else
{
y++;
newstring += "|";
} if (y == st.Length) break;//放所有已浏览了所有字 while (newstring.Contains("||"))//X行的字比X+1的字还多,竖起便会靠边,应在中间加入空格
newstring = newstring.Replace("||", "| |"); if (newstring.StartsWith("|")) newstring = " " + newstring;//防止有一个非最后一句诗的句子的长度大于其他诗句 strs.Add(newstring);
x++;
y = 0;
} string result = null;
strs.ForEach(delegate(string s)
{
result += s + "\n";
}); return result;
}
}
}至于英文字母,不再考虑当中……因为这个是诗歌……运行结果:低|举|疑|床|低|举|疑|床|低|举|疑|床|李|静|
头|头|是|前|头|头|是|前|头|头|是|前|白|夜|
是|望|地|明|思|望|地|明|思|望|地|明| |思|
地|是|上|月|故|明|上|月|故|是|上|月| | |
思|地|霜|光|乡|月|霜|光|乡|地|是|光| | |
故|是|。|,|。|,|。|,|。|明|地|,| | |
乡|地| | | | | | | |月|霜| | | |
。|是| | | | | | | |,|。| | | |
|地| | | | | | | | | | | | |
|明| | | | | | | | | | | | |
|月| | | | | | | | | | | | |
|,| | | | | | | | | | | | |
string = u'静夜思 李白床前明月光,疑似地上霜。举头望明月,低头思故乡。090131'
a = [[' ']*offset for row in xrange(offset)]
for i in xrange(offset):
for j in xrange(offset):
a[i][j] = string[j + i*offset] b = [[r[col] for r in a[::-1]] for col in xrange(len(a[0]))]
print '\n'.join(['|'.join(unicode(c) for c in row)for row in b])
0|低|举|疑|床|静
9|头|头|似|前|夜
0|思|望|地|明|思
1|故|明|上|月|
3|乡|月|霜|光|李
1|。|,|。|,|白
┊ ┊1┊头┊。┊月┊夜┊
┊ ┊3┊思┊举┊光┊思┊
┊ ┊1┊故┊头┊,┊ ┊
┊ ┊ ┊乡┊望┊疑┊李┊
┊ ┊ ┊。┊明┊似┊白┊
┊ ┊ ┊0┊月┊地┊床┊
┊ ┊ ┊9┊,┊上┊前┊SWPU 烟锁重楼