Index: Include/Classes/System/String.ab
===================================================================
--- Include/Classes/System/String.ab	(revision 270)
+++ Include/Classes/System/String.ab	(revision 272)
@@ -2,4 +2,6 @@
 
 #require <basic/function.sbp>
+#require <Classes/System/Text/StringBuilder.ab>
+#require <Classes/ActiveBasic/Strings/Strings.ab>
 
 #ifdef __STRING_IS_NOT_ALWAYS_UNICODE
@@ -21,47 +23,75 @@
 
 		m_Length As Long
+		Chars As *StrChar
+
+		Sub validPointerCheck(p As VoidPtr, size = 1 As Long)
+			If p As ULONG_PTR < &h10000 Then
+				'Throw ArgumentException
+				Debug
+			ElseIf IsBadReadPtr(p, size As ULONG_PTR) Then
+				'Throw ArgumentException
+				Debug
+			End If
+		End Sub
 	Public
-		Chars As *StrChar
+		Static Const Empty = New String
 
 		Sub String()
-			Chars = _System_malloc(SizeOf (StrChar))
-			Chars[0] = 0
-			m_Length = 0
+'			Chars = 0
+'			m_Length = 0
+		End Sub
+
+		Sub String(initStr As PCWSTR)
+			validPointerCheck(initStr)
+			Assign(initStr, lstrlenW(initStr))
+		End Sub
+
+		Sub String(initStr As PCWSTR, length As Long)
+			validPointerCheck(initStr, length)
+			Assign(initStr, length)
+		End Sub
+
+		Sub String(initStr As PCWSTR, start As Long, length As Long)
+			If start < 0 Or length Or start + length < 0 Then
+				'Throw New ArgumentOutOfRangeException
+			End If
+			validPointerCheck(initStr + start, length)
+			Assign(initStr + start, length)
 		End Sub
 
 		Sub String(initStr As PCSTR)
-			Assign(initStr)
+			validPointerCheck(initStr)
+			Assign(initStr, lstrlenA(initStr))
 		End Sub
 
 		Sub String(initStr As PCSTR, length As Long)
+			validPointerCheck(initStr, length)
 			Assign(initStr, length)
 		End Sub
 
-		Sub String(initStr As PCWSTR)
-			Assign(initStr)
-		End Sub
-
-		Sub String(initStr As PCWSTR, length As Long)
-			Assign(initStr, length)
-		End Sub
-
-		Sub String(ByRef initStr As String)
-			Assign(initStr)
-		End Sub
-
-		Sub String(length As Long)
-			ReSize(length)
+		Sub String(initStr As PCSTR, start As Long, length As Long)
+			If start < 0 Or length Or start + length < 0 Then
+				'Throw New ArgumentOutOfRangeException
+			End If
+			validPointerCheck(initStr + start, length)
+			Assign(initStr + start, length)
+		End Sub
+		
+		Sub String(initStr As String)
+			If Not String.IsNullOrEmpty(initStr) Then
+				Assign(initStr.Chars, initStr.m_Length)
+			End If
 		End Sub
 
 		Sub String(initChar As StrChar, length As Long)
-			ReSize(length, initChar)
-		End Sub
-
-		Sub ~String()
-			_System_free(Chars)
-			Chars = 0
-#ifdef _DEBUG
-			m_Length = 0
-#endif
+			AllocStringBuffer(length)
+			ActiveBasic.Strings.ChrFill(Chars, length, initChar)
+			Chars[length] = 0
+		End Sub
+
+		Sub String(sb As System.Text.StringBuilder)
+			Chars = StrPtr(sb)
+			m_Length = sb.Length
+			sb.__Stringized()
 		End Sub
 
@@ -75,100 +105,80 @@
 
 		Const Function Operator [] (n As Long) As StrChar
-#ifdef _DEBUG
-			If n > Length Then
-				'Throw ArgumentOutOfRangeException
-				Debug
-			End If
-#endif
+			rangeCheck(n)
 			Return Chars[n]
 		End Function
 
