本文介绍了从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...

推荐答案


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
}







[]


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#中获取标记内的子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 09:39