【问题标题】:How to extract Guid from file name?如何从文件名中提取 Guid?
【发布时间】:2016-01-26 09:31:13
【问题描述】:

我想从下面的字符串数组中提取 Guid 并希望输出如下:

Output:Log.txt,Logging.txt

string[] str = new string[] { "1250a2d5-cd40-4bcc-a979-9d6f2cd62b9fLog.txt", "bdb31966-e3c4-4344-b02c-305c0eb0fa0aLogging.txt" }; //file name are Log.txt and Logging.txt
List<string> list= new List<string>();
foreach (var item in str)
{
    if (!string.IsNullOrEmpty(item))
    {
        list.Add();  // here i want to store Log.txt and Logging.txt
    }
}

如何做到这一点??

【问题讨论】:

  • list.Add("Log.txt") :)

标签: c# regex guid


【解决方案1】:
var str = new string[] { "1250a2d5-cd40-4bcc-a979-9d6f2cd62b9fLog.txt", "bdb31966-e3c4-4344-b02c-305c0eb0fa0aLogging.txt" }; //file name are Log.txt and Logging.txt

var arr = str.Select(s=>s.Substring(36)).Where(s=>s.Any()).ToArray();

你可以在这里看到它的实际效果:https://dotnetfiddle.net/eHy6Fo

【讨论】:

  • 那么你确定 guid 只有 36 位数字吗??
  • GUID 是 128 位,或 32 个十六进制字符。在那个特定的表示中,有 4 个额外的破折号——等于 36!
  • 感谢您的信息和解决方案。我可以知道投反对票的原因吗?
  • 在你的帖子上?可能是因为表述不够清晰和专业。
  • 好的。这个怎么样:stackoverflow.com/questions/1700361/…这个问题说得很专业?
猜你喜欢
  • 2016-08-28
  • 1970-01-01
  • 1970-01-01
  • 2011-04-28
  • 1970-01-01
  • 1970-01-01
  • 2013-11-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多