简体   繁体   English

在标签上显示字符串的某些部分

[英]display some part of string on a label

Input 1 输入1

string str=" 1 KAUSHAL DUTTA 46 Female WL 19 WL 2"; 字符串str =“ 1 KAUSHAL DUTTA 46女WL 19 WL 2”;

Input 2 输入2

string str1= "1 AYAN PAL 38 Male CNF S5 49 (LB) CNF S5 49 (LB)"; 字符串str1 =“ 1 AYAN PAL 38 Male CNF S5 49(LB)CNF S5 49(LB)”;

i have two different types of string if user enter string str then the output should be (WL 2) & if user enter string str1 then the output should be(CNF S5 49 (LB)) 我有两种不同类型的字符串,如果用户输入字符串str,则输出应为(WL 2)&如果用户输入字符串str1,则输出应为(CNF S5 49(LB))

all the values are dynamic except(WL (number)) (CNF (1 alphabet 1 or 2 number) number (LB)) 除(WL(数字))(CNF(1个字母1或2个数字)数字(LB))以外的所有值都是动态的

If you frame your input string with some delimiter, then you can easily split the string and you can store it in some array and proceed. 如果使用一些定界符对输入字符串进行框架化,则可以轻松拆分字符串并将其存储在某个数组中并继续。

For example, Frame your string as 例如,将您的字符串框架为

string str="1@KAUSHAL DUTTA@46@Female@WL 19@WL 2"; 字符串str =“ 1 @ KAUSHAL DUTTA @ 46 @ Female @ WL 19 @ WL 2”;

After this split the string like 在此之后分割字符串像

string[] str1 = str.Split('@'); 字符串[] str1 = str.Split('@');

From str1 array, you can take last value str1[5] 从str1数组中,您可以获取最后一个值str1 [5]

You can use Regex: https://msdn.microsoft.com/en-us/library/az24scfc(v=vs.110).aspx 您可以使用正则表达式: https ://msdn.microsoft.com/zh-cn/library/az24scfc( v= vs.110).aspx

//This is the pattern for the first case WL followed by a one or more (+) digits (\d) 
//followed by any number of characters (.*) 
//the parenthesis is to us to be able to group what is inside, for further processing
string pattern1 = @"WL \d+ (.*)";

//Pattern for the second match: CNF followed by a letter (\w) followed by one or two ({1,2}) 
//digits (\d) followed by one or more (+) digits (\d), followed by (LB) "\(LB\)" 
//the backslach is to get the litteral parenthesis
//followed by any number of characters (.*)
//the parenthesis is to us to be able to group what is inside, for further processing
string pattern2 = @"CNF \w\d{1,2} \d+ \(LB\) (.*)";

string result="";

if (Regex.IsMatch(inputString, pattern1))
{
    //Groups[0] is the entire match, Groups[1] is the content of the first parenthesis
    result = Regex.Match(inputString, pattern1).Groups[1].Value;
}
else if (Regex.IsMatch(inputString, pattern2))
{
    //Groups[0] is the entire match, Groups[1] is the content of the first parenthesis
    result = Regex.Match(inputString, pattern2).Groups[1].Value;
}

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

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