简体   繁体   English

删除字符串C#(FileName)的一部分

[英]Remove A Part Of A String C# (FileName)

The Question: 问题:

I have files with time stamp added to their names, like this : 我将带有时间戳的文件添加到它们的名称中,如下所示:

"presentation final project_20141207182834657.pptx" “演示文稿最终项目_20141207182834657.pptx”

And

"presentation final project_201412071864035.pptx" “演示最终项目_201412071864035.pptx”

How can I remove the "_date" part from it ? 如何从中删除“ _date”部分?

Notice that i cant count char because sometimes the date is 15 char' and sometimes more, I need somehow to remove a part of the string that start with "_20" until it reach the "." 请注意,我无法计数char,因为有时日期是15 char',有时还会更多,我需要以某种方式删除以“ _20”开头的字符串的一部分,直到到达“”。 (dot) or maybe someone got a better idea. (点),或者有人有更好的主意。

Background: 背景:

I have a storage of files, before I upload a file I give it a time stamp like this : 我有文件的存储空间,在上传文件之前,我先给它加一个时间戳:

(JavaScript) (JavaScript)

function getDate() {
var today = new Date();
var year = today.getFullYear().toString();
var month = (today.getMonth()+1).toString();
var day = today.getDate();

if (day < 10)
    day = "0" + day.toString();
else
    day = day.toString();

var hours = today.getHours().toString();
var minutes = today.getMinutes().toString();
var seconds = today.getSeconds().toString();
var miliSecond = today.getMilliseconds().toString();


return year+month+day+hours+minutes+seconds+miliSecond;
}

And what is return from this function I added to the file name. 从该函数返回的结果是我添加到文件名中的。

Now I want to return the file to the user but without the date stamp i added to the file.. 现在,我想将文件返回给用户,但没有添加到文件的日期戳。

Thank you. 谢谢。

Update 更新资料

Let me give a wild scenario, there is underscore "_" not only before the date like this : 让我给出一个疯狂的场景,不仅在这样的日期之前还有下划线“ _”:

"presentation_final_project_201412071864035.pptx" “ presentation_final_project_201412071864035.pptx”

I cant trust the index of "_" because its can be everywhere. 我不能相信索引“ _”,因为它可以无处不在。

To extract the file parts I would use the Path class method to be more OS independent 要提取文件部分,我将使用Path类方法来提高操作系统的独立性

string input = "presentation final project_20141207182834657.pptx";
string pureFile = Path.GetFileNameWithoutExtension(input);
string ext = Path.GetExtension(input);
int pos = pureFile.LastIndexOf('_');
string newFile = pureFile.Substring(0, pos) +  ext;

Notice, to find the underscore you need to start from the end of the input string, not from the start to avoid false positives (in an underscore is present before the last one) 请注意,要查找下划线,您需要从输入字符串的末尾开始,而不是从头开始,以免出现误报(下划线位于最后一个字符串的前面)

The only point left to uncertainty is the presence of the underscore, before or after the date part. 唯一不确定的地方是在日期部分之前或之后是否存在下划线。 If you can be sure that there is always an underscore just before the date and no underscore after the date this approach would work flawlessly. 如果您可以确定在日期之前总是有一个下划线,而在日期之后没有下划线,则此方法将完美地起作用。

It's fairly trivial. 这是相当琐碎的。 Find the positions of the characters and build a new string using Substring() : 查找字符的位置并使用Substring()构建新的字符串:

string input = "presentation final project_20141207182834657.pptx";

int underscoreIndex = input.LastIndexOf("_");
int dotIndex = input.LastIndexOf(".");

string newFilename = input.Substring(0, underscoreIndex);
newFilename += input.Substring(dotIndex);

See it in action on Ideone.com . Ideone.com上查看它的运行情况

Use string.LastIndexOf and string.Remove to get the text out: 使用string.LastIndexOfstring.Remove将文本取出:

string text = "presentation final project_20141207182834657.pptx";

int indexOfUnderscore = text.LastIndexOf('_'); // find the position of _
int indexOfPeriod = text.LastIndexOf('.', indexOfUnderscore); // find the position of .

// find remove the text between _ and .
string newText = text.Remove(indexOfUnderscore, indexOfPeriod - indexOfUnderscore);

string r = "presentation final project_2011207182834657.pptx"; 字符串r =“ presentation final project_2011207182834657.pptx”;

        string part1 = r.Substring(0, r.IndexOf(@"_"));

        string part2 = r.Substring(r.IndexOf(@"."), 5);

        Console.WriteLine(part1);

        Console.WriteLine(part2);

        Console.WriteLine(part1 + part2);

here is a regex solution 这是一个正则表达式解决方案

string file = "presentation final project_201412071864035.pptx";
var result = Regex.Replace(file, @"(.*)(_[\d]{15,})(.*)$", "$1$3");
string oldpath = "presentation_final_project_201412071864035.pptx";
string date = oldpath.Split('_').LastOrDefault().Split('.').FirstOrDefault();
string newpath = oldpath.Replace("_" + date, "");

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

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