myfinename_slice_1.tif
myfilename_slice_2.tif
...
...
myfilename_slice_15.tif
...
...
myfilename_slice_210.tif
In C#, how can I get file index, like "1", "2", "15", "210" using string operations?
myfinename_slice_1.tif
myfilename_slice_2.tif
...
...
myfilename_slice_15.tif
...
...
myfilename_slice_210.tif
In C#, how can I get file index, like "1", "2", "15", "210" using string operations?
You have some options:
Regex class;String.Split.Most important is what are the assumptions you can make about the format of the file name.
For example if it's always at the end of the file name, without counting the extension, and after an underscore you can do:
var id = Path.GetFileNameWithoutExtension("myfinename_slice_1.tif")
.Split('_')
.Last();
Console.WriteLine(id);
If for example you can assume that the identifier is guaranteed to appear in the filename and the characters [0-9] are only allowed to appear in the filename as part of the identifier, you can just do:
var id = Regex.Match("myfinename_slice_1.tif", @"\d+").Value;
Console.WriteLine(id);
There are probably more ways to do this, but the most important thing is to assert which assumptions you can make and then code a implementation based on them.
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
var s1 = "myfinename_slice_1.tif";
var s2 = "myfilename_slice_2.tif";
var s3 = "myfilename_slice_15.tif";
var s4 = "myfilename_slice_210.tif";
var s5 = "myfilena44me_slice_210.tif";
var s6 = "7myfilena44me_slice_210.tif";
var s7 = "tif999";
Assert.AreEqual(1, EnumerateNumbers(s1).First());
Assert.AreEqual(2, EnumerateNumbers(s2).First());
Assert.AreEqual(15, EnumerateNumbers(s3).First());
Assert.AreEqual(210, EnumerateNumbers(s4).First());
Assert.AreEqual(210, EnumerateNumbers(s5).Skip(1).First());
Assert.AreEqual(210, EnumerateNumbers(s6).Skip(2).First());
Assert.AreEqual(44, EnumerateNumbers(s6).Skip(1).First());
Assert.AreEqual(999, EnumerateNumbers(s7).First());
}
static IEnumerable<int> EnumerateNumbers(string input)
{
var digits = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
string result = string.Empty;
foreach (var c in input.ToCharArray())
{
if (!digits.Contains(c))
{
if (!string.IsNullOrEmpty(result))
{
yield return int.Parse(result);
result = string.Empty;
}
}
else
{
result += c;
}
}
if (result.Length > 0)
yield return int.Parse(result);
}
}