如何在dot(。)之前和dateformat之后获取字符串? [英] How to get string before dot(.) and after dateformat?
本文介绍了如何在dot(。)之前和dateformat之后获取字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
20170505000F.tif
20170505000R.tif
20170505000U.jpg
从这段代码中,我需要获得R或U。我怎么得到?
我尝试过:
string date = DateTime.Now.ToString(yyyyMMdd)
string fileName = date +000;
string newFileName = fileName + Path.GetExtension(fileinfo.Name)
20170505000F.tif
20170505000R.tif
20170505000U.jpg
From this code, I need to get "R" or "U". How do I get?
What I have tried:
string date = DateTime.Now.ToString("yyyyMMdd")
string fileName = date + "000";
string newFileName = fileName + Path.GetExtension(fileinfo.Name)
推荐答案
string s = "20170505000F.tif";
var value = s.Substring(11,1); // F
或
or
var value1 = s.First(k => char.IsLetter(k)).ToString(); // F
注:照顾验证
这里有两个更多的方式。
1。在使用文件名时使用System.IO命名空间:
Here are two more ways of doing it.
1. Using the System.IO namespace as you are working with filenames:
private static string GetCode(string filename)
{
return System.IO.Path.GetFileNameWithoutExtension(filename).Last().ToString();
}
2。字符串处理:
2. String Handling:
private static string GetCode(string text)
{
return text.Split(new char[] { '.' })[0].Last().ToString();
}
使用:
To use:
var filename = "20170505000U.jpg";
var code = GetCode(filename);
这篇关于如何在dot(。)之前和dateformat之后获取字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文