Server Side Programming Articles

Page 807 of 2109

Replace a string using StringBuilder

Samual Sam
Samual Sam
Updated on 17-Mar-2026 890 Views

The StringBuilder class in C# provides an efficient way to modify strings without creating new string objects. The Replace() method allows you to replace all occurrences of a specified string or character with another string or character directly within the StringBuilder object. Syntax Following is the syntax for the Replace() method in StringBuilder − public StringBuilder Replace(string oldValue, string newValue) public StringBuilder Replace(char oldChar, char newChar) public StringBuilder Replace(string oldValue, string newValue, int startIndex, int count) Parameters oldValue/oldChar − The string or character to be replaced. newValue/newChar − The string or character ...

Read More

Compare the content of two StringBuilders

karthikeya Boyini
karthikeya Boyini
Updated on 17-Mar-2026 2K+ Views

The Equals method in C# is used to compare the content of two StringBuilder objects. This method performs a character-by-character comparison of the text content stored in both StringBuilder instances. Syntax Following is the syntax for comparing two StringBuilder objects − bool result = stringBuilder1.Equals(stringBuilder2); Parameters The Equals method takes one parameter − sb − The StringBuilder object to compare with the current instance. Return Value The method returns a bool value − true if both StringBuilder objects have the same content. ...

Read More

Iterating C# StringBuilder in a foreach loop

Samual Sam
Samual Sam
Updated on 17-Mar-2026 2K+ Views

The StringBuilder class in C# is used for efficient string manipulation when dealing with multiple string operations. While StringBuilder itself is not directly iterable in a foreach loop, you can use foreach loops to iterate over collections and append elements to a StringBuilder. Syntax Following is the syntax for using foreach with StringBuilder − StringBuilder sb = new StringBuilder(); foreach (type item in collection) { sb.Append(item); } Using foreach to Build StringBuilder from Array You can iterate through a string array and append each element to a StringBuilder − ...

Read More

Clear a StringBuilder in C#

karthikeya Boyini
karthikeya Boyini
Updated on 17-Mar-2026 2K+ Views

The Clear() method in C# is used to remove all characters from a StringBuilder object, effectively resetting it to an empty state. This method is efficient as it doesn't create a new object but simply resets the internal character buffer. Syntax Following is the syntax for the Clear() method − stringBuilder.Clear(); Return Value The Clear() method returns a reference to the same StringBuilder instance with all characters removed. This allows for method chaining. Using StringBuilder Clear() Method Example The following example demonstrates how to clear a StringBuilder and check its ...

Read More

Access a character in C# StringBuilder

Samual Sam
Samual Sam
Updated on 17-Mar-2026 488 Views

A StringBuilder in C# allows you to access individual characters using the indexer syntax, similar to accessing elements in an array. The indexer provides both read and write access to characters at specific positions within the StringBuilder. Syntax Following is the syntax for accessing a character in StringBuilder − char character = stringBuilder[index]; Following is the syntax for modifying a character in StringBuilder − stringBuilder[index] = 'newCharacter'; Parameters index − The zero-based position of the character to access or modify. Return Value Returns the character ...

Read More

C# Program to change a character from a string

karthikeya Boyini
karthikeya Boyini
Updated on 17-Mar-2026 364 Views

In C#, strings are immutable, meaning their characters cannot be changed directly. However, you can change characters in a string using the StringBuilder class, which provides a mutable sequence of characters. The StringBuilder class allows you to modify individual characters using indexer notation str[index] = newChar, where the index is zero-based. Syntax Following is the syntax for changing a character in a StringBuilder − StringBuilder stringBuilder = new StringBuilder("original"); stringBuilder[index] = 'newChar'; Using StringBuilder to Change Characters Let's say our string is − StringBuilder str = new StringBuilder(); str.Append("pre"); ...

Read More

C# program to remove characters starting at a particular index in StringBuilder

Samual Sam
Samual Sam
Updated on 17-Mar-2026 185 Views

The StringBuilder class in C# provides the Remove() method to delete a sequence of characters starting from a specific index position. This method is more efficient than string manipulation when performing multiple character operations. Syntax Following is the syntax for the Remove() method − StringBuilder.Remove(int startIndex, int length) Parameters startIndex − The zero-based position where removal begins. length − The number of characters to remove. Return Value The method returns a reference to the same StringBuilder instance after the removal operation, allowing for method chaining. ...

Read More

Declare char arrays in C#

Samual Sam
Samual Sam
Updated on 17-Mar-2026 7K+ Views

A char array in C# is used to store a sequence of characters. You can declare and initialize char arrays in several ways, depending on whether you know the values at compile time or need to set them dynamically. Syntax Following are the different ways to declare a char array − // Declaration with size char[] arr = new char[size]; // Declaration with initialization char[] arr = {'a', 'b', 'c'}; // Alternative initialization syntax char[] arr = new char[] {'a', 'b', 'c'}; Declaring and Setting Elements Individually You can declare a ...

Read More

CompareTo() method in C#

karthikeya Boyini
karthikeya Boyini
Updated on 17-Mar-2026 1K+ Views

The CompareTo() method in C# is used to compare two values and returns an integer that indicates their relative position in the sort order. This method is part of the IComparable interface and is available for most built-in data types like integers, strings, and dates. Syntax Following is the syntax for using the CompareTo() method − int result = value1.CompareTo(value2); Return Value The CompareTo() method returns the following integer values − 0 = both values are equal Positive integer (typically 1) = the calling value is greater than the parameter Negative ...

Read More

StreamWriter in C#

Samual Sam
Samual Sam
Updated on 17-Mar-2026 282 Views

The StreamWriter class in C# is used to write characters to a stream in a particular encoding. It provides an easy way to create and write text to files, making it essential for file I/O operations. StreamWriter automatically handles file creation and provides various methods to write text data efficiently. Syntax Following is the syntax for creating a StreamWriter object − StreamWriter writer = new StreamWriter("filename.txt"); Using the using statement ensures automatic disposal − using (StreamWriter writer = new StreamWriter("filename.txt")) { writer.WriteLine("text"); } Basic File Writing ...

Read More
Showing 8061–8070 of 21,090 articles
« Prev 1 805 806 807 808 809 2109 Next »
Advertisements