简体   繁体   中英

Extract All Characters after last occurrence of a pattern C#

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

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

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.

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

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM