簡體   English   中英

在標簽上顯示字符串的某些部分

[英]display some part of string on a label

輸入1

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

輸入2

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

我有兩種不同類型的字符串,如果用戶輸入字符串str,則輸出應為(WL 2)&如果用戶輸入字符串str1,則輸出應為(CNF S5 49(LB))

除(WL(數字))(CNF(1個字母1或2個數字)數字(LB))以外的所有值都是動態的

如果使用一些定界符對輸入字符串進行框架化,則可以輕松拆分字符串並將其存儲在某個數組中並繼續。

例如,將您的字符串框架為

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

在此之后分割字符串像

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

從str1數組中,您可以獲取最后一個值str1 [5]

您可以使用正則表達式: 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