Skip to main content

Posts

Showing posts with the label stringbuilder

c# - How to convert a StringBuilder characters to uppercase

Convert StringBuilder characters to uppercase The StringBuilder class represents a mutable String of characters. The StringBuilder class cannot be inherited. The StringBuilder class represents a String-like object whose value is a mutable sequence of characters. Although StringBuilder and String both represent sequences of characters, they are implemented differently. The String is an immutable type. So, each operation that appears to modify a String object actually creates a new String. The following .net c# tutorial code demonstrates how we can convert StringBuilder instance characters to uppercase. So, in this .net c# tutorial code we will convert all of the characters of a StringBuilder object to uppercase characters. Such as if a StringBuilder content is ‘Red’ then we will convert it into uppercase characters as ‘RED’. There are no direct methods in the StringBuilder class to convert StringBuilder characters to uppercase characters...

c# - How to trim a StringBuilder

Trim a StringBuilder The StringBuilder class represents a mutable String of characters. The StringBuilder class cannot be inherited. The StringBuilder class represents a String-like object whose value is a mutable sequence of characters. The following .net c# tutorial code demonstrates how we can trim a StringBuilder instance. In this .net c# tutorial code we will remove all the white spaces from starting and ending positions of a StringBuilder instance. But there is no direct method in the StringBuilder class to trim an instance. So, at first, we have to convert a StringBuilder instance to a String object then we will call String Trim() method to trim the String object. Next, we will clear the StringBuilder instance and append the trimmed String object to the StringBuilder instance. The StringBuilder ToString() method converts the value of a StringBuilder to a String. The StringBuilder ToString() method returns a string whose value is the...

c# - How to reverse a StringBuilder

Reverse a StringBuilder content The StringBuilder class represents a mutable String of characters. The StringBuilder class cannot be inherited. The StringBuilder class represents a String-like object whose value is a mutable sequence of characters. Although StringBuilder and String both represent sequences of characters, they are implemented differently. The String is an immutable type. So, each operation that appears to modify a String object actually creates a new String. The following .net c# tutorial code demonstrates how we can reverse a StringBuilder instance. So, in this .net c# tutorial code we will reverse the characters sequence of a StringBuilder object. Such as if a StringBuilder content is ‘red’ then we will reverse its content as ‘der’. There are no direct methods in the StringBuilder class to reverse the contents sequence of a StringBuilder instance. So at first, we have to convert the StringBuilder object to a String object...

c# - How to apply padding on a StringBuilder

c# example - stringbuilder padding padleft padright The following asp.net c# example code demonstrate us how can we apply padding on StringBuilder value programmatically at run time in an asp.netapplication. In .net framework's StringBuilder Class represent a mutable string of characters. StringBuilder Class has no built in method or property to apply padding on it's text. But String Class has twobuilt in methods to padding its content. We can use StringBuilder.ToString() methodto directly convert a StringBuilder value to a System.String object. String Class has two methods two padding it's content, those are PadLeft() and PadRight(). Both methods has overload. String Class PaLeft(Int32, Char) overloaded method return a new string that right-aligns the characters in this instance by padding them onthe left with a specified Unicode character, for a specified total length. So, we can specify number of times the specified cha...

c# - How to get maximum capacity of a StringBuilder

Get a StringBuilder maximum capacity The StringBuilder class represents a mutable String of characters. The StringBuilder class cannot be inherited. The StringBuilder class represents a String-like object whose value is a mutable sequence of characters. The following .net c# tutorial code demonstrates how we can get the maximum capacity of a StringBuilder instance. In this .net c# tutorial code we will also get the length and capacity of a StringBuilder instance. Here we will use StringBuilder Length, Capacity, and MaxCapacity properties. The StringBuilder Length property gets or sets the length of the current StringBuilder object. The StringBuilder Capacity property gets or sets the maximum number of characters that can be contained in the memory allocated by the current instance. The StringBuilder class MaxCapacity property gets the maximum capacity of this instance. The StringBuilder class MaxCapacity property value is an Int32 which is...

