public static string GetLastNum(string str)
{
if (string.IsNullOrEmpty(str)) throw new ArgumentNullException("参数不能为空");
string found = string.Empty;
bool hasFound = false;
string finalResult = "0";
foreach (char t in str)
{
if (char.IsNumber(t))
{
found += t;
hasFound = true;
}
else
{
if (!hasFound) continue;
finalResult = found;
hasFound = false;
found = string.Empty;
}
}
return finalResult;
}
/// <summary>
/// NOTICE:
/// This algorithm is used to do seperate sorting by a certain number.
/// The thought is:
/// 1) Set low or high at the top/bottom of your array.
/// 2) We MUST limit that low is smaller than high, otherwise the loop breaks.
/// 3) Put two parallel loop from head to bottom/from bottom to head to check whether we have numbers invalid, and then do sorting.
/// 4) The worse thing is that the number is totally unsorted, or the array is already sorted. This takes "2n" in time, so the time complex is O(n), while the S is S(1)
/// </summary>
/// <param name="numbers">
/// All the numbers to be sorted
/// </param>
/// <param name="compareNum">
/// The seperator number.
/// </param>
/// 10, 1000, 400, 4000, -111
static void SortByNum(int[] numbers, int compareNum = 1000)
{
int low = 0, high = numbers.Length - 1;
int temp = 0;
do
{
if (numbers[low] < compareNum && numbers[high] >= compareNum)
{
temp = numbers[low];
numbers[low] = numbers[high];
numbers[high] = temp;
low++;
high--;
}
else if (numbers[low] < compareNum && numbers[high] < compareNum)
{
high--;
}
else if (numbers[low] >= compareNum && numbers[high] >= compareNum)
{
low++;
}
else
{
low++;
high--;
}
} while (low < high);
}