开发者

c# easiest way to extract substring

i have a string:

string somestring = "\\\\Tecan1\\tecan #1 开发者_StackOverflow中文版output\\15939-E.ESY"

i need to extract 15939

it will always be a 5 digit number, always preceeded by '\' and it will always "-" after it


String result = Path.GetFileName("\\\\Tecan1\\tecan #1 output\\15939-E.ESY").Split('-')[0];

Perhaps?


This regex does the trick for the input string you provided:

        var input = "\\\\Tecan1\\tecan #1 output\\15939-E.ESY";

        var pattern = @".*\\(\d{5})-";

        var result = Regex.Match(input, pattern).Groups[1].Value;

But I actually like Brad's solution using Path.GetFileName more :-)


Try (based on your answer in the comments about the \ character):

string result = myString.SubString(myString.LastIndexOf(@"\") + 1, 5);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