这个问题几乎与这个问题相反: C#是否内置支持解析页码字符串?
所以给定
1,3,5,6,7,8,9,10,12:
Run Code Online (Sandbox Code Playgroud)
我会输出:
1,3,5-10,12
Run Code Online (Sandbox Code Playgroud)
这是我的第一次尝试.它看起来有点像hacky,可能是我写过的最糟糕的代码.你能提出一个改进的方法吗?更好的办法吗?
static string numListToRangeStr(List<int> numList)
{
StringBuilder retString = new StringBuilder();
numList.Sort();
bool inRangeFind = false;
int firstInRange = numList[0];
int lastNumber = firstInRange;
bool first = true;
for (int i = 1; i < numList.Count; i++)
{
if (numList[i] == (lastNumber + 1))
{
inRangeFind = true;
}
else
{
if (inRangeFind)
{
if (!first)
{
retString.Append(",");
}
retString.Append(firstInRange);
retString.Append("-");
}
else
{
if (!first)
{
retString.Append(",");
}
}
retString.Append(lastNumber);
firstInRange = numList[i]; …Run Code Online (Sandbox Code Playgroud) 这是将List传递给方法并编辑该List而不修改原始List的唯一方法吗?
class CopyTest1
{
List<int> _myList = new List<int>();
public CopyTest1(List<int> l)
{
foreach (int num in l)
{
_myList.Add(num);
}
_myList.RemoveAt(0); // no effect on original List
}
}
Run Code Online (Sandbox Code Playgroud) 是否可以从用C++编写的应用程序中启动C#.Net(2.0)应用程序?
谢谢,
编辑:很酷 - 所以我只需知道应用程序的位置:
LPTSTR szCmdline = _tcsdup(TEXT("C:\\Program Files\\MyApp -L -S"));
CreateProcess(NULL, szCmdline, /* ... */);
Run Code Online (Sandbox Code Playgroud)