Share via

How to extract a string from text ?

Chocolade 536 Reputation points
2023-01-06T21:31:00.967+00:00
int index2 = downloadLinks[i].IndexOf("S_");  
string result = downloadLinks[i].Substring(index2 + 2);  
  

Example of a string in downloadLinks :

https://ims.gov.il/sites/default/files/ims_data/map_images/IMSRadar4GIS/IMSRadar4GIS_202301062325_0.png

and from each link i want to get only the number : 202301062325

so far in the variable result i can get : 202301062325_0.png but i don't want the last part _0.png and not sure how to remove that in the substring line.

Developer technologies | Windows Forms
Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

0 comments No comments

Answer accepted by question author

Viorel 127K Reputation points
2023-01-06T21:40:43.13+00:00

Check an example:

string link = "https://ims.gov.il/sites/default/files/ims_data/map_images/IMSRadar4GIS/IMSRadar4GIS_202301062325_0.png";  
string result = Regex.Match( Path.GetFileNameWithoutExtension( link ), @"_(\d+)_\d+$" ).Groups[1].Value;  
// result is "202301062325"  

Was this answer helpful?

2 people found this answer helpful.
0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.