diff --git a/SystemTools.cxx b/SystemTools.cxx
index 12fabb40037e1a55fbb5033da22de290ffaea4cb..706ab48a5e67c90fdf284798db705200eb947037 100644
--- a/SystemTools.cxx
+++ b/SystemTools.cxx
@@ -252,23 +252,37 @@ void SystemTools::ReplaceString(kwsys_std::string& source,
                                    const char* replace,
                                    const char* with)
 {
+  const char *src = source.c_str();
+  char *searchPos = strstr(src,replace);
+  
   // get out quick if string is not found
-  kwsys_std::string::size_type start = source.find(replace);
-  if(start ==  kwsys_std::string::npos)
+  if (!searchPos)
     {
     return;
     }
-  kwsys_std::string rest;
-  kwsys_std::string::size_type lengthReplace = strlen(replace);
-  kwsys_std::string::size_type lengthWith = strlen(with);
-  while(start != kwsys_std::string::npos)
-    {
-    rest = source.substr(start+lengthReplace);
-    source = source.substr(0, start);
+
+  // perform replacements until done
+  size_t replaceSize = strlen(replace);
+  char *orig = strdup(src);
+  char *currentPos = orig;
+  searchPos = searchPos - src + orig;
+  
+  // initialize the result
+  source.clear();
+  do
+    {
+    *searchPos = '\0';
+    source += currentPos;
+    currentPos = searchPos + replaceSize;
+    // replace
     source += with;
-    source += rest;
-    start = source.find(replace, start + lengthWith );
+    searchPos = strstr(currentPos,replace);
     }
+  while (searchPos);
+
+  // copy any trailing text
+  source += currentPos;
+  free(orig);
 }
 
 // Read a registry value.