重命名非空间文件名的代码是什么

时间:2015-11-13 10:34:23

标签: c# wpf mvvm

我有文件的文件名erwin_01problem.doc,我想要的是如果文件名不包含 01problem 之间的空格(erwin_0 1p roblem.doc) 。我找到问题的索引并用“”替换它。输出将是 erwin_01 problem.doc 这是我尝试的代码,但我仍然无法在01和问题之间放置一个空格。

if (!string.IsNullOrEmpty(job.ProblemPath))
            {
                job.HasProblemFile = true;
                var problemDocFname = Path.GetFileName(job.ProblemPath);
                if (!Regex.IsMatch(problemDocFname, @"\sproblem\.doc$"))
                {
                    ProgM.JobStatus = "Checking space between filename and problem...";
                    Thread.Sleep(1000);
                    problemDocFname = problemDocFname.Insert(problemDocFname.IndexOf("problem.doc", StringComparison.Ordinal), " ");
                    //problemDocFname = problemDocFname.Replace("problem", " problem");
                }
                problemDocFname = Path.Combine(job.FilePath, problemDocFname);
                var docProblemCount = 0;
                ProgM.JobStatus = "Correcting the Format of Problem Doc...";
                Thread.Sleep(1000);
                MicrosoftWord.CorrectProblemDocFormatting(problemDocFname, ref docProblemCount);
            }
            jobs.Add(job);

1 个答案:

答案 0 :(得分:0)

我相信你真的不需要正则表达式。你可以这样做:

string key = "problem.doc";
if (problemDocFname.EndsWith(key) && problemDocFname.Length > key.Length)
{
      problemDocFname.Replace(key, " problem.doc");
}