简体   繁体   中英

display some part of string on a label

Input 1

string str=" 1 KAUSHAL DUTTA 46 Female WL 19 WL 2";

Input 2

string 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))

all the values are dynamic except(WL (number)) (CNF (1 alphabet 1 or 2 number) number (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";

After this split the string like

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

From str1 array, you can take last value str1[5]

You can use Regex: https://msdn.microsoft.com/en-us/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;
}

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