c# - How to convert a StringBuilder to a string array

c# example - convert stringbuilder to string array The following asp.net c# example code demonstrate us how can we convert a StringBuilder objectto a String Array programmatically at run time in an asp.net application. .Net framework's StringBuilder Classrepresent a mutable string of characters. StringBuilder class has no built in method or property to convert a StringBuilder object to a String Array object.We also cannot directly convert a StringBuilder object to a String Array object. To convert a StringBuilder object to a String Array, at first we need to convert the StringBuilder object to a String object.StringBuilder.ToString() method allow us to convert the value of a StringBuilder object to a string which data type isSystem.String. String Class String.Split(Char[]) overloaded method return a String Array that contains the substrings in this instancethat are delimited by elements of a specified Unicode character array. Stri...

c# - How to convert a StringBuilder to a byte array

c# example - convert stringbuilder to byte array The following asp.net c# example code demonstrate us how can we convert a StringBuilder object to a bytearray programmatically at run time in an asp.net application. .Net framework's StringBuilder Class represent amutable string of characters. StringBuilder Class has no built in property or method to convert itself to a byte array. But we can convert a String object to a byte array, so at first we need to convert a StringBuilder object's value to aString object. StringBuilder.ToString() method convert the value of this instance to a string. .Net framework's ASCIIEncoding Class represents an ASCII character encoding of Unicode characters. ASCIIEncoding ClassGetBytes(Char[]) overloaded method allow us to encode all the characters in the specified character array into a sequenceof bytes. This method require to pass a parameter named 'chars' which value type is System.Char...

c# - How to check if a StringBuilder is empty

c# example - stringbuilder check if empty The following ASP.NET C# example code demonstrate us how can we determine whether a Stringbuilder objectis empty or contain any characters. .Net framework's StringBuilder Class represent a mutable string of characters.StringBuilder class cannot be inherited. StringBuilder and String both objects represent sequences of characters, but they are implemented differently. String is animmutable type and StringBuilder is a mutable type. So, each operation that appears to modify a String object actuallycreate and return a new String but each operation to modify a StringBuilder object return the same StringBuilder objectas modified. .Net framework's StringBuilder.Length property allow us to get the length of the current Stringbuilder object. This propertyreturn a System.Int32 value. Returned integer value represent the length of StringBuilder instance. So, we can check whether a StringBuilder obj...

c# - How to get a comma separated list from a StringBuilder

Comma separated list from a StringBuilder The StringBuilder class represents a mutable String of characters. The StringBuilder class cannot be inherited. The StringBuilder class represents a String-like object whose value is a mutable sequence of characters. The default capacity of a StringBuilder object is 16 characters and its default maximum capacity is Int32.MaxValue. Although StringBuilder and String both represent sequences of characters, they are implemented differently. The String is an immutable type. So, each operation that appears to modify a String object actually creates a new String. The following .net c# tutorial code demonstrates how we can get a comma-separated list from a StringBuilder instance. So, in this .net c# tutorial code we will create a comma-separated list from StringBuilder contents. Here we will replace the empty spaces of a StringBuilder content with commas, so it will display as a comma-separated list. We...

c# - How to replace a string in a StringBuilder

Replace a String in a StringBuilder The StringBuilder class represents a mutable String of characters. The StringBuilder class cannot be inherited. The StringBuilder class represents a String-like object whose value is a mutable sequence of characters. The following .net c# tutorial code demonstrates how we can replace a String with another specified String in a StringBuilder instance. That means we will replace all occurrences of a String with another specified String in the StringBuilder object. In this .net c# tutorial code we will use the StringBuilder class Replace() method to replace a String with another specified String in a StringBuilder object. The StringBuilder Replace() method replaces all occurrences of a specified character or String in this instance with another specified character or String. The StringBuilder Replace(String, String) method overload replaces all occurrences of a specified String in this instance with another speci...

