0

I want to check if a string contain following format

   [QUOTE]
    Test sentence
    [/QUOTE]

If that so then I will do this.

            string description = dr["description"].ToString();
            description = description.Replace("[QUOTE]", "<blockquote>");
            description = description.Replace("[/QUOTE]", "</blockquote>");

This is OK.

but how about this one?

[QUOTE=Axio;26]
Test sentence
[/QUOTE]

Also In here I want to add blockquote tag and as well as Want to display this text inside the those tags

"Orginall posted by Axio. Click here"

When you click "Click HEre" you will go to that specific post. So that should be a hyperlink" 26 is the post id

How to do this?

Prageeth Liyanage
  • 1,612
  • 2
  • 19
  • 41

3 Answers3

1

You can use a regular expression to match whatever is in [QUOTE] and transform it afterward with Split on semicolon. Something like this:

        var regexPattern = @"\[QUOTE[=]{0,1}([\d\w;]*)\](.|\r|\n)*\[/QUOTE\]";
        var test1 = @"[QUOTE=Axio;26]
            Test sentence
            [/QUOTE]";
        var test2 = @"[QUOTE]
            Test sentence
            [/QUOTE]";

        var regex = new Regex(regexPattern);

        var match = regex.Match(test1);
        if (match.Success)
        {
            if (match.Groups.Count > 1) //matched [QUOTE=...]
                match.Groups[1].Value.Split(';').ToList().ForEach(s => Console.WriteLine(s));
            else //matched [QUOTE]..
                Console.WriteLine("Matched [QUOTE]");
        }
        else Console.WriteLine("No match"); 
        Console.Read();
KMoussa
  • 1,568
  • 7
  • 11
0

Many way to do this, for example:

 string des = dr["description"].ToString().Replace("\n", "");
 string info[] = des.SubString(des.IndexOf('=') + 1, des.IndexOf(']')).Split(';');
 string name = info[0];
 string id = info[1]
 string sentence = des.SubString(des.IndexOf(']') + 1, des.LastIndexOf('['));

when you got this you know what to do. i wrote it by hand, you may need adjust it youself (substring position not sure if need add/sub 1).

Bucketcode
  • 461
  • 2
  • 13
  • Even when you add so you don't have an ArgumentOutOfRange exception, you need to subtract your starting index to get the substring. And probably use the first occurance of ']' Otherwise you wind up with something like `[0]: "Axio"` `[1]: "26]Orgina"` – Fus Ro Dah Nov 01 '16 at 18:35
0
//Get the description text
var description = "[QUOTE=Axio;26]Orginall posted by Axio. Click here[/QUOTE]";
//Get your id
var id = description.Substring(description.IndexOf(";") + 1, description.IndexOf("]") - (description.IndexOf(";") + 1));

//replace with anchor with id and <blockquotes/>
var editedstring = description
     .Remove(description.IndexOf("["), description.IndexOf("]") + 1)
     .Insert(0, "<blockquote><a href=\"#" + id + "\">")
     .Replace("[/QUOTE]", "</a></blockquote>");

Result:

 <blockquote><a href="#26">Orginall posted by Axio. Click here</a> </blockquote>
Orginall posted by Axio. Click here
Fus Ro Dah
  • 331
  • 5
  • 22