简体   繁体   中英

How to check if the string contains dynamic word that starts with first letter

I am trying to find out in the string if the word that changes but starts with letter 'F' (C#). The result output from service call is as below:

Exception_Remote_Call--VNQ DN ERROR CODE found ERROR CODE= F0123, ERROR DESCRIPTION= NOT AVAILABLE

In the above string, F0123 word changes according to the different ERROR CODE. I tried as below but it works for F0123 and does not work if the output is F0111. I would like to find if it starts with 'F'.

 var isStartsWithF = s.Contains("F0123");

I would really appreciate for the help. Thank you in advance!

This is a job for regular expressions. To make things clearer and easier to spot for future maintainers, I might include the ERROR CODE = as part of the expression:

var data = "Exception_Remote_Call--VNQ DN ERROR CODE found ERROR CODE = F0123, ERROR DESCRIPTION= NOT AVAILABLE";   

var exp = new Regex(@"ERROR CODE\s?= (F\d{4,5})");  
var result = exp.Match(data).Groups[1].Value;

See it work here:

https://dotnetfiddle.net/nOXOCt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM