Browse Source

Added string replace all.

Branimir Karadžić 9 years ago
parent
commit
e5d3c045d5
1 changed files with 17 additions and 0 deletions
  1. 17 0
      include/bx/string.h

+ 17 - 0
include/bx/string.h

@@ -386,6 +386,23 @@ namespace bx
 		va_end(argList);
 		va_end(argList);
 	}
 	}
 
 
+	/// Replace all instances of substring.
+	template <typename Ty>
+	inline Ty replaceAll(const Ty& _str, const char* _from, const char* _to)
+	{
+		Ty str = _str;
+		size_t startPos = 0;
+		const size_t fromLen = strlen(_from);
+		const size_t toLen   = strlen(_to);
+		while ( (startPos = str.find(_from, startPos) ) != Ty::npos)
+		{
+			str.replace(startPos, fromLen, _to);
+			startPos += toLen;
+		}
+
+		return str;
+	}
+
 	/// Extract base file name from file path.
 	/// Extract base file name from file path.
 	inline const char* baseName(const char* _filePath)
 	inline const char* baseName(const char* _filePath)
 	{
 	{