-		Sub Operator []= (n As Long, c As StrChar)
-#ifdef _DEBUG
-			If n >= Length Then
-				'Throw ArgumentOutOfRangeException
-				Debug
-			End If
-#endif
-			Chars[n] = c
-		End Sub
-
-	/*	Const Function Operator + (text As *Byte) As String
-			Return Concat(text As PCTSTR, lstrlen(text))
-		End Function*/
-
-		Const Function Operator + (text As PCSTR) As String
-			Return Concat(text, lstrlenA(text))
-		End Function
-
-		Const Function Operator + (text As PCWSTR) As String
-			Return Concat(text, lstrlenW(text))
-		End Function
-
-		Const Function Operator + (objString As String) As String
-			Return Concat(objString.Chars, objString.m_Length)
-		End Function
-
-		Const Function Operator & (text As PCSTR) As String
-			Dim tempString = This + text
+		Const Function Operator + (y As PCSTR) As String
+			Return Concat(y, lstrlenA(y))
+		End Function
+
+		Const Function Operator + (y As PCWSTR) As String
+			Return Concat(y, lstrlenW(y))
+		End Function
+
+		Const Function Operator + (y As String) As String
+			Return Concat(y.Chars, y.m_Length)
+		End Function
+
+		Const Function Operator & (y As PCSTR) As String
+			Return This + y
+		End Function
+
+		Const Function Operator & (y As PCWSTR) As String
+			Dim tempString = This + y
 			Return tempString
 		End Function
 
-		Const Function Operator & (text As PCWSTR) As String
-			Dim tempString = This + text
+		Const Function Operator & (y As String) As String
+			Dim tempString = This + y
 			Return tempString
 		End Function
 
-		Const Function Operator & (objString As String) As String
-			Dim tempString = This + objString
-			Return tempString
-		End Function
-
-		Const Function Operator == (objString As String) As Boolean
-			Return String.Compare(This, objString) = 0
-		End Function
-
-		Const Function Operator == (text As *StrChar) As Boolean
-			Return _System_StrCmp(This.Chars, text) = 0
-		End Function
-
-		Const Function Operator <> (objString As String) As Boolean
-			Return String.Compare(This, objString) <> 0
-		End Function
-
-		Const Function Operator <> (text As *StrChar) As Boolean
-			Return _System_StrCmp(This.Chars, text) <> 0
-		End Function
-
-		Const Function Operator < (objString As String) As Boolean
-			Return String.Compare(This, objString) < 0
-		End Function
-
-		Const Function Operator < (text As *StrChar) As Boolean
-			Return _System_StrCmp(This.Chars, text) < 0
-		End Function
-
-		Const Function Operator > (objString As String) As Boolean
-			Return String.Compare(This, objString) > 0
-		End Function
-
-		Const Function Operator > (text As *StrChar) As Boolean
-			Return _System_StrCmp(This.Chars, text) > 0
-		End Function
-
-		Const Function Operator <= (objString As String) As Boolean
-			Return String.Compare(This, objString) <= 0
-		End Function
-
-		Const Function Operator <= (text As *StrChar) As Boolean
-			Return _System_StrCmp(This.Chars, text) <= 0
-		End Function
-
-		Const Function Operator >= (objString As String) As Boolean
-			Return String.Compare(This, objString) >= 0
-		End Function
-
-		Const Function Operator >= (text As *StrChar) As Boolean
-			Return _System_StrCmp(This.Chars, text) >= 0
+		Const Function Operator == (y As String) As Boolean
+			Return String.Compare(This, y) = 0
+		End Function
+
+		Const Function Operator == (y As *StrChar) As Boolean
+			Return String.Compare(This, y) = 0
+		End Function
+
+		Const Function Operator <> (y As String) As Boolean
+			Return String.Compare(This, y) <> 0
+		End Function
+
+		Const Function Operator <> (y As *StrChar) As Boolean
+			Return String.Compare(This, y) <> 0
+		End Function
+
+		Const Function Operator < (y As String) As Boolean
+			Return String.Compare(This, y) < 0
+		End Function
+
+		Const Function Operator < (y As *StrChar) As Boolean
+			Return String.Compare(This, y) < 0
+		End Function
+
+		Const Function Operator > (y As String) As Boolean
+			Return String.Compare(This, y) > 0
+		End Function
+
+		Const Function Operator > (y As *StrChar) As Boolean
+			Return String.Compare(This, y) > 0
+		End Function
+
+		Const Function Operator <= (y As String) As Boolean
+			Return String.Compare(This, y) <= 0
+		End Function
+
+		Const Function Operator <= (y As *StrChar) As Boolean
+			Return String.Compare(This, y) <= 0
+		End Function
+
+		Const Function Operator >= (y As String) As Boolean
+			Return String.Compare(This, y) >= 0
+		End Function
+
+		Const Function Operator >= (y As *StrChar) As Boolean
+			Return String.Compare(This, y) >= 0
 		End Function
 
