在最后一次出现模式后提取所有字符 C#
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1153630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Extract All Characters after last occurrence of a pattern C#
提问by
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
魔法
采纳答案by Guffa
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();
回答by ba__friend
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();
}
回答by bufferz
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);
回答by Fredrik M?rk
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);
回答by Larry Watanabe
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.
我现在正在使用 VB,但转换为 C# 应该很简单。从这里实现您的实际问题应该很简单 - 您有一个集合,最后一项是最后一个数字,只需增加它并替换集合的最后一项并再次写出。
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
回答by Robert Venables
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);
回答by SCL
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.
我会让其他帖子回答您的其余问题。