将一个字符串重排顺序的小问题,妹妹急求! string mid="3";string mids="1,2,3,4,5,6";现在我要的结果是"3,4,5,6,1,2";如果mid="5"那么我要结果是"5,6,1,2,3,4" 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 string mid = "5"; string mids = "1,2,3,4,5,6";System.Text.StringBuilder sb = new System.Text.StringBuilder(); string[] t = mids.Split(','); int pos = Convert.ToInt32(mid); for(int i = pos;i <= t.Length;i++) { sb.Append(t[i-1]); sb.Append(","); } for(int i = 1;i < pos;i++) { sb.Append(t[i-1]); sb.Append(","); } string newMids = sb.ToString(); newMids = newMids.Substring(0,newMids.Length - 1); Response.Write(newMids); 如果string mid="b";string mids="a,b,g,p,y,z";呢?不一定是数字啊! static void Main(string[] args){ string mid = "b"; string mids = "a,b,c,d,e"; Console.WriteLine(Order(mid, mids)); Console.Read();}static string Order(string num1, string num2){ int n1 = (int)Convert.ToChar(num1); string[] nums = num2.Split(','); int[] ns = new int[nums.Length]; for (int i = 0; i < ns.Length; i++) ns[i] = (int)Convert.ToChar(nums[i]); for( int i = 0; i < ns.Length - 1; i++) for (int j = i + 1; j < ns.Length; j++) if (ns[i] > ns[j]) { int tmp = ns[i]; ns[i] = ns[j]; ns[j] = ns[i]; } StringBuilder sbNums = new StringBuilder(); for (int i = 0; i < ns.Length; i++) if (ns[i] >= n1) { for (int j = ns.Length - 1; j >= i; j--) sbNums.Insert(0, (char)ns[j]).Insert(1, ","); break; } else sbNums.Append((char)ns[i]).Append(","); return sbNums.ToString().Substring(0, sbNums.Length - 1);} ns[j] = ns[i];-->ns[j] = tmp;//写错了 string mid="5"; string mids="1,2,3,4,5,6"; string repmids = mids.Replace(",",""); char c = Convert.ToChar(mid); string[] str = repmids.Split(c); string newmids = mid + str[1] + str[0]; string relmids = null; for(int i=0;i<newmids.Length;i++) { if(i == newmids.Length-1) relmids += newmids[i]; else relmids += newmids[i] + ","; }relmids 就是你想要的string 自己弄的在局域网聊天软件,想问一下怎么判断当有新消息的时候,跟这个用户的聊天窗口是否已经实例化? 向ArrayList中添加对象后,再怎么调用呢 picturebox清除刚刚所画的线(急) 第一次做B/S的考试系统,感觉实在费劲儿啊请大家帮帮忙 如何动态加载窗体菜单并设置菜单为不同样式 自定义控件如何设定默认的响应事件 c# 多查询方案设置 请问为何不能通过网络访问放在另一电脑的应用程序? 100分送 在C#中如何用代码改变水晶报表中的某一个标题或文本?急啊 Assembly 怎么用? C#编写的DLL如何注册,让别的程序调用?在线等。
string mids = "1,2,3,4,5,6";System.Text.StringBuilder sb = new System.Text.StringBuilder();
string[] t = mids.Split(',');
int pos = Convert.ToInt32(mid);
for(int i = pos;i <= t.Length;i++)
{
sb.Append(t[i-1]);
sb.Append(",");
}
for(int i = 1;i < pos;i++)
{
sb.Append(t[i-1]);
sb.Append(",");
} string newMids = sb.ToString();
newMids = newMids.Substring(0,newMids.Length - 1);
Response.Write(newMids);
string mid="b";
string mids="a,b,g,p,y,z";
呢?不一定是数字啊!
{
string mid = "b";
string mids = "a,b,c,d,e"; Console.WriteLine(Order(mid, mids));
Console.Read();
}static string Order(string num1, string num2)
{
int n1 = (int)Convert.ToChar(num1);
string[] nums = num2.Split(',');
int[] ns = new int[nums.Length]; for (int i = 0; i < ns.Length; i++)
ns[i] = (int)Convert.ToChar(nums[i]); for( int i = 0; i < ns.Length - 1; i++)
for (int j = i + 1; j < ns.Length; j++)
if (ns[i] > ns[j])
{
int tmp = ns[i];
ns[i] = ns[j];
ns[j] = ns[i];
} StringBuilder sbNums = new StringBuilder(); for (int i = 0; i < ns.Length; i++)
if (ns[i] >= n1)
{
for (int j = ns.Length - 1; j >= i; j--)
sbNums.Insert(0, (char)ns[j]).Insert(1, ",");
break;
}
else
sbNums.Append((char)ns[i]).Append(","); return sbNums.ToString().Substring(0, sbNums.Length - 1);
}
string mids="1,2,3,4,5,6";
string repmids = mids.Replace(",",""); char c = Convert.ToChar(mid);
string[] str = repmids.Split(c); string newmids = mid + str[1] + str[0]; string relmids = null; for(int i=0;i<newmids.Length;i++)
{
if(i == newmids.Length-1)
relmids += newmids[i];
else
relmids += newmids[i] + ",";
}relmids 就是你想要的string