@@ -177,25 +187,51 @@
 		End Function
 
+	Public
 		Static Function Compare(x As String, indexX As Long, y As String, indexY As Long, length As Long) As Long
-			Return CompareOrdinal(x, indexX, y, indexY, length)
+			Return String.CompareOrdinal(x, indexX, y, indexY, length)
 		End Function
 
 		Static Function CompareOrdinal(x As String, y As String) As Long
-			Return _System_StrCmp(x.Chars, y.Chars)
+			Return String.CompareOrdinal(x.Chars, y.Chars)
 		End Function
 
 		Static Function CompareOrdinal(x As String, indexX As Long, y As String, indexY As Long, length As Long) As Long
-			If Object.ReferenceEquals(x, Nothing) Then
-				If Object.ReferenceEquals(y, Nothing) Then
+			Return String.CompareOrdinal(x.Chars, indexX, y.Chars, indexY, length)
+		End Function
+	Private
+		Static Function Compare(x As String, y As *StrChar) As Long
+			Return String.CompareOrdinal(x, y)
+		End Function
+
+		Static Function CompareOrdinal(x As String, y As *StrChar) As Long
+			Return String.CompareOrdinal(x.Chars, y)
+		End Function
+
+		Static Function CompareOrdinal(x As *StrChar, y As *StrChar) As Long
+			If x = 0 Then
+				If y = 0 Then
 					Return 0
 				Else
 					Return -1
 				End If
-			ElseIf Object.ReferenceEquals(y, Nothing) Then
+			ElseIf y = 0 Then
 				Return 1
 			End If
-			Return _System_StrCmpN(VarPtr(x.Chars[indexX]), VarPtr(y.Chars[indexY]), length As SIZE_T)
-		End Function
-
+			Return ActiveBasic.Strings.StrCmp(x, y)
+		End Function
+
+		Static Function CompareOrdinal(x As *StrChar, indexX As Long, y As *StrChar, indexY As Long, length As Long) As Long
+			If x = 0 Then
+				If y = 0 Then
+					Return 0
+				Else
+					Return -1
+				End If
+			ElseIf y = 0 Then
+				Return 1
+			End If
+			Return ActiveBasic.Strings.ChrCmp(VarPtr(x[indexX]), VarPtr(y[indexY]), length As SIZE_T)
+		End Function
+	Public
 		Function CompareTo(y As String) As Long
 			Return String.Compare(This, y)
@@ -207,5 +243,5 @@
 	'			Throw New ArgumentException
 	'		End If
-			Return CompareTo(y)
+			Return CompareTo(y As String)
 		End Function
 
@@ -213,27 +249,5 @@
 			Return Chars
 		End Function
-
-		Sub ReSize(allocLength As Long)
-			If allocLength < 0 Then Exit Sub
-			Dim oldLength  = m_Length
-			If AllocStringBuffer(allocLength) <> 0 Then
-				If allocLength > oldLength Then
-					ZeroMemory(VarPtr(Chars[oldLength]), SizeOf (StrChar) * (m_Length - oldLength + 1))
-				Else
-					Chars[m_Length] = 0
-				End If
-			End If
-		End Sub
-
-		Sub ReSize(allocLength As Long, c As StrChar)
-			If allocLength < 0 Then Exit Sub
-			Dim oldLength = m_Length
-			If AllocStringBuffer(allocLength) <> 0 Then
-				If allocLength > oldLength Then
-					_System_FillChar(VarPtr(Chars[oldLength]), (m_Length - oldLength) As SIZE_T, c)
-				End If
-				Chars[m_Length] = 0
-			End If
-		End Sub
+Private
 
 		Sub Assign(text As PCSTR, textLengthA As Long)
