A quickie with regular expressions:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace IncStr
{
class Program
{
static void Main(string[] args)
{
//string test1 = "ab12cd-34e";
string test2 = "a1.9b";
//string test3 = "ab12cd34e66";
string pattern = "[-+]?[0-9]+";
MatchCollection matches = Regex.Matches(test2, pattern);
if (matches == null || matches.Count == 0) return;
foreach (Match match in matches)
{
Console.WriteLine("{0}Match index: {1}", Environment.NewLine, match.Index);
Console.WriteLine("{0}Match value: {1}", Environment.NewLine, match.Value);
Console.WriteLine("{0}Match length: {1}", Environment.NewLine, match.Length);
}
// work with the last match only...
Match lastMatch = matches[matches.Count - 1];
int testIncrement = 2;
int newValue;
bool result = int.TryParse(lastMatch.Value, out newValue);
string replacedString = test2.Substring(0, lastMatch.Index) +
(newValue + testIncrement) +
test2.Substring(lastMatch.Index + lastMatch.Length);
Console.WriteLine(replacedString);
Console.ReadLine();
}
}
}