System.Text.StringBuilder.Replace C# (CSharp) Method

Replace() public method

public Replace ( string oldValue, string newValue, int startIndex, int count ) : StringBuilder
oldValue string
newValue string
startIndex int
count int
return StringBuilder
		public StringBuilder Replace( string oldValue, string newValue, int startIndex, int count ) 
		{
			if (oldValue == null)
				throw new ArgumentNullException ("The old value cannot be null.");

			if (startIndex < 0 || count < 0 || startIndex > _length - count)
				throw new ArgumentOutOfRangeException ();

			if (oldValue.Length == 0)
				throw new ArgumentException ("The old value cannot be zero length.");

			string substr = _str.Substring(startIndex, count);
			string replace = substr.Replace(oldValue, newValue);
			// return early if no oldValue was found
			if ((object) replace == (object) substr)
				return this;

			InternalEnsureCapacity (replace.Length + (_length - count));

			// shift end part
			if (replace.Length < count)
				String.CharCopy (_str, startIndex + replace.Length, _str, startIndex + count, _length - startIndex  - count);
			else if (replace.Length > count)
				String.CharCopyReverse (_str, startIndex + replace.Length, _str, startIndex + count, _length - startIndex  - count);

			// copy middle part back into _str
			String.CharCopy (_str, startIndex, replace, 0, replace.Length);
			
			_length = replace.Length + (_length - count);

			return this;
		}

Same methods

StringBuilder::Replace ( char oldChar, char newChar ) : StringBuilder
StringBuilder::Replace ( char oldChar, char newChar, int startIndex, int count ) : StringBuilder
StringBuilder::Replace ( string oldValue, string newValue ) : StringBuilder

Usage Example

Beispiel #1
0
        public string Compile(bool minify)
        {
            var js = jslastvalid;
            var html = htmllastvalid;

            // Merge fields
            var output = new StringBuilder(html);
            output.Replace("%resourcepath%", Program.BlobPathResource);
            output.Replace("%build%", Program.Config.Configuration.Build.ToString());
            output.Replace("%buildserial%", Program.Config.Configuration.BuildSerial.ToString());
            html = output.ToString();

            // Minify
            if (minify) {
                js = Minify.MinifyJS(js);
                html = Minify.MinifyHTML(html);
            }

            // Add JS
            var doc = new HtmlDocument();
            doc.LoadHtml(html);
            doc.GetElementbyId("_script_").AppendChild(doc.CreateComment(js));

            return doc.DocumentNode.OuterHtml;
        }
All Usage Examples Of System.Text.StringBuilder::Replace