Author Topic: Increment a Number in a String  (Read 8811 times)

0 Members and 1 Guest are viewing this topic.

SteveK

  • Guest
Re: Increment a Number in a String
« Reply #15 on: July 29, 2010, 11:52:03 AM »
how about:
Code: [Select]
Console.WriteLine(Incr("asdf0004", -5));~'J'~
At the moment I'm not sure what is better for my purposes, to go into the negatives or to floor at 0, but since I said "stop at 0" near the start of the thread I've stayed with that. In time it might change.

Glenn R

  • Guest
Re: Increment a Number in a String
« Reply #16 on: July 30, 2010, 05:53:18 AM »
A quickie with regular expressions:
Code: [Select]

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();
        }
    }
}

SteveK

  • Guest
Re: Increment a Number in a String
« Reply #17 on: July 30, 2010, 07:31:52 AM »
Thanks Glenn.  :-)
It doesn't keep zero prefixes, but I'm happy to see a regex example anyway.

Glenn R

  • Guest
Re: Increment a Number in a String
« Reply #18 on: July 30, 2010, 07:37:11 AM »
It's just an example to show what's possible. It's up to you or somebody else to come up with the arcane regex incantation needed :D

SteveK

  • Guest
Re: Increment a Number in a String
« Reply #19 on: July 30, 2010, 10:34:34 AM »
the arcane regex incantation needed :D
That's exactly what I thought when I was trying to work it out - by the time I learn the required regex combination I could have twice written something from scratch that does it. Probably worthwhile to know, but it's a language inside a language.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Increment a Number in a String
« Reply #20 on: July 31, 2010, 04:35:48 AM »
noticed this a couple of weeks ago
Regex Editor Add-on
http://visualstudiogallery.msdn.microsoft.com/en-us/55c24bf1-2636-4f94-831d-28db8505ce00?SRC=VSIDE

Haven't made time to look at it yet.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.