public static unsafe string ReplaceEx(this string str, string oldStr, string newStr) { if (oldStr.Length != newStr.Length) { throw new ArgumentOutOfRangeException("oldStr.Length != newStr.Length"); } if (oldStr == null) { throw new ArgumentNullException("oldStr"); } if (oldStr.Length == 0) { throw new ArgumentException("oldStr is the empty string."); } if (str.Length == 0 || oldStr == newStr || oldStr.Length > str.Length) { return(str); } if (oldStr.Length == 1 && newStr.Length == 1) { return(ReplaceEx(str, oldStr[0], newStr[0])); } int length = str.Length; int step = oldStr.Length; fixed(char *dst = str, s = newStr) { for (int i = 0; i < length;) { int found = str.IndexOf(oldStr, i, length - i); if (found < 0) { break; } CString.CharCopy(dst + i, s, newStr.Length); i = found + step; } } return(str); }
public static unsafe void CopyToEx(this string str, int startIndex, string outStr) { if (startIndex < 0) { throw new ArgumentOutOfRangeException("startIndex", "Cannot be negative."); } if (startIndex + outStr.Length > str.Length) { throw new ArgumentOutOfRangeException("startIndex", "Cannot exceed length of string."); } fixed(char *src = str, dest = outStr) { CString.CharCopy(dest, src + startIndex, outStr.Length); } }
public static unsafe string ReplaceEx(this string dest, int offset, string src) { if (offset < 0) { throw new ArgumentOutOfRangeException("offset", "Cannot be negative."); } if (offset >= dest.Length) { throw new ArgumentOutOfRangeException("offset >= dest.Length"); } if (offset + src.Length > dest.Length) { throw new ArgumentOutOfRangeException("offset + src.Length > dest.Length"); } fixed(char *dst = dest, s = src) { CString.CharCopy(dst + offset, s, src.Length); } return(dest); }