I want to trim a string and get the number between special characters.
For example there is a string BC/PO/88/2018
from it i want to get 88.
Asked
Active
Viewed 118 times
1

Pranay Rana
- 175,020
- 35
- 237
- 263

neetha mathew
- 39
- 1
- 2
- 11
-
3Possible duplicate of [C# Splitting Strings?](https://stackoverflow.com/questions/7559121/c-sharp-splitting-strings) – Sebastian Hofmann Mar 14 '18 at 06:27
-
show what you have done so far for this, also go through how to create [mcve]. – Paritosh Mar 14 '18 at 06:27
-
try `"BC/PO/88/2018".Split('/')[2]` if the string format is same – programtreasures Mar 14 '18 at 06:28
-
1_I want a packet of Tim Tams that never runs out_. Sadly it is yet to happen – Mar 14 '18 at 06:29
-
This question has been answered here before: https://stackoverflow.com/questions/11575683/copy-part-of-a-string-to-another-string-in-c-sharp – NickitaeWSamuels Mar 14 '18 at 06:30
-
Possible duplicate of [Find and extract a number from a string](https://stackoverflow.com/questions/4734116/find-and-extract-a-number-from-a-string) – Michael Henderson Mar 15 '18 at 17:05
2 Answers
0
you can make use of regular expression and extract number
Match match = Regex.Match("BC/PO/88/2018 f" , @"(\d+)");
if (match.Success) {
return int.Parse(match.Groups[0].Value);
}
other way is you can do with the help of String.Split
as suggested in comments if you are sure about string coming as input i.e. sure about format of string.

Pranay Rana
- 175,020
- 35
- 237
- 263
-
why there is - 1? its already accepted ans working solution...seems like some one doing with purpose ...hope SO people note that – Pranay Rana Mar 15 '18 at 17:02
0
You could use Regular Expressions:
string strRegex = @"[A-Z]{2}/[A-Z]{2}/(?<MyNumber>[0-9]*)/[0-9]{4}";
Regex myRegex = new Regex(strRegex, RegexOptions.None);
string strTargetString = @"BC/PO/88/2018";
foreach (Match myMatch in myRegex.Matches(strTargetString))
{
if (myMatch.Success)
{
// Add your code here
}
}

richej
- 834
- 4
- 21