views:

1187

answers:

7

Hello,

The strings are of the following pattern

1.0.0.0
1.0.0.1
1.0.0.2
...
...
...

I am looking for a code which will read the last created string and increment the last numeral by 1 and save it as a new string.

How do I do it?

Best Regards,

Magic

+5  A: 

You can split the string into the components, parse the last one so that you can increase it, and put them together again:

string[] parts = version.Split('.');
parts[3] = (Int32.Parse(parts[3]) + 1).ToString();
version = String.Join(".", parts);

Another method that may be slightly more efficient is to get only the last component using string operations:

int pos = version.LastIndexOf('.') + 1;
int num = Int32.Parse(version.Substring(pos));
version = version.Substring(0, pos) + num.ToString();
Guffa
+1  A: 
public string DoMagic(string s)
{
 string t = s.Substring(s.LastIndexOf(' ')+1);
 return t.Substring(0, t.Length-1) + (int.Parse(t[t.Length-1].ToString())+1).ToString();
}
bassfriend
+1 for what I assume is a pun? (OP's name is Magic)
Tim Coker
A: 

Assuming your string list is:

List<string> stringList;

You could obtain the last entry in that list using:

string lastString = stringList[stringList.Length - 1];

Then get the last character of that string using:

char c = lastString[lastString.Length - 1];

Convert and increment the char into a decimal:

int newNum = Int32.Parse(c.ToString()) + 1;

Finally, copy the original string and replace the last number with the new one:

string finalString = lastString;
finalString[finalString.Length - 1] = c;

Now add this back to the original list:

stringList.Add(finalString);
bufferz
A: 

Assuming that only the last element in the sub strings vary:

List<string> items = GetItems();
string[] max = input.Split(' ').Max().Split('.');
string next = string.Format("{0}.{1}.{2}.{3}", max[0], max[1], max[2], int.Parse(max[3]) + 1);
Fredrik Mörk
A: 

I'm using VB right now, but the translation to C# should be straightforward. From here implementing your actual problem should be straightforward - you have a collection, the last item is the last number, just increment it and replace the last item of the collection and write it out again.

Imports System.Text.RegularExpressions

Module Module1

Sub Main()
    Dim matchC As MatchCollection = Regex.Matches("111.222.333", "\d+")
    Dim i As Integer = 1
    For Each x In matchC
        Console.Write(i.ToString & " ")
        Console.WriteLine(x)
        i = i + 1
    Next
    ' remember to check the case where no matches occur in your real code.
    Console.WriteLine("last number is " & matchC.Item(matchC.Count - 1).ToString)
    Console.ReadLine()
End Sub

End Module

Larry Watanabe
+1  A: 

Assuming that the format will not change, this may be your best solution. This will work even with unordered version list strings.

        string VersionList = "1.0.0.0 1.0.0.1 1.0.0.2";

        List<Version> Versions = new List<Version>();

        foreach (string FlatVersion in VersionList.Split(' '))
            Versions.Add(new Version(FlatVersion));

        Versions.Sort(); Versions.Reverse();

        Version MaximumVersion = Versions[0];

        Version NewVersion = new Version(
            MaximumVersion.Major, 
            MaximumVersion.MajorRevision,
            MaximumVersion.Minor,
            MaximumVersion.MinorRevision + 1);
Robert Venables
This is a good solution. If any of the strings contains an extra dot, or a non numeric character, this will fail. But if all the strings are in a correct format, it will work very well.
configurator
I wouldn't do Versions.Reverse() though - it's a waste of cpu. Simple Version maxVersion = Versions[Versions.Count - 1];
configurator
True. Both List(T).Item and List(T).Count() are O(1). List.Reverse() is O(n).
Robert Venables
A: 

If your intention is to always get the last substring after a specific character (excluding the delimiter character of course (in this case a period)), which I believe is your intention:

Keep it simple with a 1 liner:

string myLastSubString = myStringToParse.Split('.').Last();

I'll let the other posts answer the rest of your query.

SCL