从Mainstring C#中获取标记内的子字符串 [英] Get a substring inside a tag from Mainstring C#
本文介绍了从Mainstring C#中获取标记内的子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨我有这样的字符串
Hi I have a string like this
"[fconsender]Username (11:00 AM):[/fconsender] Hi Good Afternoon!"
或
or
"[fconreceiver]AnotherUser (11:02 AM):[/fconreceiver] Yea! How are you ?"
我想在[fconsender]和[/ fconsender]中提取字符串或
[ fconreceiver]和[/ fconreceiver] ...
如何在不使用太多循环的情况下简单地实现它。
它只是为了改变发送者/接收者名称的颜色...
I want to extract the string inside [fconsender] and [/fconsender] or
[fconreceiver] and [/fconreceiver]...
How can I simply achieve it without using much loops,,.
Its just to change the colour of sender/reciever name...
推荐答案
当然,没有循环。请参阅:
http:// msdn。 microsoft.com/en-us/library/ms131448(v=vs.110).aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/tabh47cf(v = vs.110)的.aspx [<一个href =http://msdn.microsoft.com/en-us/library/tabh47cf(v=vs.110).aspx\"target =_ blanktitle =New Window> ^ ],
http://msdn.microsoft.com/ en-us / library / kwb0bwyd(v = vs.110).aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/k8b1470s(v=vs.110).aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/hxthx5h6(ⅴ = vs.110).aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/aka44szs(v = vs.110).aspx [ ^ ]。
或 http://msdn.microsoft .com / zh-CN / library / system.text.regularexpressions.regex%28v = vs.110%29.aspx [ ^ ]。
-SA
Of course, no loops. Please see:
http://msdn.microsoft.com/en-us/library/ms131448(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/tabh47cf(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/kwb0bwyd(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/k8b1470s(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/hxthx5h6(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/aka44szs(v=vs.110).aspx[^].
Or http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex%28v=vs.110%29.aspx[^].
—SA
您可以使用以下函数..
You can use following function..
string GetMiddleString(string input, string firsttoken, string lasttoken)
{
int pos1 = input.IndexOf(firsttoken) + 1;
int pos2 = input.IndexOf(lasttoken);
string result = input.Substring(pos1 , pos2 - pos1);
return result
}
http://stackoverflow.com/a/2449659 [ ^ ]
试用此代码..
工作正常..
Try this code..
works fine..
string input = "[fconsender]Username (11:00 AM):[/fconsender] Hi Good Afternoon!";
string starttag = "[fconsender]";
string endtag = "[/fconsender]";
int sindex = input.IndexOf(starttag) + starttag.Length;
int length = input.IndexOf(endtag) - starttag.Length;
string output = input.Substring(sindex, length);
这篇关于从Mainstring C#中获取标记内的子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文