简体   繁体   中英

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"

And

"presentation final project_201412071864035.pptx"

How can I remove the "_date" part from it ?

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 "." (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)

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"

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

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() :

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 .

Use string.LastIndexOf and string.Remove to get the text out:

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";

        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, "");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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