首页 新闻 会员 周边 捐助

C# 字符串的重新组合的问题

0
悬赏园豆:50 [已解决问题] 解决于 2013-08-19 16:08

我有一个问题,就是将一串字符,按照数字的大小重新组合

比如一个字符串:“1,3,4,5,8,9,11”

按照相临的数字是否连续,重新显示成“1,3-5,8-9,11”

请高手能给出原程序,十分感谢!

xiangers的主页 xiangers | 初学一级 | 园豆:158
提问于:2013-08-17 06:35
< >
分享
最佳答案
0

/// <summary>
/// 按照升序排列的字符串,根据是否数字大小相连,重新显示字符串
/// </summary>
/// <param name="str">当前字符串</param>
/// <returns>返回新排列的字符串</returns>
public string GetStringNewList(string str)
{
string ret = "";
// string str = "1,2,3,4,5,8,9,11,12";
string[] arr = str.Split(',');

ret = arr[0];

for (int i = 1; i < arr.Length; i++)
{
int pre = int.Parse(arr[i - 1]);

int current = int.Parse(arr[i]);

int next = 0;

if (i < arr.Length - 1)
{
next = int.Parse(arr[i + 1]);

if (current - 1 == pre)
{
if (current + 1 == next)
{

}
else
{
ret += "-" + arr[i];
}
}
else
{
if (current + 1 == next)
{
ret += "," + arr[i];
}
else
{
ret += "," + arr[i];
}
}
}
else
{
if (current - 1 == pre)
{

ret += "-" + arr[i];
}
else
{

ret += "," + arr[i];
}
}
}
return ret;
}

xiangers | 初学一级 |园豆:158 | 2013-08-19 16:06
其他回答(4)
0

有预先排序好么

mushishi | 园豆:430 (菜鸟二级) | 2013-08-17 08:44

static void Main(string[] args)
{

string strA = "1,3,4,5,8,9,11";
string[] strArr = strA.Split(',');
string strB = "";

int numFrom = int.Parse(strArr[0]);
int numTo = numFrom;
for (int i = 0; i < strArr.Length - 1; i++)
{

int intA = int.Parse(strArr[i]);
int intB = int.Parse(strArr[i + 1]);

if (intB - intA == 1)
{
numTo = intB;
}
else
{

if (numFrom == numTo)
{
strB += numFrom + ",";
}
else
{
strB += numFrom + "-" + numTo + ",";
}
numFrom = intB;
}
if (i == strArr.Length - 2)
{
numTo = int.Parse(strArr[strArr.Length - 1]);
if (numFrom == numTo)
{
strB += numFrom + ",";
}
else
{
strB += numFrom + "-" + numTo + ",";
}
}

}

Console.WriteLine(strA);
Console.Write(strB);

Console.ReadKey();
}

支持(0) 反对(0) mushishi | 园豆:430 (菜鸟二级) | 2013-08-17 09:27

记得去掉最后面的那个逗号

支持(0) 反对(0) mushishi | 园豆:430 (菜鸟二级) | 2013-08-17 09:32

numFrom = intB;
numTo = intB;

 

不好意思 不小心删掉了这句       

支持(0) 反对(0) mushishi | 园豆:430 (菜鸟二级) | 2013-08-17 09:43
 static void Main(string[] args)
        {

            string strA = "1,3,4,5,8,9,11";
            string[] strArr = strA.Split(',');
            string strB = "";

            int numFrom = int.Parse(strArr[0]);
            int numTo = numFrom;
            for (int i = 0; i < strArr.Length - 1; i++)
            {

                int intA = int.Parse(strArr[i]);
                int intB = int.Parse(strArr[i + 1]);

                if (intB - intA == 1)
                {
                    numTo = intB;
                }
                else
                {

                    if (numFrom == numTo)
                    {
                        strB += numFrom + ",";
                    }
                    else
                    {
                        strB += numFrom + "-" + numTo + ",";
                    }
                    numFrom = intB;
                    numTo = intB;
                }
                if (i == strArr.Length - 2)
                {
                    numTo = int.Parse(strArr[strArr.Length - 1]);
                    if (numFrom == numTo)
                    {
                        strB += numFrom + ",";
                    }
                    else
                    {
                        strB += numFrom + "-" + numTo + ",";
                    }
                }

            }

            Console.WriteLine(strA);
            Console.Write(strB);

            Console.ReadKey();
        }
支持(0) 反对(0) mushishi | 园豆:430 (菜鸟二级) | 2013-08-17 09:43
1
public class KeyItems
    {
        List<int> keys = new List<int>();
        Dictionary<int, List<int>> result = new Dictionary<int, List<int>>();
        public KeyItems(string keyStr)
        {
            keys = keyStr.Split(',').Select(n => int.Parse(n)).ToList();

            Compare();
        }

        public override string ToString()
        {
            return string.Join(",", result
                .Select(n => n.Value.Count == 1 ? n.Value[0].ToString() : string.Format("{0}-{1}", n.Value[0].ToString(), n.Value[n.Value.Count - 1].ToString())).ToArray());
        }
        void Compare()
        {
            int key = -1;
            for (int i = 0; i < keys.Count; i++)
            {
                if (Compare(keys[i], i==keys.Count-1?Convert.DBNull as int?:keys[i + 1]))
                {
                    if (key == -1) key = i;
                    if (result.Keys.Contains(key))
                    {
                        result[key].Add(keys[i]);
                    }
                    else
                    {
                        result.Add(i, new List<int> { keys[i] });
                    }
                }
                else
                {
                    if (key != -1)
                    {
                        result[key].Add(keys[i]);
                        key = -1;
                    }
                    else
                    {
                        result.Add(i, new List<int> { keys[i] });
                    }
                }
            }
        }

        bool Compare(int? a, int? b)
        {
            if (b.HasValue)
            {
                if (b - a == 1) return true;
            }
            return false;
        }




string text = "1,3,4,5,6,8,9,11,13,15,16,17,18,20";

MessageBox.Show(new KeyItems(text).ToString());
收获园豆:30
Yu | 园豆:12990 (专家六级) | 2013-08-17 09:35

这是传说中的狼不大表达式么    看不懂额

支持(0) 反对(0) mushishi | 园豆:430 (菜鸟二级) | 2013-08-17 09:54
0

标准的递归题目,此题用递归甚是合适,代码应该比楼上各位的少得多。

空明流光 | 园豆:111 (初学一级) | 2013-08-17 14:32

请问怎么递归?

支持(0) 反对(0) mushishi | 园豆:430 (菜鸟二级) | 2013-08-19 08:22
0
var str = "1,3,4,5,8,9,13,14,15,16";
var array = str.Split(',').Select(t => Convert.ToInt32(t)).ToArray();
IDictionary<int, List<int>> keys = new Dictionary<int, List<int>>();
int key = Int32.MinValue;
for (var i = 0; i < array.Count(); i++)
{
     if (i == 0 || (array[i] - array[i - 1] > 1))
     {
          key = array[i];
          keys.Add(array[i], new List<int> { array[i] });
     }
     else
     {
          keys[key].Add(array[i]);
      }
}
return string.Join(",",
             keys.Select(t => t.Value.Count == 1 ? t.Key.ToString() : t.Value.First() + "-" + t.Value.Last())).ToArray());

    
收获园豆:20
shoy160 | 园豆:218 (菜鸟二级) | 2013-08-17 14:38
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册