如何解析给定字符串中的字符串? [英] How can I parse string from a given string ?

查看:104
本文介绍了如何解析给定字符串中的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个样本字符串,例如测试它@Number(1,2,3,4 ......)你有



如果我找到@字符串中的Numebr我需要将@Number开始的部分带到结束括号)。



I have sample string like "Test it @Number(1,2,3,4....) that you got"

if I find @Numebr in the string I need to take the portion where @Number is start to the closing bracket ")".

string data= "Anythig can be there( happened @Number(1,7, bad, nice) here)."

output: @Number(1,7, bad, nice)

推荐答案

1。找到位置@Number

这可以通过 IndexOf函数 [ ^ ]



2.找到第一个位置(

这个手杖完成了重载 IndexOf功能 [ ^ ]其中startindex是前一个值电话(对于@Number)



3.找到第一个的位置(

与2相同,但startindex是值#2不是#1。



4.从总字符串中获取部分。

使用子串函数 [ ^ ]



有了这个,你应该能够把它弄好:-)。

希望这会有所帮助。
1. Find position "@Number"
This can be done with the IndexOf function[^]

2. Find the position of the first "("
This cane be done with the overloaded IndexOf function[^] where startindex is the value of the previous call (for @Number)

3. Find the position of the first "("
Same as 2 but startindex is the value of #2 not #1.

4. Get the portion out of the total string.
Use the Substring function[^]

With this, you should be able to get it right :-).
Hope this helps.


你可以使用子串函数来做到这一点,



you can do this using substring function,

string data= "Anythig can be there( happened @Number(1,7, bad, nice) here).";
string sub = data.Substring(data.indexOf("@Number"));
string sub1 = sub.Substring(0, data.indexOf(")")+1);





注意:



这个没有经过测试但是如果你玩它会有效,因为这是一种方法



Note :

this is not tested but it will work if you play around it because this the one way to do it


你也可以先使用正则表达式。



You can start by using regular expression also.

Regex regex = new Regex(@"@\w+\([\w|,|\s]+\)");

Match match = regex.Match(@"Anythig can be there( happened @Number(1,7, bad, nice) here)");
if (match.Success)
{
    Console.WriteLine(match.Value);
}


这篇关于如何解析给定字符串中的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