C# 替换最接近的单词实例

C# 替换最接近的单词实例,c#,string,C#,String,我有这样一个字符串: “I’m a member of the Imperial Senate on a diplomatic mission to Alderaan.” string s = “I’m a member of the Imperial Senate on a diplomatic mission to Alderaan.”; string bolded = MakeStrongBefore("a", "diplomatic", s); 我想在“外交”中的“a”周围插入,但不

我有这样一个字符串:

“I’m a member of the Imperial Senate on a diplomatic mission to Alderaan.”
string s = “I’m a member of the Imperial Senate on a diplomatic mission to Alderaan.”;
string bolded = MakeStrongBefore("a", "diplomatic", s);
我想在“外交”中的“a”周围插入
,但不在其他地方

我的输入是前一个函数中的外交,我不想将
添加到最近的“a”实例中

现在,当然,当我使用
.Replace(“a”,“a
”)时,“a”的每个实例都会接受
-处理,但有没有办法将其应用于我想要的一个实例

编辑

字符串和单词/char(“a”(在上面的例子中)可以是任何东西,因为我循环了很多,所以解决方案必须是动态的。

这就足够了吗

string MakeStrongBefore(string strong, string before, string s)
{
    return s.Replace(strong + " " + subject, "<strong>" + strong + "</strong> " + before);
}
var stringyourusing=”“;
var字母=”;
var regex=新regex(regex.Escape(字母));
var newText=regex.Replace(使用“字母””,1);
试试这个:

public string BoldBeforeString(string source, string bolded, 
                               int boldBeforePosition)
{   
    string beforeSelected = source.Substring(0, boldBeforePosition).TrimEnd();

    int testedWordStartIndex = beforeSelected.LastIndexOf(' ') + 1;

    string boldedString;
    if (beforeSelected.Substring(testedWordStartIndex).Equals(bolded))
    {
        boldedString = source.Substring(0, testedWordStartIndex) + 
                       "<strong>" + bolded + "</strong>" + 
                       source.Substring(testedWordStartIndex + bolded.Length);
    }
    else 
    {
        boldedString = source;
    }

    return boldedString;
}

string phrase = "I’m a member of the Imperial Senate on a diplomatic mission to Alderaan.";
string boldedPhrase = BoldBeforeString(phrase, "a", 41);
public string boldbeforesting(字符串源,字符串粗体,
整型黑体字(前置)
{   
string beforeSelected=source.Substring(0,boldBeforePosition.TrimEnd();
int testedWordStartIndex=在选择之前。LastIndexOf(“”)+1;
字符串粗体字符串;
if(在选定的子字符串(TestedOrdStartIndex).Equals(粗体)之前)
{
boldedString=source.Substring(0,TestedOrdStartIndex)+
“”+粗体+”“+
source.Substring(testedWordStartIndex+粗体.Length);
}
其他的
{
boldedString=源;
}
返回粗体字符串;
}
string phrase=“我是帝国参议院的一名成员,在奥德朗执行外交任务。”;
字符串boldedPhrase=boldbeforesting(短语“a”,41);

我已经对此进行了测试,并且效果良好:

String replaced = Regex.Replace(
    "I’m a member of the Imperial Senate on a diplomatic mission to Alderaan.",
    @"(a) diplomatic",
    match => "<strong>" + match.Result("$1") + "</strong>");
编辑:

考虑到您的其他评论,这是一个在搜索词周围的每个词周围放置强标记的功能:

public static String StrongReplace(String sentence, String word)
{
    return Regex.Replace(
        sentence,
        @"(\w+) " + Regex.Escape(word) + @" (\w+)",
        match => "<strong>" + match.Result("$1") + "</strong> " + word + " <strong>" + match.Result("$2") + "</strong>");
}
公共静态字符串strong替换(字符串句子、字符串单词)
{
返回正则表达式。替换(
判决,
@“(\w+)+Regex.Escape(word)+@”(\w+)”,
match=>“”+match.Result(“$1”)+”“+word+”“+match.Result(“$2”)+””;
}

我应该提到,“我是帝国元老院的一名成员,在奥德朗执行外交任务。”这只是众多数字中的一个随机字符串,“a”只是一个例子。我需要一种动态方式,其中我有一个输入字符串和一个输入单词/字符。在您的随机示例中,
a
forecutical
是否都会传递给函数?是的,我有
a
forecutical
和字符串,还有一个问题:
strong
是否会出现在比
之前的
更远的位置?在我选择的单词之前应该只有一个strong,但后面也会有一个strong。我的目标是在所选单词的两边都有
。我可能会尝试在“外交”上拆分字符串,并使用FirstIndex或LastIndex来查找要替换的“a”。这不会导致
“我是帝国al Senate的adiplomatic使团成员,前往桤木aan。”
?@MagnusEngdal-nope 1使它只在第一次出现时才出现。我只是快速尝试了一下,以确保它能正常工作。@AlexBeisley你是什么意思?我的怎么不这样做呢。
public static String StrongReplace(String sentence, String toStrong, String wordAfterStrong)
{
    return Regex.Replace(
        sentence,
        @"("+Regex.Escape(toStrong)+") " + Regex.Escape(wordAfterStrong),
        match => "<strong>" + match.Result("$1") + "</strong>");
}
String sentence = "I’m a member of the Imperial Senate on a diplomatic mission to Alderaan.";
String replaced = StrongReplace(sentence, "a", "diplomatic");
public static String StrongReplace(String sentence, String word)
{
    return Regex.Replace(
        sentence,
        @"(\w+) " + Regex.Escape(word) + @" (\w+)",
        match => "<strong>" + match.Result("$1") + "</strong> " + word + " <strong>" + match.Result("$2") + "</strong>");
}