How to extract the number from the string, e.g. get the number 123 from string "c:\folderA\abc123.txt".  
Author Message
Rush hour





PostPosted: Visual C# General, How to extract the number from the string, e.g. get the number 123 from string "c:\folderA\abc123.txt". Top

Dear all,
I met one problem about extracting the number from the string.
Say, I had one string "c:\folderA\abc123.txt", with the most interesting the number 123.
However, how shall I get this number. By which means, I have to subtract the ***prefix*** string "c:\folderA\abc" and ***postfix*** string ".txt" from the original one. Is it easy for me to do it
Do you have any idea about it
Thanks in advance,
Cordially,
Luis



Visual C#7  
 
 
AndersBryhni





PostPosted: Visual C# General, How to extract the number from the string, e.g. get the number 123 from string "c:\folderA\abc123.txt". Top

Hi,

If you can expect there is only one sequence of digits in your text string, this code will do the trick.

string text = "c:\folderA\abc123.txt";

char[] digits = new char[] { '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' };

int startIndex = text.IndexOfAny(digits);

int endIndex = text.LastIndexOfAny(digits);

string number = text.Substring(startIndex, endIndex - startIndex + 1);

However, there may be loads of better ways of doing this.

Best Regards,

Anders



 
 
Baris ERGUN





PostPosted: Visual C# General, How to extract the number from the string, e.g. get the number 123 from string "c:\folderA\abc123.txt". Top

Hi Luis;

Check the below algorithm. In the example below you have a Fake FileName for trial and you first get file Name with out extension than get the number inside it and convert it to Integer. Dont forget to name the complete folder + file name in proper format ie you may fail with wrong number with the following format

c:\f3olderA\abc123.txt

Here is the code:

System.IO.FileInfo fileToRead = new

string fileNameWithOutSextension = fileToRead.FullName.Replace(fileToRead.Extension, "");

string numberInTheFileName =string.Join(null,System.Text.RegularExpressions.Regex.Split(fileNameWithOutSextension, "[^\\d]"));

int numberAsInteger = Int32.Parse(numberInTheFileName);

MessageBox.Show(numberAsInteger.ToString());

Bye;

Baris

www.thecoreopsis.com

 



 
 
Luis in SE





PostPosted: Visual C# General, How to extract the number from the string, e.g. get the number 123 from string "c:\folderA\abc123.txt". Top

Hey Baris,
Thanks for your code sneppit. It helps me a lot. :0)
BTW, for the Regex. I had some basic understandings after looking information from google. Can you point out my misleading if there are some...
Regex.Split(fileNameWithOutSextension, "[^\\d]")), which will split the file name without extension.
^ means the position in the beginning of the line/name/string. One "\" means devided name by slash "\", and "\d" means to get numeric number.
However, the information "\d" that I looked after is to get the number ranges from 0 to 9. But how does it get the number 123
I will mark the your answer as the answer to this question.
Thanks a lot,
Cordially,
Luis


 
 
Luis in SE





PostPosted: Visual C# General, How to extract the number from the string, e.g. get the number 123 from string "c:\folderA\abc123.txt". Top

Hey Anders,
Thanks for your answer. I got a lot from it!!!
Have a nice evening,
Cordially,
Luis

Hi,

If you can expect there is only one sequence of digits in your text string, this code will do the trick.

string text = "c:\folderA\abc123.txt";

char[] digits = new char[] { '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' };

int startIndex = text.IndexOfAny(digits);

int endIndex = text.LastIndexOfAny(digits);

string number = text.Substring(startIndex, endIndex - startIndex + 1);

However, there may be loads of better ways of doing this.

Best Regards,

Anders



 
 
Baris ERGUN





PostPosted: Visual C# General, How to extract the number from the string, e.g. get the number 123 from string "c:\folderA\abc123.txt". Top

Hey Luis,

Your welcome. By the way what that Regex expression "[^\\d]" does is that it takes all the numbers from the string and adds them side by side in the found order. eg if the string was er4345e2343 The Regex expression was going to take the number as 43452343.

Baris

www.thecoreopsis.com