【问题标题】:how to know that string contain some characters?如何知道该字符串包含一些字符?
【发布时间】:2013-02-01 10:14:36
【问题描述】:

你好,我想用这个字符串做,

D:\Bank Pelapor\BNI\Repository\201209\Instance\123456789-2012-09-30-BSMS1-1.xbrl

路径与带有版本控制的文件一致,文件扩展名前为-1,我想做点什么,

现在我已经使用此代码,我拆分路径:

string[] filefile = dr["Path_XBRL"].ToString().Split('\\', '-');


   if( filefile.length >11)
    {

      do.something();

    }

    else
    {
      do.somethingelse();
    }

但我担心有一天路径会改变或修改,有什么解决方案吗?

【问题讨论】:

  • 这就是路径上文件的版本控制,我在每个新上传的同名文件中使用 - 1,所以当那个文件带有 -1 我想做一些事情时,我很困惑如何检查 -1:p

标签: c# string path split


【解决方案1】:

我假设path 将具有以下格式:"{DirectoryName}\{FileName}-{VersionNumber}.{Extension}"

在不带扩展名的路径中获取文件名:Path.GetFileNameWithoutExtension(myPath) in System.IO

string fileName = Path.GetFileNameWithoutExtension(path);  // Gets the file name without extension
string versionString = fileName.Split('-').LastOrDefault();  // Gets the string after the last "-"
int version;
if(int.TryParse(versionString, out version))  // If "versionString" can be converted into an integer
    // TODO : If there is a version number (stored in "version")
else
    // TODO : If there is no version number

【讨论】:

  • 我已经使用 Path.GetFileNameWithoutExtension(myPath) 但是,每条记录的文件名和版本都不同..
  • @SabilValdano 在 System.Linq 命名空间中。
猜你喜欢
  • 1970-01-01
  • 2014-10-08
  • 2011-02-15
  • 2019-07-31
  • 2015-05-31
  • 1970-01-01
  • 1970-01-01
  • 2011-03-31
相关资源
最近更新 更多