@@ -261,50 +275,4 @@
 		End Sub
 
-		Sub Assign(ByRef objString As String)
-			Assign(objString.Chars, objString.m_Length)
-		End Sub
-
-		Sub Assign(text As PCSTR)
-			If text Then
-				Assign(text, lstrlenA(text))
-			Else
-				If Chars <> 0 Then
-					Chars[0] = 0
-				End If
-				m_Length = 0
-			End If
-		End Sub
-
-		Sub Assign(text As PCWSTR)
-			If text Then
-				Assign(text, lstrlenW(text))
-			Else
-				If Chars <> 0 Then
-					Chars[0] = 0
-				End If
-				m_Length = 0
-			End If
-		End Sub
-
-		Sub Append(text As *StrChar, textLength As Long)
-			Dim prevLen As Long
-			prevLen = m_Length
-			If AllocStringBuffer(m_Length + textLength) <> 0 Then
-				memcpy(VarPtr(Chars[prevLen]), text, SizeOf (StrChar) * textLength)
-				Chars[m_Length] = 0
-			End If
-		End Sub
-
-		Sub Append(text As *StrChar)
-			Append(text, lstrlen(text))
-		End Sub
-
-		Sub Append(ByRef str As String)
-			Append(str.Chars, str.m_Length)
-		End Sub
-
-		Const Function Clone() As String
-			Return This
-		End Function
 	Private
 		Static Function ConcatStrChar(text1 As *StrChar, text1Length As Long, text2 As *StrChar, text2Length As Long) As String
@@ -312,6 +280,6 @@
 			With ConcatStrChar
 				.AllocStringBuffer(text1Length + text2Length)
-				memcpy(.Chars, text1, SizeOf (StrChar) * text1Length)
-				memcpy(VarPtr(.Chars[text1Length]), text2, SizeOf (StrChar) * text2Length)
+				ActiveBasic.Strings.ChrCopy(.Chars, text1, text1Length As SIZE_T)
+				ActiveBasic.Strings.ChrCopy(VarPtr(.Chars[text1Length]), text2, text2Length As SIZE_T)
 				.Chars[text1Length + text2Length] = 0
 			End With
@@ -324,6 +292,7 @@
 			With Concat
 				Dim lenW = MultiByteToWideChar(CP_THREAD_ACP, 0, text, len, 0, 0)
+				Concat = New String
 				.AllocStringBuffer(m_Length + lenW)
-				memcpy(.Chars, This.Chars, m_Length)
+				ActiveBasic.Strings.ChrCopy(.Chars, This.Chars, m_Length)
 				MultiByteToWideChar(CP_THREAD_ACP, 0, text, len, VarPtr(.Chars[m_Length]), lenW)
 				.Chars[m_Length + lenW] = 0
@@ -335,7 +304,8 @@
 #ifdef __STRING_IS_NOT_UNICODE
 			With Concat
+				Concat = New String
 				Dim lenA = WideCharToMultiByte(CP_THREAD_ACP, 0, text, len, 0, 0, 0, 0)
 				.AllocStringBuffer(m_Length + lenA)
-				memcpy(.Chars, This.Chars, m_Length)
+				ActiveBasic.Strings.ChrCopy(.Chars, This.Chars, m_Length As SIZE_T)
 				WideCharToMultiByte(CP_THREAD_ACP, 0, text, len, VarPtr(.Chars[m_Length]), lenA, 0, 0)
 				.Chars[m_Length + lenA] = 0
@@ -358,27 +328,48 @@
 		End Function
 
