Skip to main content

Posts

Showing posts with the label string

c# - How to convert a string to an int array

Convert a String to an Int Array The String represents text as a sequence of UTF-16 code units. The String is a sequential collection of characters that is used to represent text. The String is a sequential collection of System.Char objects. The following .net c# tutorial code demonstrates how we can convert a String object to an Int Array. In this .net c# tutorial code we will initialize a String instance in which content is int values separated by commas. We will split the String object into a String Array then we will create an Int array from this String Array. The String Split(Char[], StringSplitOptions) method splits a String into substrings based on specified delimiting characters and String split options. Here we will remove empty entries while we split the String instance. Now, we will call Enumerable Select() method. The Enumerable Select() method projects each element of a sequence into a new form. Using this method, we convert...

c# - How to convert a string to an IP address

Convert a String to an IP address The String represents text as a sequence of UTF-16 code units. The String is a sequential collection of characters that is used to represent text. The String is a sequential collection of System.Char objects. The following .net c# tutorial code demonstrates how we can convert a String object to an IP address. The IPAddress class provides an Internet Protocol (IP) address. IPAddress Parse(String) method overload converts an IP address String to an IPAddress instance. The IPAddress Parse(string ipString) has a required parameter named ‘ipString’. The ‘ipString’ is a String that contains an IP address in the dotted-quad notation for IPv4 and in the colon-hexadecimal notation for IPv6. The IPAddress Parse() method returns an IPAddress instance. The IpAddress Parse(ipString string) method throws ArgumentNullException if the ‘ipString’ is null. It also throws FormatException if the ‘ipString’ is not a valid ID ...

c# - How to convert a string to an int list

Convert a String into an int List The String represents text as a sequence of UTF-16 code units. The String is a sequential collection of characters that is used to represent text. The String is a sequential collection of System.Char objects. The following .net c# tutorial code demonstrates how we can convert a String instance into an int List object. So, in this .net c# tutorial code we will split a String instance into a String Array then we will convert the String Array into an int data type List object. Here we will split a String whose contents are numbers and they are separated by the comma delimiter. The String Split(Char[], StringSplitOptions) method splits a String into substrings based on specified delimiting characters and String split options. In this example .net c# code, we removed the empty entries using the String split options StringSplitOptions.RemoveEmptyEntries. So, using the Split(Char[], StringSplitOptions) method we can sp...

c# - How to get bytes from a string

Get bytes from a String The String represents text as a sequence of UTF-16 code units. The String is a sequential collection of characters that is used to represent text. The String is a sequential collection of System.Char objects. The following .net c# tutorial code demonstrates how we can get bytes from a String instance. Here we will use ASCII, Default, and UTF8 encoding and Encoding class GetBytes() method. The Encoding class represents the character encoding. Encoding is the process of transforming a set of Unicode characters into a sequence of bytes and decoding is the process of transforming a sequence of encoded bytes into a set of Unicode characters. The Encoding ASCII property gets an encoding for the ASCII (7-bit) character set. The Encoding Default property gets the default encoding for this .NET implementation and the Encoding UTF8 property gets an encoding for the UTF-8 format. The Encoding GetBytes() method when ove...

c# - How to convert a string to a list

Convert a String into a List The String represents text as a sequence of UTF-16 code units. The String is a sequential collection of characters that is used to represent text. The String is a sequential collection of System.Char objects. The following .net c# tutorial code demonstrates how we can convert a String instance into a List object. So, in this .net c# tutorial code we will split a String instance into a String Array then we will convert the String Array into a List object. Here we will split a String by a comma delimiter. The String Split(Char[], StringSplitOptions) method splits a String into substrings based on specified delimiting characters and String split options. In this example .net c# code, we removed the empty entries using the String split options StringSplitOptions.RemoveEmptyEntries. So, using the Split(Char[], StringSplitOptions) method we can split a String into a String Array by a comma delimiter. The String Spl...

c# - How to convert a string to a guid

Convert a String to a Guid The String represents text as a sequence of UTF-16 code units. The String is a sequential collection of characters that is used to represent text. The String is a sequential collection of System.Char objects. The following .net c# tutorial code demonstrates how we can convert a String object to a Guid. The Guid represents a globally unique identifier (GUID). A GUID is a 128-bit integer (16 bytes) that can be used across all computers and networks wherever a unique identifier is required. The Guid(String) constructor initializes a new instance of the Guid structure by using the value represented by the specified String. The Guid(string g) constructor throws ArgumentNullException if the g is null. It throws FormatException if the format of provided String is invalid. The Guid(String) constructor also throws OverflowException if the format of the passed String is invalid. So, using this Guid(String) constructor we can con...

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

