开发者

Extracting a sub-string in C#

开发者 https://www.devze.com 2022-12-23 04:40 出处:网络
I am new to c# programming and I want to ask a question. How can I get the value in () and store it in another string. example:

I am new to c# programming and I want to ask a question.

How can I get the value in () and store it in another string. example:

I have string

s1="here there (hi)";

How can I get

开发者_如何学C
s2="hi";

the () will always be at the end of the sentence (never at first or in between).


string s1 = "abc (hi)";
string s2 = s1.Substring(s1.LastIndexOf("(") + 1, s1.LastIndexOf(")") - s1.LastIndexOf("(") - 1);


string s2 = s1.Substring(s1.LastIndexOf("(") + 1, s1.LastIndexOf(")") - s1.LastIndexOf("(") - 1);


 string e1 = "here there (hi)";
//Extraction 
 string s2 = e1.Substring(e1.IndexOf("(")+1, (e1.LastIndexOf(")") - e1.IndexOf("("))-1);
0

精彩评论

暂无评论...
验证码 换一张
取 消