简体   繁体   English

如何检查字符串是否包含以第一个字母开头的动态单词

[英]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#).我试图在字符串中找出发生变化但以字母“F”(C#)开头的单词。 The result output from service call is as below:服务调用的结果 output 如下:

Exception_Remote_Call--VNQ DN ERROR CODE found ERROR CODE= F0123, ERROR DESCRIPTION= NOT AVAILABLE Exception_Remote_Call--找到 VNQ DN 错误代码 错误代码 = F0123,错误描述 = 不可用

In the above string, F0123 word changes according to the different ERROR CODE.上述字符串中, F0123字根据 ERROR CODE 的不同而变化。 I tried as below but it works for F0123 and does not work if the output is F0111.我尝试如下,但它适用于 F0123,如果 output 是 F0111,则不起作用。 I would like to find if it starts with 'F'.我想知道它是否以'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:为了让未来的维护者更清楚、更容易发现,我可能会将ERROR CODE =作为表达式的一部分:

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 https://dotnetfiddle.net/nOXOCt

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Regex 检查每个单词是否以大写字母开头 - Regex Check that each word starts with a capital letter 如何在 LINQ 查询中检查字符串是否以大写字母开头 - How to check if a string starts with a capital letter in a LINQ query 如何检查字符串的第一个字符,如果是字母,C# 中的任何字母 - How to check first character of a string if a letter, any letter in C# 检查字符串是否包含单词? - Check if a string contains a word? 如何检查字符串是否包含单词的所有字符 - How to check if a string contains all of the characters of a word 我如何检查一个字符串包含一个字母空格字母空格和另一个字母 C# - How can i check of a string contains a letter space letter space and anotherl letter C# 除了任何单词的第一个字母,我如何将字符串中的两个字母的单词都大写? - How do I capitalized any two letter word in a string in addition to the first letter of any word? 除了任何单词的第一个字母外,如何在字符串中大写特定的两个字母的单词? - How do I capitalized specific two letter word in a string in addition to the first letter of any word? 如何替换字符串中每个单词的第一个字母? - How do i replace first letter in each word in a string? 检查一个字符串是否包含一个Word,然后检查它是否从所述Word开始,如果它不是? - Checking If a String Contains a Word and Then Checking If It Starts With said Word and If It Doesnt?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM