简体   繁体   English

最后一次出现模式C#后提取所有字符

[英]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. 我正在寻找一个代码,它将读取最后创建的字符串并将最后一个数字递增1并将其另存为新字符串。

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: 使用1个衬垫保持简单:

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: 将char转换并递增为十进制:

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. 我现在正在使用VB,但是对C#的翻译应该是直截了当的。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检测文本文件 c# 中最后两次出现的字符/数据集 - How would I detect the last two occurrence of characters/data sets in a text file c# 替换字符串中最后一次出现的单词 - C# - Replace the last occurrence of a word in a string - C# 以字符串开头的正则表达式(在C#中),但是匹配最后一次出现之后的所有内容…… - Regular Expression (in C#) that starts with a string, but matches everything that is after the last occurrence … 正则表达式-从日志C#获取模式后的所有文本 - Regex - Get all text after pattern from log C# 正则表达式获取模式的最后一次出现 - Regex get last occurrence of the pattern 在C#中使用“关键字”获取最后一行打印后的所有行 - Get all lines after the last print of line with 'keyword' in C# 使用C#正则表达式分别提取文本金额的出现 - Extract the occurrence of textual amount separately using C# regex 如何使用RegEx在c#中的字符串中查找最后一次出现的&#39;,&#39; - how to find the last occurrence of ',' in a string in c# using RegEx 在 C# 中第二次出现逗号时拆分字符串 - Split a String on 2nd last occurrence of comma in C# 根据模式 C# 提取字符串的一部分 - Extract a part of the string based on a pattern C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM