Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by karthikeya Boyini
Page 5 of 143
Compare the content of two StringBuilders
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 MoreClear a StringBuilder in C#
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 MoreC# General Date Short Time ("g") Format Specifier
The General Date Short Time ("g") format specifier in C# is a standard DateTime format that combines the short date pattern ("d") and short time pattern ("t"), separated by a space. This format provides a compact representation of both date and time information. Syntax Following is the syntax for using the "g" format specifier − dateTime.ToString("g") dateTime.ToString("g", CultureInfo) The format pattern varies based on the current culture or specified culture. It typically displays − Short date − MM/dd/yyyy or dd/MM/yyyy depending on culture Short time − HH:mm or h:mm tt (12-hour with ...
Read MoreC# Program to change a character from a string
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 MoreDecimal to Multiple-Bases Conversion with Stack
Decimal to multiple-base conversion is a common programming task where we convert a decimal number to binary, octal, hexadecimal, or any other base. Using a stack data structure makes this process efficient because stacks follow the Last-In-First-Out (LIFO) principle, which naturally reverses the remainder sequence obtained during division. How It Works The conversion algorithm repeatedly divides the decimal number by the target base and stores remainders in a stack. When we pop elements from the stack, we get the digits in the correct order for the converted number. Decimal to Binary Conversion (45 ...
Read MoreHow to find the Rank of a given Array in C#?
The rank of an array in C# refers to the number of dimensions it has. A one-dimensional array has a rank of 1, a two-dimensional array has a rank of 2, and so on. To find the rank of any array, use the Rank property. Syntax Following is the syntax to get the rank of an array − arrayName.Rank Return Value The Rank property returns an int value representing the number of dimensions in the array. Using Rank Property with Different Array Types Example 1: Single-Dimensional Array using System; ...
Read MoreCompareTo() method in C#
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 MoreC# Program to get the absolute value of the time
To get the absolute value of time, use the TimeSpan.Duration()
Read MoreWhy do we use internal keyword in C#?
The internal keyword in C# is an access modifier that provides assembly-level access. It allows class members to be accessible within the same assembly but restricts access from other assemblies, making it ideal for creating APIs that should only be used internally within your project. Syntax Following is the syntax for declaring internal members − internal class ClassName { // class accessible within assembly only } class MyClass { internal int field; // field accessible within assembly ...
Read MoreGroupBy() Method in C#
The GroupBy() method in C# is a LINQ extension method that groups elements from a collection based on a specified key selector function. It returns an IGrouping where elements sharing the same key are grouped together. Syntax Following is the syntax for the GroupBy() method − public static IEnumerable GroupBy( this IEnumerable source, Func keySelector ) Parameters source − The collection to group elements from. keySelector − A function that extracts the key for each element. Return Value Returns an ...
Read More