Best way for this string.replace?

hoi all...
i have a string with some placeholders in it. this string is added to a StringBuilder in a loop. While adding it, i replace each placeholder with another stirng. currently it looks like this:
.... loop start...
    "[forumSubject]", forumSubject).Replace(
    stringPlaceholderOne, oneTopic.getReplyCount().ToString()).Replace(
    "[countViews]", oneTopic.getViewCount().ToString()).Replace(
    "[lastPostAuthor]", oneTopic.getLastPostAuthorNick()).Replace(
    "[lastPostDate]", oneTopic.getLastPostDateFormatted())
... loop end...

the code above has a problem:
If a placeholder variable ("stringPlaceholderOne") is an empty string (length=0), then the .Replace() throws an exception.

What is the best way to handle this?
I thought about a simple code like this:
static public string replaceInString(string inString, string inFind, string inReplaceWith) {
   if (inString.IndexOf(inFind) >= 0) {
      return inString.Replace(inFind, inReplaceWith);
   } else {
      return inString;

But I believe there are better ways to do it... e.g. extend the normal string with my own replace method?
is this possible? how?
any other idea?

Thank you
Who is Participating?
Also, it may be worth checking your code for a more efficient solution like this, which can handle any placeholder in square brackets:

uses System.Text.RegularExpressions;

      private string ReplacePlaceholder(Match match) {
            switch (match.Groups["placeholder"].Value) {
            case "forumSubject":
                  return forumSubject;
            case "countViews":
                  return oneTopic.getViewCount().ToString();
            case "lastPostAuthor":
                  return oneTopic.getLastPostAuthorNick();
            case "lastPostDate":
                  return oneTopic.getLastPostDateFormatted();
                  if (match.Value==stringPlaceholderOne) {
                        return oneTopic.getReplyCount().ToString();
            return match.Value; // set this to "" if you want all undknown placeholders to be deleted

            listCode.Append(Regex.Replace(forumTemplate.getLoopCode(), @"\[(?<placeholder>[^\]]*)\]", new MatchEvaluator(ReplacePlaceholder)));

Why would "stringPlaceholderOne" be null?
Or, sorry: An empty string?
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Oh - my apologies again!  I understand the problem now...  Unfortunately you cannot extend (inherit from) the System.String or System.Text.StringBuilder classes, as they are sealed.
SmoerbleAuthor Commented:
Hi again.
For clarification:
There might be the following case:
stringPlaceholderOne = "";

In this case, teh string.Replace() crashes.

So, if you say, I can't extend the string class... any idea, what else I could do to get it really fast done?
Well, it does make sense that you cannot use "" as search pattern for replacements.

Try this: before the call, make sure that stringPlaceholderOne is not empty. I'd suggest this workaround:

if (stringPlaceholderOne.Length==0)

Then it will work as expected, since the placeHolderOne will not be ampty, but will contain a pattern which will certainly not appear in the string to be replaced.
SmoerbleAuthor Commented:
This is a perfect solution, thank you!
Unfortunatly I can't give your more points than 500 total :(

Thanks again (sorry for the delay with the points).
Tanks for grading the question!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.