Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用多个delimeter从字符串中提取单词_C#_Linq_Substring - Fatal编程技术网

C# 使用多个delimeter从字符串中提取单词

C# 使用多个delimeter从字符串中提取单词,c#,linq,substring,C#,Linq,Substring,这是我的网址: 现在我想在我的查询中从我的url获取twitter var userDetails = context .UserDetails .Where(x => x.Email == _userDetails.Email && x.Password == _userDetails.Password) .Select(x => new UserDetailsModel {

这是我的网址:

现在我想在我的查询中从我的url获取twitter

  var userDetails = context
      .UserDetails
      .Where(x => x.Email == _userDetails.Email && x.Password == _userDetails.Password)
      .Select(x => 
          new UserDetailsModel
          {
              Filename= x.Videourl
          });
在我的文件名中,我只需要文件名,而不是查询中的整个url

如何做到这一点???

您可以尝试:

string url = "https://videodb.blob.core.windows.net/video/0c7f5ecf-22fc-4032-8ca1-17481f18aaa8_twitter.jpg";
string fileName = url.Substring(url.LastIndexOf('_') + 1);
这将返回您
twitter.jpg

如果需要不带扩展名的文件名:

string fileNameWithoutExtention = System.IO.Path.GetFileNameWithoutExtension(fileName);

它会给你推特
twitter

除了Arshad所做的,你也可以这样做

string url = "https://videodb.blob.core.windows.net/video/0c7f5ecf-22fc-4032-8ca1-17481f18aaa8_twitter.jpg";
string[] delimitedString = url.Split('_');
string fileName = delimitedString[delimitedString.Length - 1].Split('.')[0];

什么是上下文?什么是用户详细信息?你到底想要做什么?你想要
0c7f5ecf-22fc-4032-8ca1-17481f18aaa8_twitter.jpg
?@Arshad:我只想要twitter你可以使用
System.IO
名称空间:
System.IO.Path.GetFileName(x.Videourl)
,但如果它来自数据库,则将结果加载到内存中first@EhsanSajjad:是,它来自数据库为什么不
var fileName=“twitter.jpg”
那么?!如果OP想要twitter,给他们twitter:)但是如果名字是这样的怎么办:0c7f5ecf-22fc-4032-8ca1-17481_f18aaa8_twitter.jpg
var
将是字符串。@Mihai:但整个url都来了,而不仅仅是名字来了这也会起作用,因为我正在使用twitter的最后一个索引_