C# 字符串替换C中的奇偶值#
我有一个C#中带有“wiki语法”的字符串,我想替换其中的值C# 字符串替换C中的奇偶值#,c#,regex,replace,C#,Regex,Replace,我有一个C#中带有“wiki语法”的字符串,我想替换其中的值 "My '''random''' text with '''bold''' words." translate to: "My <b>random</b> text with <b>bold</b> words." “我的”“随机”“文本和”“粗体”“文字。” 翻译为: “我用粗体字写的随机文本。” 问题是我想将成对的值替换为不同的值 odd ''' => <b>
"My '''random''' text with '''bold''' words."
translate to:
"My <b>random</b> text with <b>bold</b> words."
“我的”“随机”“文本和”“粗体”“文字。”
翻译为:
“我用粗体字写的随机文本。”
问题是我想将成对的值替换为不同的值
odd ''' => <b>
even ''' => </b>
odd'=>
偶数“”=>
邪恶黑客:包括空格和
replace " '''" with <b>
and "''' " with </b>
将“''”替换为
和“'''”一起
直到由于某种原因失败为止;) 这也应该有效:
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;
}
静态字符串替换偶数(字符串s、字符串语法、字符串奇数、字符串偶数)
{
string[]split=s.split(新的[]{syntax},StringSplitOptions.None);
字符串结果=string.Empty;
for(int i=0;i
您可以这样做
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]);
}
string c=“我的”“随机”“文本和”“粗体”“单词。”;
字符串[]标记={“”“”};
对于(inti=0,index;(index=c.IndexOf(“''”,StringComparison.Ordinal))>0;i++)
{
var temp=c.移除(索引,3);
c=临时插入(索引,标签[i%2]);
}
- 获取
''
- 如果未找到匹配项,
将返回负数,否则将给出IndexOf
''
- 从该位置删除3个字符(即
)''
- 如果计数器为偶数,则插入
,否则
String[] replaceText = new String[] { "<B>", "</B>" };
通过从1中减去,在所需值之间切换
iReplacerIndex = 1 - iReplacerIndex; // If last used was an opening tag,
// then next required is a closing tag
上述方法还可以轻松检查不匹配的标记-如果循环后iReplacerIndex
不是0,则存在不匹配的标记
整个代码如下(为清晰起见,其长度比需要的长度长):
String sourceText=“我的”“随机”“文本和”“粗体”“单词。”;
int sourceLength=sourceText.Length;
字符串searchText=“””;
int searchLength=searchText.Length;
字符串[]replaceText=新字符串[]{“”“”};
int iReplacerIndex=0
,iStartIndex=0
,iStopIndex=sourceText.Length-1;
System.Text.StringBuilder sbCache=新的System.Text.StringBuilder(sourceText.Length*2);
做
{
iStopIndex=sourceText.IndexOf(searchText,iStartIndex);
如果(iStopIndex==-1)
{
追加(sourceText.Substring(iStartIndex,sourceLength-iStartIndex));
}
其他的
{
追加(sourceText.Substring(iStartIndex,iStopIndex-iStartIndex));
Append(replaceText[iReplacerIndex]);
iReplacerIndex=1-iReplacerIndex;
IStarIndex=iStopIndex+搜索长度;
}
}而(iStopIndex!=-1);
Console.WriteLine(sbCache.ToString());
要向混合中添加更多选项,请执行以下操作: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>" );
var txt=“我的”“随机”“文本和”“粗体”“单词。”;
int i=0;
var newtext=newregex(“”)。替换(txt,m=>i++%2==0?”:“”);
到目前为止您尝试了什么?使用空格+''和''''+空格可能更好?@LeonidMalyshev在一般情况下您不能依赖它。如何:Regex.Replace(输入,“''.''”(.''?''”,“$1”)
我相信它不会起作用,因为-'.
虽然OP中没有说明,但它是在上下文中匹配的。至少在SO评论中是这样做的。让OP提供反馈。在我看来,这是最好的答案!我将其转换为通用的扩展方法:
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());
var txt = "My '''random''' text with '''bold''' words.";
int i = 0;
var newtext = new Regex("'''").Replace(txt, m => i++ % 2 == 0 ? "<B>" : "</B>" );