Convert a String to a Float Array The String represents text as a sequence of UTF-16 code units. The String is a sequential collection of characters that is used to represent text. The String is a sequential collection of System.Char objects. The following .net c# tutorial code demonstrates how we can convert a String object to a Float Array. In this .net c# tutorial code we will initialize a String instance in which content is float values separated by commas. We will split the String object into a String Array then we will create a Float array from this String Array. The String Split(Char, StringSplitOptions) method splits a String into substrings based on the specified delimiting character and String split options. So, at first, we will split the String instance into a String Array object by using the comma delimiter. Then we will call Array ConvertAll() method to convert the String Array to a Float Array. The Array ConvertAll() metho...

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

Convert a String to a Double Array The String represents text as a sequence of UTF-16 code units. The String is a sequential collection of characters that is used to represent text. The String is a sequential collection of System.Char objects. The following .net c# tutorial code demonstrates how we can convert a String object to a Double Array. In this .net c# tutorial code we will initialize a String instance in which content is double values separated by commas. We will split the String object into a String Array then we will create a Double array from this String Array. The String Split(Char, StringSplitOptions) method splits a String into substrings based on the specified delimiting character and String split options. So, at first, we have to split the String instance into a String Array object by using the comma delimiter. The Array ConvertAll() method allows us to convert the String Array to a Double Array. The Array ConvertAll() m...

c# - How to convert a hex string to color

Convert a Hex String to a Color The String represents text as a sequence of UTF-16 code units. The String is a sequential collection of characters that is used to represent text. The String is a sequential collection of System.Char objects. The following .net c# tutorial code demonstrates how we can convert a Hex String object to a Color. The Color represents an ARGB (alpha, red, green, blue) color. The ColorTranslator class translates color to and from GDI+ Color structures. The ColorTranslator class cannot be inherited. The ColorTranslator FromHtml(String) method translates an HTML color representation to a GDI+ Color structure. The ColorTranslator FromHtml(string htmlColor) method’s htmlColor parameter is a String representation of the HTML color to translate. ColorTranslator FromHtml(string htmlColor) method returns the Color structure that represents the translated HTML color or Empty if the htmlColor color is null. So, using this C...

c# - How to get color from string

Get Color from String The String represents text as a sequence of UTF-16 code units. The String is a sequential collection of characters that is used to represent text. The String is a sequential collection of System.Char objects. The following .net c# tutorial code demonstrates how we can get a Color instance from a String object. So, in this .net c# tutorial code we will convert a String instance that contains a color name into a Color object. The Color represents an ARGB (alpha, red, green, blue) color. The Color FromName() method creates a Color structure from the specified name of a predefined color. The Color FromName(string name) has a required parameter named ‘name’. The Color FromName(string name) method's name parameter is a String that is the name of a predefined color. The valid names are the same as the names of the elements of the KnownColor enumeration. The Color FromName() method returns a Color that this method creates...

c# - How to convert a string to an arraylist

Convert a String to an ArrayList The String represents text as a sequence of UTF-16 code units. The String is a sequential collection of characters that is used to represent text. The String is a sequential collection of System.Char objects. The following .net c# tutorial code demonstrates how we can convert a String object to an ArrayList. In this .net c# tutorial code we will split a String instance into a String Array then we create an ArrayList from the converted String Array. At last, we loop through the ArrayList to display its items on the user interface. The String Split() method returns a String Array that contains the substrings in this instance that are delimited by elements of a specified string or Unicode character array. In this .net c# tutorial we passed the comma ‘,’ delimiter to split the String into a String Array. The String Split(Char[]) method split a string into substrings based on specified delimiting characters. ...

c# - How to convert string to ascii values

Convert String to ASCII values The String represents text as a sequence of UTF-16 code units. The String is a sequential collection of characters that is used to represent text. The String is a sequential collection of System.Char objects. The following .net c# tutorial code demonstrates how we can convert a String instance to ASCII values. Here we will use ASCII encoding and the Encoding class GetBytes() method. The Encoding class represents the character encoding. Encoding is the process of transforming a set of Unicode characters into a sequence of bytes and decoding is the process of transforming a sequence of encoded bytes into a set of Unicode characters. The Encoding ASCII property gets an encoding for the ASCII (7-bit) character set. The Encoding GetBytes() method when overridden in a derived class, encodes a set of characters into a sequence of bytes. The Encoding GetBytes(String) method overload when overridden in a derived cla...

c# - How to trim a string to a specified length

Trim a String to a specified length The String represents text as a sequence of UTF-16 code units. The String is a sequential collection of characters that is used to represent text. The String is a sequential collection of System.Char objects. The following .net c# tutorial code demonstrates how we can trim a String object to a specified length. So, we will truncate a long String to a specified length. We can also add three dots at the end of the truncated string while we trim a long-length String instance. The String Substring() method retrieves a substring from this instance. The String Substring(int startIndex, int length) method retrieves a substring from this instance. The substring starts at a specified character position and has a specified length. So we can pass zero for the start index and a specified number for the length to truncate/trim a String instance. The String Substring(Int32, Int32) method returns a String that is equiv...

c# - How to truncate a string

