更改文字显示方向
作者:互联网
using System.Text; string text = "你好,我是richard,我家在中国陕西,在没有汉字输入法起做用的时候输入的字母ladalkd,数字和字符都是半角的。"; string result = Change(text, 3, false); Console.WriteLine(result); Console.ReadKey(); static string Change(string text, int rowCount, bool fromLeft = false) { var list = text.Select(p => p.ToString()).ToList(); var yuShu = list.Count()%rowCount; for (int i = 0; i < rowCount- yuShu; i++) { list.Add(" "); } StringBuilder sb = new StringBuilder(); int rowPerCount = list.Count()%rowCount==0 ? list.Count()/rowCount : list.Count()/rowCount+1; if (fromLeft ==true) { for (int i = 0; i < rowCount; i++) { for (int j = 0; j < rowPerCount; j++) { int index = j*rowCount+i; if (index>=list.Count()) { continue; } else { sb.Append(ToSBC(list[index])); } } sb.AppendLine(); } } else { for (int i = 0; i < rowCount; i++) { for (int j = rowPerCount-1; j >=0; j--) { int index = j*rowCount+i; if (index>=list.Count()) { continue; } else { sb.Append(ToSBC(list[index])); } } sb.AppendLine(); } } return sb.ToString(); } //static string ToSBC(string input) //{ // // 半角转全角: // char[] c = input.ToCharArray(); // for (int i = 0; i < c.Length; i++) // { // if (c[i] == 32) // { // c[i] = (char)12288; // continue; // } // if (c[i] < 127) // { // c[i] = (char)(c[i] + 65248); // } // } // return new string(c); //} static string ToSBC(string input) { if (input.Length == Encoding.Default.GetByteCount(input)) { return $"{input} "; } else { return input; } }
标签:文字,index,string,更改,int,list,rowCount,方向,input 来源: https://www.cnblogs.com/nanqinling/p/16473472.html