/// <summary>Returns a new string in which all occurrences (or a specified /// number of occurrences) of a specified string in the current instance /// are replaced with another specified string.</summary> /// <param name="what"></param> /// <param name="replacement"></param> /// <param name="ignoreCase"></param> /// <param name="maxReplacements"></param> /// <returns>Returns a new string with replacements made, or the same /// string if no replacements occurred.</returns> public UString Replace(UString what, UString replacement, bool ignoreCase = false, int maxReplacements = int.MaxValue) { if (maxReplacements <= 0) { return(this); } UString sub = Find(what, ignoreCase); if (sub.IsEmpty) { return(this); } StringBuilder sb = new StringBuilder(_str, _start, sub._start - _start, capacity: _count + replacement._count - what._count); sb.Append(replacement._str, replacement._start, replacement._count); UString self; for (int rep = 1; ; rep++) { self = sub.Substring(what.Length); if (rep >= maxReplacements) { break; } sub = self.Find(what, ignoreCase); sb.Append(self._str, self._start, sub._start - self._start); if (sub.IsEmpty) { return(sb.ToString()); } sb.Append(replacement._str, replacement._start, replacement._count); } sb.Append(self._str, self._start, self._count); return(sb.ToString()); }
/// <summary>Returns a version of the string without the specified suffix. /// If the string does not have the specified suffix, it is returned unchanged. /// The suffix check is case-sensitive.</summary> public static UString WithoutSuffix(this UString s, UString suffix, bool ignoreCase = false) => s.EndsWith(suffix, ignoreCase) ? s.Substring(0, s.Length - suffix.Length) : s;
/// <summary>Returns a version of the string without the specified prefix. /// If the string does not have the specified prefix, it is returned unchanged. /// The prefix check is case-sensitive.</summary> public static UString WithoutPrefix(this UString s, UString prefix, bool ignoreCase = false) => s.StartsWith(prefix, ignoreCase) ? s.Substring(prefix.Length) : s;