c# - How to clear contents of a StringBuilder

Clear a StringBuilder contents The StringBuilder class represents a mutable String of characters. The StringBuilder class cannot be inherited. The StringBuilder class represents a String-like object whose value is a mutable sequence of characters. The following .net c# tutorial code demonstrates how we can clear the contents of a StringBuilder instance. That means how we will remove/delete all the characters of a StringBuilder object. In this .net c# tutorial code we will use the StringBuilder class Clear() method. The StringBuilder Clear() method removes all characters from the current StringBuilder instance. This Clear() method returns a StringBuilder object whose length is zero. So finally, using the StringBuilder class Clear() method the .net c# developers can clear/delete StringBuilder instance contents. stringbuilder-clear-contents.aspx <%@ Page Language="C#" AutoEventWireup="true"%> <!DOCTYPE html...

c# - How to loop through a StringBuilder elements

Loop through a StringBuilder elements The StringBuilder class represents a mutable String of characters. The StringBuilder class cannot be inherited. The StringBuilder class represents a String-like object whose value is a mutable sequence of characters. Although StringBuilder and String both represent sequences of characters, they are implemented differently. The String is an immutable type. So, each operation that appears to modify a String object actually creates a new String. The following .net c# tutorial code demonstrates how we can loop through a StringBuilder object’s elements. So, in this .net c# tutorial code we will iterate all of the elements of a StringBuilder instance. Here we will count elements of a StringBuilder object those are separated by white space. That means we will loop through the words or characters that are separated with white space in a StringBuilder instance. There are no direct methods in the StringBuilde...

c# - How to convert a StringBuilder to a string

Convert a StringBuilder to a String The StringBuilder class represents a mutable String of characters. The StringBuilder class cannot be inherited. The StringBuilder class represents a String-like object whose value is a mutable sequence of characters. Although StringBuilder and String both represent sequences of characters, they are implemented differently. The String is an immutable type. So, each operation that appears to modify a String object actually creates a new String. The following .net c# tutorial code demonstrates how we can convert a StringBuilder instance to a String object. In this .net c# tutorial code we will convert a StringBuilder instance to a String object by using the StringBuilder ToString() method. The StringBuilder ToString() method converts the value of a StringBuilder to a String. The StringBuilder ToString() method returns a String whose value is the same as this instance. convert-stringbuilder-to-string.asp...

c# - How to insert a string into a StringBuilder

Insert a String into a StringBuilder The StringBuilder class represents a mutable String of characters. The StringBuilder class cannot be inherited. The StringBuilder class represents a String-like object whose value is a mutable sequence of characters. The following .net c# tutorial code demonstrates how we can insert a String into a StringBuilder instance. That means we will insert a String into a StringBuilder object’s specified index position. In this .net c# tutorial code we will use the StringBuilder class Insert() method to insert a String to a specified index position of a StringBuilder object. The StringBuilder Insert() method inserts the String representation of a specified object into this instance at a specified character position. The StringBuilder Insert(Int32, String) method overload inserts a String into this instance at the specified character position. So, using this method we can insert a String at the specified index of a Str...

c# - How to get the capacity of a StringBuilder

Get the capacity of a StringBuilder The StringBuilder class represents a mutable String of characters. The StringBuilder class cannot be inherited. The StringBuilder class represents a String-like object whose value is a mutable sequence of characters. The following .net c# tutorial code demonstrates how we can get the capacity of a StringBuilder instance. In this .net c# tutorial code we will get the capacity of a StringBuilder instance in various stages. Such as we get the capacity of a StringBuilder instance initially after appending some text, then again we append some text on it and get the current capacity of the StringBuilder instance. Again we append some text on the StringBuilder instance and get the current capacity of the StringBuilder instance after appending the text. We also get the StringBuilder instance length simultaneously with capacity. Here we will use the StringBuilder class Capacity, and Length properties. The StringBuilder Length pr...

