시도 뭔가 ...
static string ReplaceOccurrence(string input, string wordToReplace, string replaceWith, int occToReplace)
{
MatchCollection matches = Regex.Matches(input, string.Format("([\\w]*)", wordToReplace), RegexOptions.IgnoreCase);
int occurrencesFound = 0;
int captureIndex = 0;
foreach (Match matchItem in matches)
{
if (matchItem.Value == wordToReplace)
{
occurrencesFound++;
if (occurrencesFound == occToReplace)
{
captureIndex = matchItem.Index;
break;
}
}
}
if (captureIndex > 0)
{
return string.Format("{0}{1}{2}", input.Substring(0, captureIndex), replaceWith, input.Substring(captureIndex + wordToReplace.Length));
} else
{
return input;
}
}
당신은 상단에 using System.Text.RegularExpressions;
을 넣어해야합니다.
Word 문서 (예 : 매크로)에서 실행되는 코드를 작성 하시겠습니까? 아니면 Word 문서를 수정하는 서버에서 코드를 실행 하시겠습니까? –
실제로 나는 http://www.codeproject.com/KB/edit/Application_to_Word.aspx에서 그 길을 원합니다. 이 링크는 하나를 대체하고 모두 바꾸는 방법을 제공합니다. 그래서 나는 그런 식으로하고 싶습니다. 내가 필요로하는 것은 –