3
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?

2
  • Describe your parameters. Does the number always occur at the end of the filename, just before the file extension, after an underscore character? Commented Nov 4, 2011 at 17:38
  • @micheal, yes, number always appear before extention, and after slice_, but maybe 1 digit or 2 or 3 digtis. Commented Nov 4, 2011 at 17:41

6 Answers 6

5

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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;

Comments

1

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"

Comments

1

Here is the method which will handle that:

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

Enjoy

Comments

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.