Iterate over a string?

Not quite sure if this is possible, but I will say that I have two lines:

"IAmAString-00001"
"IAmAString-00023"

What would be a quick way to iterate from IAmAString-0001 to IAmAString-00023 by moving the index of only the number at the end?

The problem is a bit more general than that, for example, the string I could deal with can be in any format, but the last character set will always be a number, something like Super_Confusing-String # w00t0003, in which case the last 0003 would be what I would use for iteration.

Any ideas?

+3
source share
8 answers
string start = "IAmAString-00001";
string end = "IAmAString-00023";

// match constant part and ending digits
var matchstart = Regex.Match(start,@"^(.*?)(\d+)$");
int numberstart = int.Parse(matchstart.Groups[2].Value);

var matchend = Regex.Match(end,@"^(.*?)(\d+)$");
int numberend = int.Parse(matchend.Groups[2].Value);

// constant parts must be the same
if (matchstart.Groups[1].Value != matchend.Groups[1].Value)
    throw new ArgumentException("");

// create a format string with same number of digits as original
string format = new string('0', matchstart.Groups[2].Length);

for (int ii = numberstart; ii <= numberend; ++ii)
    Console.WriteLine(matchstart.Groups[1].Value + ii.ToString(format));
+3
source

string.Format and the for loop should do what you want.

for(int i = 0; i <=23; i++)
{
    string.Format("IAmAString-{0:D4}",i);
}

or something close to this (not sitting in front of the compiler).

+6
source

char.IsDigit:

    static void Main(string[] args)
    {
        var s = "IAmAString-00001";
        int index = -1;
        for (int i = 0; i < s.Length; i++)
        {
            if (char.IsDigit(s[i]))
            {
                index = i;
                break;
            }
        }
        if (index == -1)
            Console.WriteLine("digits not found");
        else
            Console.WriteLine("digits: {0}", s.Substring(index));
    }

:

digits: 00001
+6

Regex:

var match=Regex.Match("Super_Confusing-String#w00t0003",@"(?<=(^.*\D)|^)\d+$");
if(match.Success)
{
    var val=int.Parse(match.Value);
    Console.WriteLine(val);
}

, , , :

var match=Regex.Match(
    "Super_Confusing-String#w00t0003",
    @"(?<prefix>(^.*\D)|^)(?<digits>\d+)$");

if(match.Success)
{
    var prefix=match.Groups["prefix"].Value;
    Console.WriteLine(prefix);
    var val=int.Parse(match.Groups["digits"].Value);
    Console.WriteLine(val);
}
+3

, 5 - , :

string prefix = "myprefix-";
for (int i=1; i <=23; i++)
{
  Console.WriteLine(myPrefix+i.ToString("D5"));
}
+2

.

private int FindTrailingNumber(string str)
{
    string numString = "";
    int numTest;
    for (int i = str.Length - 1; i > 0; i--)
    {
        char c = str[i];
        if (int.TryParse(c.ToString(), out numTest))
        {
            numString = c + numString;
        }
    }
    return int.Parse(numString);
}

, , .

string s1 = "asdf123";
string s2 = "asdf127";
int num1 = FindTrailingNumber(s1);
int num2 = FindTrailingNumber(s2);

string strBase = s1.Replace(num1.ToString(), "");

for (int i = num1; i <= num2; i++)
{
    Console.WriteLine(strBase + i.ToString());
}
+2

, , ( , : -))

static void Main(string[] args)
    {
        var s = "IAmAString-00001";
        int index = -1;
        for (int i = s.Length - 1; i >=0; i--)
        {
            if (!char.IsDigit(s[i]))
            {
                index = i;
                break;
            }
        }
        if (index == -1)
            Console.WriteLine("digits not found");
        else
            Console.WriteLine("digits: {0}", s.Substring(index));
        Console.ReadKey();
    }

+2

X- , :

int x = 5;
string s = "IAmAString-00001";
int num = int.Parse(s.Substring(s.Length - x, x));
Console.WriteLine("Your Number is: {0}", num);

3, 4 5 , :

int x = 0;
string s = "IAmAString-00001";
foreach (char c in s.Reverse())//Use Reverse() so you start with digits only.
{
    if(char.IsDigit(c) == false)
        break;//If we start hitting non-digit characters, then exit the loop.
    ++x;
}
int num = int.Parse(s.Substring(s.Length - x, x));
Console.WriteLine("Your Number is: {0}", num);

RegEx. - , . , RegEx , . , , , , .

: , "I 2 AmAString-000001", "2000001" "1",.

+2

All Articles