c# - How to create bold text inside a StringBuilder

c# example - stringbuilder bold text The following asp.net c# example code demonstrate us how can we render a part of text to bold characters insidea StringBuilder object programmatically at run time in an asp.net application. .Net framework's StringBuilder Class represent a mutable string of characters. Each operation that appearsto modify a StringBuilder object does not create a new StringBuilder object, itjust return itself as modified StringBuilder object. .Net framework's StringBuilder Class has no built in method or property to render part of text/characters are boldinside a StringBuilder object. So, we can technically render bold text inside a StringBuilder sequence of characters. StringBuilder Class StringBuilder.Append(String) overloaded method allow us to append a copy of the specified stringto this StringBuilder instance. We can populate a StringBuilder object by characters using this Append() method. To rendera part o...

c# - How to find a substring in a StringBuilder

c# example - find a substring in stringbuilder The following asp.net c# example code demonstrate us how can we find a substring within a StringBuilder objectprogrammatically at run time in an asp.net application. We can explain the tutorial as 'how can we determine aStringBuilder object contains a specified substring'. .Net framework's StringBuilder Class represent a mutable string of characters. StringBuilder Class does not have any methodor property to find a substring from it. So, we need to take help from .net framework's String Class. We can convert aStringBuilder object's value to a System.String object by using StringBuilder.ToString() method. String Class String.Contains() method return a value indicating whether a specified substring occurs within this string.String.Contains() method require to pass a parameter named 'value' which value data type is Sysetm.String and representthe substring to seek/find/sea...

c# - How to check if StringBuilder contains a substring

Check StringBuilder contains a specified substring The StringBuilder class represents a mutable String of characters. The StringBuilder class cannot be inherited. The StringBuilder class represents a String-like object whose value is a mutable sequence of characters. The default capacity of a StringBuilder object is 16 characters and its default maximum capacity is Int32.MaxValue. Although StringBuilder and String both represent sequences of characters, they are implemented differently. The String is an immutable type. So, each operation that appears to modify a String object actually creates a new String. The following .net c# tutorial code demonstrates how we can check whether a StringBuilder object contains a specified substring within it. So, in this .net c# tutorial code we will determine whether a StringBuilder instance contains a specified substring or not. There are no direct methods in the StringBuilder class to check whether a...

c# - How to get the length of a StringBuilder

Get the length of a StringBuilder The StringBuilder class represents a mutable String of characters. The StringBuilder class cannot be inherited. The StringBuilder class represents a String-like object whose value is a mutable sequence of characters. The following .net c# tutorial code demonstrates how we can get the length of a StringBuilder instance. The StringBuilder instance length means how many characters contains within the StringBuilder instance. In this .net c# tutorial code we will get the length of a StringBuilder instance using the StringBuilder class Length property. The StringBuilder Length property gets or sets the length of the current StringBuilder object. The StringBuilder Length property value is an Int32 which is the length of this instance. So finally, using the StringBuilder class length property .net developers can get the length of a StringBuilder instance. stringbuilder-length.aspx <%@ Page Language="C#...

c# - How to prepend a string to a StringBuilder

c# example - stringbuilder prepend The following asp.net c# example code demonstrate us how can we prepend a character/text/string/substringto a StringBuilder object programmatically at run time in an asp.net application. .Net framework's StringBuilder Classrepresent a mutable string of characters. StringBuilder Class has an Append() method to append text to it but there is noPrepend method or property to prepend content to a StringBuilder object. Append indicate content will be add at the end of an object and Prepend indicate the content will be add at thebeginning of the specified object. StringBuilder.Append(String) overloaded method allow us to append a copy of the specified string to this instance.StringBuilder Class has no Prepend method. But we can technically prepend text/substring to a StringBuilder object. Prepend indicate that the new substring will be add at the beginning of StringBuilder object, so we can simply use ...