String str = "USD SBFARE 4067.71 OVDPCT 8P SBMARKUP 0A TPS 59486 CC 0P OTH 0A"
I need "8" (OVDPCT 8P) from this string.
Number always follow OVDPCT and precede with a P.
This 8 may be 10,12 etc means any number.
How can i by using c#.?
How many different variants are we talking about?
If it's always OVDPCT *P then the pattern can be:
.*OVDPCT (\d+)P.*
You can use it like this:
Match match = Regex.Match(str,@".*OVDPCT (\d+)P.*");
int num = int.Parse(match.Groups[1].Value);
Note: I'm being very rough here, you'd probably want to check match.Success and also use int.TryParse.
So you can use a regular expression to do that
var match = System.Text.RegularExpressions.Regex.Match(str, "OVDPCT (?<Number>\\d+)P", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
if(match.Success)
{
var number = match.Groups["Number"].Value;
}
But this line seems like an data base record isn't it?
Excuse the scrappy code but something like this:
var str = "USD SBFARE 4067.71 OVDPCT 8P SBMARKUP 0A TPS 59486 CC 0P OTH 0A";
var strAsArray = str.Split(" ".ToArray(), StringSplitOptions.RemoveEmptyEntries);
var subject = strAsArray[4].Trim(); // assuming fifth element
var value = new Regex("^[0-9]+").Match(subject).Value;
Console.WriteLine("Found: ", value);
OVDPCTand precede aP? Or is it always the 5th string? Also, what solutions have you tried so far?