Truncate a String The String represents text as a sequence of UTF-16 code units. The String is a sequential collection of characters that is used to represent text. The String is a sequential collection of System.Char objects. The following .net c# tutorial code demonstrates how we can truncate a String instance. So, in this .net c# tutorial code we will truncate a long String to a short length String. Here we will use String Substring() method to truncate a long-length String. The String Substring() method retrieves a substring from this instance. The String Substring(Int32, Int32) method overload retrieves a substring from this instance where the substring starts at a specified character position and has a specified length. The String Substring(int startIndex, int length) method overload has two required parameters. The startIndex parameter is the zero-based starting character position of a substring in this instance. And the length pa...

c# - How to remove white spaces from strating of a string

Remove white spaces from starting of a String The String represents text as a sequence of UTF-16 code units. The String is a sequential collection of characters that is used to represent text. The String is a sequential collection of System.Char objects. The following .net c# tutorial code demonstrates how we can remove white spaces from starting of a String instance. So in this .net c# tutorial code, we will delete empty spaces/white spaces from the starting/left side of a String object. Here we will use the String TrimStart() method to remove String beginning white spaces. The String TrimStart() removes all the leading white-space characters from the current instance. The String trimStart() method returns the String that remains after all white-space characters are removed from the start of the current String. If there is no white space in the String instance then the current String instance returns unchanged. So, simply using this ...

c# - How to get number of characters from left side of a string

Get number of characters from String left side The String represents text as a sequence of UTF-16 code units. The String is a sequential collection of characters that is used to represent text. The String is a sequential collection of System.Char objects. The following .net c# tutorial code demonstrates how we can get a number of characters from the left side of a String instance. So, in this .net c# tutorial code we will get a specified number of characters from the beginning of a String object. Here we will use String Substring() method to do this. The String Substring() method retrieves a substring from this instance. The String Substring(Int32, Int32) method overload retrieves a substring from this instance where the substring starts at a specified character position and has a specified length. So using Substring(int startIndex, int length) method overload we can get a specified number of characters from a String left side. To get t...

c# - How to get number of characters from right side of a string

Get number of characters from String right side The String represents text as a sequence of UTF-16 code units. The String is a sequential collection of characters that is used to represent text. The String is a sequential collection of System.Char objects. The following .net c# tutorial code demonstrates how we can get a number of characters from the right side of a String instance. So, in this .net c# tutorial code we will take only a specified number of characters from a String object’s right side. To achieve this, here we create a custom function whose name is ‘right’. This custom function gets a substring from the source String. We get the substring from the source String at the very end position with a specified length. The String SubString(int startIndex) method overload retrieves a substring from this instance where the substring starts at a specified character position and continues to the end of the String. So using Sub...

c# - How to reverse a string

Reverse a String The String represents text as a sequence of UTF-16 code units. The String is a sequential collection of characters that is used to represent text. The String is a sequential collection of System.Char objects. The following .net c# tutorial code demonstrates how we can reverse a String object. So, in this .net c# tutorial code we will reverse String characters such as if a String instance contains the text ‘Yellow’ then we will reverse it as ‘wolleY’. It means we will rearrange the character’s position of a String instance from last to first and so on. To reverse a String object, at first we will convert the String instance into a Char array. Then we reverse the elements of the Char array using the .net Array built-in method. Finally, we will create a new instance of the String object from the reversed Char Array object. The String ToCharArray() method copies the characters in this instance to a Unicode character Array. T...

c# - How to remove special characters from a string

String remove special characters The following asp.net c# example code demonstrate us how can we remove special characters programmatically at run timefrom a string value. We only keep the alphabets (letter) (a to z), numeric characters (0 to 9), white space and punctuation (.,?-;)..Net framework's String Class has no built in method or property to remove special characters from a string object. So, we need toapply few techniques to remove/delete special characters from a string object. First, we need to convert the String value to a char array. String.ToCharArray(Char) overloaded method copies the characters in thisinstance to a Unicode character array. Next, we loop through the char array elements using foreach loop. Now we can check each character of char array within loop to determine whether the current character is special character or not.Char.IsLetterOrDigit(Char) overloaded method indicate whether the specified Unicode character is ...

c# - How to remove non alphanumeric characters from a string

String remove non alphanumeric The following asp.net c# example code demonstrate us how can we remove non alphanumeric charactersprogrammatically at run time from a string object. So, we keep only alphabet (letter) (a to z) and numeric characters(0 to 9). .Net framework's String class has no built in method or property to remove non alphanumeric charactersfrom a string value. To remove non alphanumeric characters from a string object, first we need to convert the string value to a char array.String.ToCharArray() copies the characters in this instance to a Unicode character array. Next, we need to loop through thechar array elements. In this example, we loop char array elements by using foreach loop. Finally, we can test, which character is alphanumeric or not. We can determine a character's alphanumeric stateby using Char.IsLetterOrDigit() method. The Char.IsLetterOrDigit(Char) overloaded method indicate whether a Unicode characteris c...