1

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.

Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
neetha mathew
  • 39
  • 1
  • 2
  • 11

2 Answers2

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