-		Const Function Contains(objString As String) As Boolean
-			Return IndexOf(objString, 0, m_Length) >= 0
-		End Function
-
-		Const Function Contains(lpszText As *StrChar) As Boolean
-			Return IndexOf(lpszText, 0, m_Length) >= 0
-		End Function
-
-		Const Function IndexOf(lpszText As *StrChar) As Long
-			Return IndexOf(lpszText, 0, m_Length)
-		End Function
-
-		Const Function IndexOf(lpszText As *StrChar, startIndex As Long) As Long
-			Return IndexOf(lpszText, startIndex, m_Length - startIndex)
-		End Function
-
-		Const Function IndexOf(lpszText As *StrChar, startIndex As Long, count As Long) As Long
-			Dim length = lstrlen(lpszText)
-
-			If startIndex < 0 Then Return -1
-			If count < 1 Or count + startIndex > m_Length Then Return -1
-			If length > m_Length Then Return -1
-
+		Const Function Contains(s As String) As Boolean
+			If Object.ReferenceEquals(s, Nothing) Then
+				'Throw New ArgumentNullException
+			End If
+			Return IndexOf(s, 0, m_Length) >= 0
+		End Function
+
+		Const Function IndexOf(c As StrChar) As Long
+			Return indexOfCore(c, 0, m_Length)
+		End Function
+
+		Const Function IndexOf(c As StrChar, start As Long) As Long
+			rangeCheck(start)
+			Return indexOfCore(c, start, m_Length - start)
+		End Function
+
+		Const Function IndexOf(c As StrChar, start As Long, count As Long) As Long
+			rangeCheck(start, count)
+			Return indexOfCore(c, start, count)
+		End Function
+	Private
+		Const Function indexOfCore(c As StrChar, start As Long, count As Long) As Long
+			indexOfCore = ActiveBasic.Strings.ChrFind(VarPtr(Chars[start]), count, c)
+			If indexOfCore <> -1 Then
+				indexOfCore += start
+			End If
+		End Function
+	Public
+		Const Function IndexOf(s As String) As Long
+			Return IndexOf(s, 0, m_Length)
+		End Function
+
+		Const Function IndexOf(s As String, startIndex As Long) As Long
+			Return IndexOf(s, startIndex, m_Length - startIndex)
+		End Function
+
+		Const Function IndexOf(s As String, startIndex As Long, count As Long) As Long
+			rangeCheck(startIndex, count)
+			If Object.ReferenceEquals(s, Nothing) Then
+				'Throw New ArgumentNullException
+				Debug
+			End If
+
+			Dim length = s.Length
 			If length = 0 Then Return startIndex
 
@@ -386,5 +377,5 @@
 			For i = startIndex To startIndex + count - 1
 				For j = 0 To length - 1
-					If Chars[i + j] = lpszText[j] Then
+					If Chars[i + j] = s[j] Then
 						If j = length - 1 Then Return i
 					Else
@@ -396,19 +387,25 @@
 		End Function
 
-		Const Function LastIndexOf(lpszText As *StrChar) As Long
-			Return LastIndexOf(lpszText, m_Length - 1, m_Length)
-		End Function
-
-		Const Function LastIndexOf(lpszText As *StrChar, startIndex As Long) As Long
-			Return LastIndexOf(lpszText As *StrChar, startIndex, startIndex + 1)
-		End Function
-
-		Const Function LastIndexOf(lpszText As *StrChar, startIndex As Long, count As Long) As Long
-			Dim length = lstrlen(lpszText)
-
-			If startIndex < 0 Or startIndex > m_Length - 1 Then Return -1
-			If count < 1 Or count > startIndex + 2 Then Return -1
+		Const Function LastIndexOf(s As String) As Long
+			Return LastIndexOf(s, m_Length - 1, m_Length)
+		End Function
+
+		Const Function LastIndexOf(s As String, startIndex As Long) As Long
+			Return LastIndexOf(s, startIndex, startIndex + 1)
+		End Function
+
+		Const Function LastIndexOf(s As String, startIndex As Long, count As Long) As Long
+			If Object.ReferenceEquals(s, Nothing) Then
+				'Throw New ArgumentNullException
+				Debug
+			End If
+
+			If startIndex < 0 Or startIndex > m_Length - 1 Or _
+				count < 0 Or count > startIndex + 2 Then
+				'Throw New ArgumentOutOfRangeException
+				Debug
+			End If
+			Dim length = s.Length
 			If length > m_Length Then Return -1
