有了這個陣列int[]{ 1, 2, 3, 4, 7, 8, 11, 15,16,17,18 };
我如何可以轉換到這個字符串數組"1-4","7-8","11","15-18"
使用LINQ組號碼沒有間隙
建議的順序? Linq?
有了這個陣列int[]{ 1, 2, 3, 4, 7, 8, 11, 15,16,17,18 };
我如何可以轉換到這個字符串數組"1-4","7-8","11","15-18"
使用LINQ組號碼沒有間隙
建議的順序? Linq?
var array = new int[] { 1, 2, 3, 4, 7, 8, 11, 15, 16, 17, 18 };
var result = string.Join(",", array
.Distinct()
.OrderBy(x => x)
.GroupAdjacentBy((x, y) => x + 1 == y)
.Select(g => new int[] { g.First(), g.Last() }.Distinct())
.Select(g => string.Join("-", g)));
與
public static class LinqExtensions
{
public static IEnumerable<IEnumerable<T>> GroupAdjacentBy<T>(
this IEnumerable<T> source, Func<T, T, bool> predicate)
{
using (var e = source.GetEnumerator())
{
if (e.MoveNext())
{
var list = new List<T> { e.Current };
var pred = e.Current;
while (e.MoveNext())
{
if (predicate(pred, e.Current))
{
list.Add(e.Current);
}
else
{
yield return list;
list = new List<T> { e.Current };
}
pred = e.Current;
}
yield return list;
}
}
}
}
謝謝,這節省了很多時間。 – nRk 2013-11-06 08:47:40
你想實現什麼算法?找出你想要發生的事情,然後看看它是否可以通過LINQ轉換更清晰。這裏有一些非LINQ可以給你一個想法。
int[] array = { 1, 2, 3, 4, 7, 8, 11, 15, 16, 17, 18};
List<string> ranges = new List<string>();
// code assumes array is not zero-length, is distinct, and is sorted.
// to do: handle scenario as appropriate if assumptions not valid
Action<int, int, List<string>> addToRanges = (first, last, list) =>
{
if (last == first)
list.Add(last.ToString());
else
list.Add(string.Format("{0}-{1}", first, last)); ;
};
int firstItem = array[0];
int lastItem = firstItem;
foreach (int item in array.Skip(1))
{
if (item > lastItem + 1)
{
addToRanges(firstItem, lastItem, ranges);
firstItem = lastItem = item;
}
else
{
lastItem = item;
}
}
addToRanges(firstItem, lastItem, ranges);
// return ranges or ranges.ToArray()
我想實現什麼更清晰,我正在嘗試LINQ組通過但沒有成功 – Alexandre 2011-01-13 16:05:30
你不需要LINQ的;事實上,最簡單的解決方案需要知道陣列中的三個位置(您的起始編號,當前編號和當前的下一個編號),因此Enumerables並不適合。
試試這個:
var start = 0;
var end = 0;
var write = false;
var builder = new StringBuilder();
for(var i=0; i<array.Length; i++)
{
//arranged this way to avoid ArrayOutOfBoundException
//if the next index doesn't exist or isn't one greater than the current,
//the current index is the end of our incremental range.
if(i+1 == array.Length || array[i+1] > array[i] + 1)
{
end = i;
write = true;
}
if(write)
{
if(end - start == 0) //one number
builder.Append(String.Format("{0}, ", array[start]);
else //multi-number range
builder.Append(String.Format("{0}-{1}, ", array[start], array[end]);
start = i+1;
end = i+1; //not really necessary but avoids any possible case of counting backwards
write = false;
}
}
可以重新排列此減少代碼嵌套,continue
在循環邏輯早期,並刪除了幾個瓦爾;你將獲得幾毫西的執行時間。在將字符串取出之前,還需要修剪StringBuilder末尾的最後兩個字符(尾隨逗號和空格)。
_你不需要Linq_?儘管你的版本完全沒問題,但是一個意識形態的LINQ解決方案也有一個好處,你可以很容易地將這樣的解決方案移植到任何函數式編程語言,比如F#或者Haskell。 – mbx 2016-09-01 07:49:27
這裏是它切:
public static IEnumerable<string> ToRanges(this IEnumerable<int> values)
{
int? start = null, end = null;
foreach (var value in values.OrderBy(vv => vv))
{
if (!start.HasValue)
{
start = value;
}
else if (value == (end ?? start) + 1)
{
end = value;
}
else
{
yield return end.HasValue
? String.Format("{0}-{1}", start, end)
: String.Format("{0}", start);
start = value;
end = null;
}
}
if (start.HasValue)
{
yield return end.HasValue
? String.Format("{0}-{1}", start, end)
: String.Format("{0}", start);
}
}
以升序總是順序?可以重複嗎? – Ani 2011-01-13 15:49:42