-2

I have a string in C# with "wiki syntax" that i would like to replace values in.

"My '''random''' text with '''bold''' words."

translate to:

"My <b>random</b> text with <b>bold</b> words."

The problem is that I would like to replace pairs of values to different values.

odd ''' => <b>
even ''' => </b>
Andreas
  • 6,958
  • 10
  • 38
  • 52

5 Answers5

3

To add one more option to the mix: Regex.Replace can be used with a callback specifying the replacement string:

var txt = "My '''random''' text with '''bold''' words.";

int i = 0;
var newtext = new Regex("'''").Replace(txt, m => i++ % 2 == 0 ? "<B>" : "</B>" );
Me.Name
  • 12,259
  • 3
  • 31
  • 48
0

Evil hack: include the spaces and

replace " '''" with <b>
and "''' " with </b>

Works until it fails for some reason ;)

COeDev
  • 336
  • 3
  • 9
0

This should work aswell:

static string ReplaceEvenOdd(string s, string syntax, string odd, string even)
{
    string[] split = s.Split(new[] { syntax }, StringSplitOptions.None);
    string result = string.Empty;
    for (int i = 0; i < split.Length; i++)
    {
        result += split[i];
        if (i < split.Length - 1)
            result += (i + 1) % 2 == 0 ? even : odd;
    }
    return result;
}
Ian H.
  • 3,840
  • 4
  • 30
  • 60
  • I believe it won't work because - although it is not stated in the OP - `'''` are to be matched in-context. At least that is how it is done in SO comments. Let OP provide feedback though. – Wiktor Stribiżew Dec 16 '16 at 13:25
0

You can do this.

string c = "My '''random''' text with '''bold''' words.";

string[] tags = {"<b>", "</b>"};

for (int i = 0, index; (index = c.IndexOf("'''", StringComparison.Ordinal)) > 0; i++)
{
    var temp = c.Remove(index, 3);
    c = temp.Insert(index, tags[i % 2]);
}
  • Get index of '''
  • IndexOf will return negative number if match is not found other wise will give position of '''
  • Remove 3 characters from that position (i.e ''').
  • if counter is even insert <b> otherwise </b>
M.kazem Akhgary
  • 18,645
  • 8
  • 57
  • 118
0

While this has a loop similar to other answers, a minor difference is the selection of the opening and closing tags - instead of calculating modulo 2 to determine if we use an opening or closing tag, I define the pair of tags in an array of size 2:

String[] replaceText = new String[] { "<B>", "</B>" };

Select the element based on a variable iReplacerIndex

replaceText[iReplacerIndex]  // Gives either the opening or closing tag 

And toggle between the required values by subtracting from 1.

iReplacerIndex = 1 - iReplacerIndex; // If last used was an opening tag, 
                                     // then next required is a closing tag 

The above also makes it easy to check for mismatched tags - if iReplacerIndex is not 0 after the loop, then there is a mismatched tag.

The entire code is as follows (its lengthier than it needs to be for clarity):

String sourceText = "My '''random''' text with '''bold''' words.";
int sourceLength = sourceText.Length;
String searchText = "'''";
int searchLength = searchText.Length;
String[] replaceText = new String[] { "<B>", "</B>" };

int iReplacerIndex = 0
  , iStartIndex = 0 
  , iStopIndex = sourceText.Length - 1;
System.Text.StringBuilder sbCache = new System.Text.StringBuilder(sourceText.Length * 2);

do
{
    iStopIndex = sourceText.IndexOf(searchText, iStartIndex);

    if (iStopIndex == -1)
    {
        sbCache.Append(sourceText.Substring(iStartIndex, sourceLength - iStartIndex));
    }
    else
    { 
    sbCache.Append(sourceText.Substring(iStartIndex, iStopIndex - iStartIndex));
    sbCache.Append(replaceText[iReplacerIndex]);

    iReplacerIndex = 1 - iReplacerIndex;
    iStartIndex = iStopIndex + searchLength;
    }
} while (iStopIndex != -1);

Console.WriteLine(sbCache.ToString());
Phylyp
  • 1,659
  • 13
  • 16