-
 			If length = 0 Then Return startIndex
 
@@ -416,5 +413,5 @@
 			For i = startIndex To  startIndex - count + 1 Step -1
 				For j = length - 1 To 0 Step -1
-					If Chars[i + j] = lpszText[j] Then
+					If Chars[i + j] = s[j] Then
 						If j = 0 Then Return i
 					Else
@@ -426,63 +423,37 @@
 		End Function
 
-		Const Function StartsWith(lpszText As *StrChar) As Boolean
-			Return IndexOf(lpszText) = 0
-		End Function
-
-		Const Function EndsWith(lpszText As *StrChar) As Boolean
-			Return LastIndexOf(lpszText) = m_Length - lstrlen(lpszText)
+		Const Function StartsWith(s As String) As Boolean
+			Return IndexOf(s) = 0
+		End Function
+
+		Const Function EndsWith(s As String) As Boolean
+			Return LastIndexOf(s) = m_Length - s.Length
 		End Function
 
 		Const Function Insert(startIndex As Long, text As String) As String
-			Return Insert(startIndex, text.Chars, text.Length)
-		End Function
-
-		Const Function Insert(startIndex As Long, text As *StrChar) As String
-			Return Insert(startIndex, text, lstrlen(text))
-		End Function
-
-		Const Function Insert(startIndex As Long, text As *StrChar, length As Long) As String
-			If startIndex < 0 Or startIndex > m_Length Or length < 0 Then
-				Debug 'ArgumentOutOfRangeException
-
-			End If
-			Insert = New String(m_Length + length)
-			memcpy(Insert.Chars, Chars, SizeOf (StrChar) * startIndex)
-			memcpy(VarPtr(Insert.Chars[startIndex]), text, SizeOf (StrChar) * length)
-			memcpy(VarPtr(Insert.Chars[startIndex + length]), VarPtr(Chars[startIndex]), SizeOf (StrChar) * (m_Length - startIndex + 1))
-		End Function
-
-		Const Function SubString(startIndex As Long) As String
-			Return SubString(startIndex, m_Length - startIndex)
-		End Function
-
-		Const Function SubString(startIndex As Long, length As Long) As String
-			If startIndex < 0 Or length <= 0 Then Return ""
-			If startIndex + length > m_Length Then Return ""
-
-			Dim temp As String
-			temp.AllocStringBuffer(length)
-			memcpy(temp.Chars, VarPtr(Chars[startIndex]), SizeOf (StrChar) * length)
-			temp.Chars[length] = 0
-			Return temp
+			Dim sb = New System.Text.StringBuilder(This)
+			sb.Insert(startIndex, text)
+			Return sb.ToString
+		End Function
+
+		Const Function Substring(startIndex As Long) As String
+			rangeCheck(startIndex)
+			Return Substring(startIndex, m_Length - startIndex)
+		End Function
+
+		Const Function Substring(startIndex As Long, length As Long) As String
+			rangeCheck(startIndex, length)
+			Return New String(Chars, startIndex, length)
 		End Function
 
 		Const Function Remove(startIndex As Long) As String
-			If startIndex < 0 Or startIndex > m_Length Then
-				Debug 'ArgumentOutOfRangeException
-			End If
-
-			Remove = New String(startIndex)
-			memcpy(Remove.Chars, This.Chars, SizeOf (StrChar) * startIndex)
+			rangeCheck(startIndex)
+			Remove = Substring(0, startIndex)
 		End Function
 
 		Const Function Remove(startIndex As Long, count As Long) As String
