简体   繁体   English

在 C# 中拆分字符串值

[英]Split string value in C#

I have a string value which I need to get the middle bit out of, eg "Cancel Payer" / "New Paperless".我有一个字符串值,我需要从中取出中间位,例如“取消付款人”/“新无纸化”。

These are examples of the string format:这些是字符串格式的示例:

"REF_SPHCPHJ0000057_Cancel Payer_20100105174151.pdf" "REF_SPHCPHJ0000057_取消付款人_20100105174151.pdf"
"REF_SPHCPHJ0000056_New Paperless_20100105174151.pdf" 《REF_SPHCPHJ0000056_新无纸化_20100105174151.pdf》

Use:使用:

string s = "REF_SPHCPHJ0000057_Cancel Payer_20100105174151.pdf";
string middleBit = s.Split('_')[2];
Console.WriteLine(middleBit);

The output is输出是

Cancel Payer

This is a place for regular expressions:这是正则表达式的地方:

Regex re = new Regex(@".*_(?<middle>\w+ \w+)_.*?");
string name = "REF_SPHCPHJ0000057_Cancel Payer_20100105174151.pdf";
string middle = re.Match(name).Groups["middle"].Value;

I think that the regular expression我认为正则表达式

Regex re = new Regex(@"\w+_\w+_(?<searched>.*)_\d*.pdf");

will meet your needs, if the PDF files always come to you as:将满足您的需求,如果 PDF 文件总是以以下方式出现在您面前:

REF_<text>_<your text here>_<some date + id maybe>.pdf

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

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