2016-12-16 11 views
-2

に私はで値を置換したい「ウィキ構文」とC#で文字列を持っている偶数と奇数の値を置き換える。文字列は、C#

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

translate to: 

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

問題は、私は値のペアを交換したいということです異なる値に変更する。

odd ''' => <b> 
even ''' => </b> 
+1

これまでに何を試しましたか? – kiziu

+0

はスペース+ '' 'と' '' +スペースを使う方が良いかもしれませんか? –

+0

@ LeonidMalyshevあなたは一般的なケースでそれに頼ることはできません。 – juharr

答えて

0

悪ハック:それはいくつかの理由で失敗するまでのスペースと

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

作品が含まれる;)

0

これは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; 
} 
+0

OPで述べられていませんが - '' ''は文脈にマッチします。少なくともそれはSOのコメントでどのように行われるかです。 OPがフィードバックをもらいましょう。 –

0

あなたがこれを行うことができます。

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]); 
} 
  • 試合は他の賢明がその位置(すなわち''')から3つの文字を削除'''
  • の位置を与える見つからない場合'''
  • IndexOfのインデックスが負の数を返します取得します。これは他の回答に似たループを有しているが
  • カウンターも<b>を挿入されている場合は、それ以外の</b>
0

、マイナーな違いは、開始タグと終了タグの選択である - 代わりにモジュロ2を計算する我々が使用するかどうかを判断します開閉タグ、Iは、サイズ2のアレイ内のタグのペアを定義する:

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

可変iReplacerIndex

に基づいて要素を選択します

と1

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

上方から減算することにより、必要な値を切り替えもミスマッチタグを確認することが容易になり - iReplacerIndexループ後に0でない場合、次に不一致タグがあります。

次のようにコード全体が(それが明確にするために必要以上のより長い)である:

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()); 
1

ミックス1つの以上のオプションを追加する:Regex.Replaceは、交換の指定コールバックで使用することができ文字列:

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

int i = 0; 
var newtext = new Regex("'''").Replace(txt, m => i++ % 2 == 0 ? "<B>" : "</B>");