-			If startIndex < 0 Or count < 0 Or startIndex + count > m_Length Then
-				Debug 'ArgumentOutOfRangeException
-			End If
-
-			Remove = New String(m_Length - count)
-			memcpy(Remove.Chars, This.Chars, SizeOf (StrChar) * startIndex)
-			memcpy(VarPtr(Remove.Chars[startIndex]), VarPtr(This.Chars[startIndex + count]), SizeOf (StrChar) * (m_Length - startIndex - count))
+			Dim sb = New System.Text.StringBuilder(This)
+			sb.Remove(startIndex, count)
+			Remove = sb.ToString
 		End Function
 
@@ -497,75 +468,49 @@
 
 		Const Function Replace(oldChar As StrChar, newChar As StrChar) As String
-			Replace = Copy(This)
-			With Replace
-				Dim i As Long
-				For i = 0 To ELM(.m_Length)
-					If .Chars[i] = oldChar Then
-						.Chars[i] = newChar
-					End If
-				Next
-			End With
-		End Function
-
-		Const Function Replace(ByRef oldStr As String, ByRef newStr As String) As String
-	'		If oldStr = Nothing Then Throw ArgumentNullException
-	'
-	'		If newStr = Nothing Then
-	'			Return ReplaceCore(oldStr, oldStr.m_Length, "", 0)
-	'		Else
-				Return ReplaceCore(oldStr, oldStr.m_Length, newStr, newStr.m_Length)
-	'		End If
-		End Function
-
-		Const Function Replace(oldStr As *StrChar, newStr As *StrChar) As String
-			If oldStr = 0 Then Debug 'Throw ArgumentNullException
-			If newStr = 0 Then newStr = ""
-			Return ReplaceCore(oldStr, lstrlen(oldStr), newStr, lstrlen(newStr)) 
-		End Function
-
-		Const Function Replace(oldStr As *StrChar, oldLen As Long, newStr As *StrChar, newLen As Long) As String
-			If oldStr = 0 Then Debug 'Throw ArgumentNullException
-			If newStr = 0 Then
-				newStr = ""
-				newLen = 0
-			End If
-			Return ReplaceCore(oldStr, oldLen, newStr, newLen)
+			Dim sb = New System.Text.StringBuilder(This)
+			sb.Replace(oldChar, newChar)
+			Replace = sb.ToString
+		End Function
+
+		Const Function Replace(oldStr As String, newStr As String) As String
+			Dim sb = New System.Text.StringBuilder(This)
+			sb.Replace(oldStr, newStr)
+			Return sb.ToString
 		End Function
 
 		Const Function ToLower() As String
-			ToLower.ReSize(m_Length)
+			Dim sb = New System.Text.StringBuilder(m_Length)
+			sb.Length = m_Length
 			Dim i As Long
 			For i = 0 To ELM(m_Length)
-				ToLower.Chars[i] = _System_ASCII_ToLower(Chars[i])
+				sb[i] = _System_ASCII_ToLower(Chars[i])
 			Next
+			Return sb.ToString
 		End Function
 
 		Const Function ToUpper() As String
-			ToUpper.ReSize(m_Length)
+			Dim sb = New System.Text.StringBuilder(m_Length)
+			sb.Length = m_Length
 			Dim i As Long
 			For i = 0 To ELM(m_Length)
-				ToUpper.Chars[i] = _System_ASCII_ToUpper(Chars[i])
+				sb[i] = _System_ASCII_ToUpper(Chars[i])
 			Next
-		End Function
-	/*
-		Sub Swap(ByRef x As String)
-			Dim tempLen As Long
-			Dim tempChars As *StrChar
-			tempLen = x.m_Length
-			tempChars = x.Chars
-			x.m_Length = This.m_Length
-			x.Chars = This.Chars
-			This.m_Length = tempLen
-			This.Chars = tempChars
-		End Sub
-	*/
+			Return sb.ToString
+		End Function
+
 		Override Function ToString() As String
-			Return This
-		End Function
-
+			ToString = This
+		End Function
+
+		Const Function Clone() As String
+			Clone = This
+		End Function
 		Static Function Copy(s As String) As String
