Anders Lind
Anders Lind

Reputation: 4840

C# string operation. get file name substring

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?

Upvotes: 3

Views: 2079

Answers (6)

Aaron Anodide
Aaron Anodide

Reputation: 17196

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);
    }
}

Upvotes: 0

Artak
Artak

Reputation: 2907

Here is the method which will handle that:

public int GetFileIndex(string argFilename) { return Int32.Parse(argFilename.Substring(argFilename.LastIndexOf("_")+1, argFilename.LastIndexOf("."))); }

Enjoy

Upvotes: 1

Jo&#227;o Angelo
Jo&#227;o Angelo

Reputation: 57728

You have some options:

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.

Upvotes: 5

Jacob
Jacob

Reputation: 78920

This looks like a job for regular expressions. First define the pattern as a regular expression:

.*?_(?<index>\d+)\.tif

Then get a match against your string. The group named index will contain the digits:

var idx = Regex.Match(filename, @".*?_(?<index>\d+)\.tif").Groups["index"].Value;

Upvotes: 2

Iesvs
Iesvs

Reputation: 125

You can use the regex "(?<digits>\d+)\.[^\.]+$", and if it's a match the string you're looking for is in the group named "digits"

Upvotes: 1

fardjad
fardjad

Reputation: 20424

String.Split('_')[2].Split('.')[0]

Upvotes: 0

Related Questions