-2

I know this question has been asked but I did not find a solution that worked form me, I am new to C#.

I have images names as follows:

propimage22997_1.jpg or /propimage229927_1.jpg etc..

I need the number 22997 so start at char 9 and end at _.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
user3458266
  • 235
  • 1
  • 5
  • 12

4 Answers4

3

Since that's a filename use the Path class. Then you can use LINQ:

string fileName = "propimage22997_1.jpg";
string fileNameOnly = Path.GetFileNameWithoutExtension(fileName);
string token = fileNameOnly.Remove(fileNameOnly.LastIndexOf('_'));
string number =  new string(token.SkipWhile(Char.IsLetter).ToArray()); //22997

or by skipping characters that are not digits:

number = new string(token.SkipWhile(c=> !Char.IsDigit(c)).ToArray());

If you need it as int instead of string use int.TryParse(if the format can be invalid) or int.Parse:

int num = int.Parse(number);
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • will this remove `propimage` and `_`.jpg? and only keep 22997 or whatever the number is.? I am asking because there can be 22997_1,22997_2 ect. But i just need 22997 – user3458266 Apr 29 '14 at 12:13
  • @user3458266: you just need to copy paste the code and test it ;-) It will remove everything from the filename that comes after the last `_` (including `_`). Then it skips everything that is not a digit(0-9). So the result is `"22997"`. – Tim Schmelter Apr 29 '14 at 12:14
  • sorry i just dont want to copy and paste code, I understand what you are saying but i want to know what I am doing and learn. – user3458266 Apr 29 '14 at 12:18
  • @user3458266: note that i've added `int.Parse` which converts the string to an `int`. You need to add `using System.Linq` to get the LINQ part working and `using System.IO` to use `Path`. – Tim Schmelter Apr 29 '14 at 12:21
  • You can skip the first two steps: `fileName.SkipWhile(char.IsLetter).TakeWhile(char.IsNumber).ToArray()` – Ferruccio Apr 29 '14 at 12:46
  • @Ferruccio: better `Char.IsDigit` than `IsNumber`. However, yes, you could also use that. But the string methods have two advantages: 1. they are more efficient 2. OP learns more about .NET ;-) – Tim Schmelter Apr 29 '14 at 12:50
  • @TimSchmelter - Thanks for the tip on `char.IsDigit`. It actually does what I thought `char.IsNumber` did. I'm not sure I'd agree with you on the efficiency of the string operations, but your method also has two disadvantages: 1. two extra memory allocations 2. code is not as clear. – Ferruccio Apr 29 '14 at 13:02
  • @Ferruccio: `String.Substring`/`Remove`(internally also `Substring`) are by far the most efficient approach to extract substrings which is the core of this question. Additional variables don't cost much since they are local variables but increase readability and maintainibility. It's also easier to test/debug. Since OP has mentioned that he's new in C# it's even more important to prevent one-liners. OP has learnt the `Path` class, important string methods like `Remove`/`Substring`/`LastIndexOf` and LINQ in a few lines ;-) – Tim Schmelter Apr 29 '14 at 13:32
0

You can do this:

string test = "propimage22997_1.jpg";
string number = Regex.Match(test,"\d+").Value;
Amit Joki
  • 58,320
  • 7
  • 77
  • 95
0

I'm not in front of a PC with visual studio right now, but something like this should work:

var numbers = Regex.Split(yourImageName, @"^\d");

This should return an array of strings containing the numbers found inside it. You can then check f the array (numbers) is empty and if not, use it.

Nahuel Ianni
  • 3,177
  • 4
  • 23
  • 30
0
    private static int GetNumber(string sz)
    {
        string szFileName = sz; //"propimage22997_1.jpg";
        int index = 0;
        int Number = 0;
        index = szFileName.IndexOf('_');

        Number = Convert.ToInt32(szFileName.Substring(9, (szFileName.Length - index - 1)));
         return Number;

    }
}
SMA
  • 74
  • 2