-			Copy.ReSize(s.m_Length)
-			memcpy(Copy.Chars, This.Chars, SizeOf (StrChar) * m_Length)
-		End Function
+			Copy = New String(s.Chars, s.m_Length)
+		End Function
+
+		Sub CopyTo(sourceIndex As Long, destination As *StrChar, destinationIndex As Long, count As Long)
+			ActiveBasic.Strings.ChrCopy(VarPtr(destination[destinationIndex]), VarPtr(Chars[sourceIndex]), count As SIZE_T)
+		End Sub
 
 		Override Function GetHashCode() As Long
@@ -577,47 +522,66 @@
 			Return _System_GetHashFromWordArray(Chars As *Word, size)
 		End Function
+
+		Function PadLeft(total As Long) As String
+			PadLeft(total, &h30 As StrChar)
+		End Function
+
+		Function PadLeft(total As Long, c As StrChar) As String
+			If total < 0 Then
+				'Throw New ArgumentException
+			End If
+			If total >= m_Length Then
+				Return This
+			End If
+			Dim sb = New System.Text.StringBuilder(total)
+			sb.Append(c, total - m_Length)
+			sb.Append(This)
+			Return sb.ToString
+		End Function
+
+		Function PadRight(total As Long) As String
+			PadLeft(total, &h30 As StrChar)
+		End Function
+
+		Function PadRight(total As Long, c As StrChar) As String
+			If total < 0 Then
+				'Throw New ArgumentException
+			End If
+			If total >= m_Length Then
+				Return This
+			End If
+			Dim sb = New System.Text.StringBuilder(total)
+			sb.Append(This)
+			sb.Append(c, total - m_Length)
+			Return sb.ToString
+		End Function
 	Private
-		' メモリ確保に失敗すると元の文字列は失われない。（例外安全でいう強い保障）
 		Function AllocStringBuffer(textLength As Long) As *StrChar
 			If textLength < 0 Then
 				Return 0
-			ElseIf textLength > m_Length or Chars = 0 Then
-				AllocStringBuffer = _System_realloc(Chars, SizeOf(StrChar) * (textLength + 1))
-				If AllocStringBuffer <> 0 Then
-					m_Length = textLength
-					Chars = AllocStringBuffer
-				End If
-			Else
-				m_Length = textLength
-				AllocStringBuffer = Chars
-			End If
-		End Function
-
-		Function ReplaceCore(oldStr As *StrChar, oldLen As Long, newStr As *StrChar, newLen As Long) As String
-			If oldLen = 0 Then
-				Debug 'Throw ArgumentException
-			End If
-			Dim tmp As String
-			With tmp
-				Dim current = 0 As Long
-				Do
-					Dim pos = IndexOf(oldStr, current)
-					If pos = -1 Then
-						Exit Do
-					End If
-					.Append(VarPtr(Chars[current]), pos - current)
-					.Append(newStr, newLen)
-					current = pos + oldLen
-				Loop
-				.Append(VarPtr(Chars[current]), m_Length - current)
-			End With
-			Return tmp
+			End If
+			AllocStringBuffer = GC_malloc_atomic(SizeOf(StrChar) * (textLength + 1))
+			If AllocStringBuffer = 0 Then
+				'Throw New OutOfMemoryException
+			End If
+			m_Length = textLength
+			Chars = AllocStringBuffer
 		End Function
 
 		Sub AssignFromStrChar(text As *StrChar, textLength As Long)
-			If text = Chars Then Exit Sub
-			If AllocStringBuffer(textLength) <> 0 Then
-				memcpy(Chars, text, SizeOf (StrChar) * textLength)
-				Chars[m_Length] = 0
+			AllocStringBuffer(textLength)
+			ActiveBasic.Strings.ChrCopy(Chars, text, textLength As SIZE_T)
+			Chars[m_Length] = 0
+		End Sub
+
+		Const Sub rangeCheck(index As Long)
+			If index < 0 Or index > m_Length Then
+				Debug 'ArgumentOutOfRangeException
+			End If
+		End Sub
+		
+		Const Sub rangeCheck(start As Long, length As Long)
+			If start < 0 Or start > This.m_Length Or length < 0 Then
+				Debug 'ArgumentOutOfRangeException
 			End If
 		End Sub
