Skip to main content

Posts

Showing posts from October, 2013

c# - How to remove characters from a string

Remove characters 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 remove characters from a String instance. So, in this .net c# tutorial code we will remove a specified number of characters from a String object from the specified character position. Here we will use String Remove() method to do this. The String Remove() method returns a new String in which a specified number of characters from the current String are deleted. The String Remove() method has two overloads.The String Remove(int startIndex) returns a new String in which all the characters in the current instance, beginning at a specified position and continuing through the last position, have been deleted. So using this overload we can delete all the ch...

c# - How to find second occurrence of a substring in a string

Find the second occurrence of a substring within 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 find the second occurrence of a substring within a String. In this .net c# tutorial code we will find that a specified substring is exist more than one time inside a String object and we will get the second occurrence index position of the substring. Such as we have a String instance, we have to find a substring ‘Birch’ within it and we also have to show the index position of the ‘Birch’ substring’s second occurrence on the user interface. The substring maybe exists more than two times within the String instance but we have to find the second occurrence index position of the specified substring within the String instance....

c# - How to find all occurrences of a substring within a string

Find all occurrences of a substring within 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 find all occurrences of a substring within a String. In this .net c# tutorial code we will find that a specified substring is how many times exists inside a String object. Such as we have a String instance, we have to find a substring ‘Cherry’ within it and we also have to show all the index positions of the ‘Cherry’ substring found within the String instance. We can do this using a ‘while’ loop. We will loop through all occurrences of the specified substring inside the String instance. The loop will continue until there are no matching available. On the looping time, we also display the substring occurrences index position on ...

c# - How to find first index of a character within a string

Find index of character in string The following asp.net c# example code demonstrate us how can we find index of a specified character in a stringprogrammatically at run time in an asp.net application. .Net framework's String Class represent Text as a series of UnicodeCharacters. String Class String.IndexOf(Char) overloaded method reports the zero-based index of the first occurrence of the specified Unicode characterin this string. String.IndexOf(Char) method has a required parameter named 'value' which data type is System.Char. This parameter valuerepresent a Unicode character to seek. String.IndexOf(Char) method return a System.Int32 data type integer value. This return value represent the zero-based index position ofspecified character, if that character is found; otherwise it return -1. So, we can find index of a character from a string using String.IndexOf(Char) method. We just need to call the method andspecify the strin...

c# - How to find index of a substring/character in a string

Find the index of a substring or character in 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 find the index of a substring or a character in a String object. So, in this .net c# tutorial code we will find the index position of a specified substring or a specified character within a String instance. Here we will find the index of a substring or a character from a String object using the String IndexOf() method. The String IndexOf() method reports the zero-based index of the first occurrence of a specified Unicode character or string within this instance. The String IndexOf() method returns -1 if the character or String is not found in this instance. The String IndexOf(String) method overload reports the zero-ba...

c# - How to check whether a string starts with vowel

Check whether a String starts with a vowel 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 check whether a String instance starts with a vowel. So, in this .net c# tutorial code we will determine whether a String object’s first character is a vowel or not. To check that we have to create a char array of all lowercase vowels. Then we will check whether the first character of the String instance is a vowel or not. The String ToLower() method returns a copy of this String converted to lowercase. So using the ToLower() method we convert the String instance characters to lowercase. Then we create a char array from the lowercase String instance. The String ToCharArray() method copies the characters in this instance to a Unico...

c# - String startswith case insensitive

String startswith case insensitive The following asp.net c# example code demonstrate us how can we determine whether a string starts with a specifiedsubstring (string comparison is ignoring case) programmatically at run time in an asp.net application. .Net framework's String Class String.StartsWith(String, StringComparison) overloaded method can determine whether thebeginning of this string instance matches the specified string when compared using the specified comparison option. String.StartsWith(String, StringComparison) method has two required parameters named 'value' and 'comparisonType'. The 'value' parameterrepresent the string to compare. The 'comparisonType' parameter value type is 'System.StringComparison' which represent one of the enumerationvalues that determines how this string and specified string (value parameter) are compared. System.StringComparison enumeration value 'Ordinal...

c# - How to check whether a string starts with letter

String starts with letter The following asp.net c# example code demonstrate us how can we determine whether a string starts with a letterprogrammaticaly at run time in an asp.net application. .Net framework's String Class represent text as a series of Unicodecharacters. String Class has no direct method or property to determine whether a string begins with a letter (a to z). First, we need to get the first character of a string object to verify it is a letter or not. String.ElementAt()method allow us to get the character at a specified index position from a string. So, we can get the first character of a stringby calling the ElementAt() method as this way String.ElementAt(0), because string object is zero-based index. Char.IsLetter(Char) overloaded method indicate whether the specified Unicode character is categorized as a Unicode letter.So, we can determine the previously collected first character of a string is a letter or not by accessing...

c# - How to check whether a string ends with a number

Check whether a String ends with a number 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 check whether a String ends with a number. In this .net c# tutorial we will determine whether a String’s last character is a number or not. The String ToCharArray() method copies the characters in this instance to a Unicode character array. So, we can convert a String to a char array. The ElementAt() method allows us to get the item from a specified index position. This way, we get the last character of a String instance. The Char IsDigit() method indicates whether a Unicode character is categorized as a decimal digit. We have to pass a Char object to this IsDigit(Char) method to check this provided Char is a digit or not. This met...

c# - String endswith ignorecase

String ends with ignore case 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 determine whether a String instance ends with a specified String while we compare by ignoring the case. So, in this .net c# tutorial code we will check whether a String object ends with a specified String by ignoring the case. Here we will compare it using the String EndsWith() method. The String EndsWith() method determines whether the end of this String instance matches a specified String. The String EndsWith(String, StringComparision) method overload determines whether the end of this String instance matches the specified String when compared using the specified comparison option. The String EndsWith(string value, StringComparision comparisonTy...

c# - How to check whether a string starts with specific substring

Check whether a String starts with a specific substring 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 check whether a String instance starts with a specific substring. In this .net c# tutorial code we will check whether a String object starts with a specific substring or not by using the String StartsWith() method. The String StartsWith(String) method overload determines whether the beginning of this String instance matches the specified String. So using this String StartsWith(string value) method overload .net developers can determine whether a String instance starts with a specific substring or not. The String StartsWith(string value) method overload has a required parameter named value whose data type is a string. ...

c# - How to check whether a string ends with specific substring

Check whether a String ends with a specific substring 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 check whether a String instance ends with a specific substring. In this .net c# tutorial code we will check whether a String object ends with a specific substring or not by using the String EndsWith() method. The String EndsWith() method determines whether the end of this String instance matches a specified String. The String EndsWith(String) method overload determines whether the end of this String instance matches the specified String. So using this String EndsWith(string value) method overload .net developers can determine whether a String instance ends with a specific substring or not. The String EndsWith(string val...

c# - How to escape double quotes in a string

String escape double quote The following asp.net c# example code demonstrate us how can we escape double quote in a string programmaticallyat run time in an asp.net application. .Net framework's String Class represent text as a series of Unicode characters.We can escape double quotes (") in a string by several ways. We can escape double quotes in a string by using a escape character Backslash (\). If we want to include a doublequotes in this way, we should write the string as ("a \"sample\" text"). Here the word (sample) will be surrounded by twodouble quotes as "sample". We also can escape double quotes in a string object by using @ symbol. If we want to write the same string in this technique,then we should format it as (@"a ""sample"" text"). Here we also need to place a double quotes two times to get output a single doublequotes. Both techniques show the same output. ...

c# - String equals case insensitive

Check whether two Strings are equal by case-insensitive comparison 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 check whether two String instances are equal when we ignore the case while comparing the String instances. So in this .net c# tutorial code example, we will check whether two String objects have the same value or not in a case-insensitive comparison way. The .net c# developers can check the two String object’s equality by using the String Equals() method. The String Equals() method determines whether two String objects have the same value. The String Equals(String, StringComparison) method overload determines whether this String and a specified String object have the same value. The second parameter specifies the...

c# - How to append a char to a string

Append a Char to 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 append a Char to a String. In this .net c# tutorial code we will append a Char object to a String object. Here we will append a Char ‘L’ to the String using the String Insert() method and we also add a Char to the String instance using a plus operator. The Char represents a character as a UTF-16 code unit. We can simply append a Char instance to a String object by a plus operator. The plus operator adds the specified Char object at the end of the specified String instance. The result also returns a String object. The String Insert() method returns a new String in which a specified String is inserted at a specified index position in the instance. So u...

c# - How to append a substring to a string

Append a substring to 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 append a substring to a String. In this .net c# tutorial code we will append a substring to a String object. Here we will append a substring ‘ Laceflower.’ to the String instance using the String Insert() method and we also add a substring to the String instance using a plus operator. We can simply append a substring to a String object by a plus operator. The plus operator adds the specified substring at the end of the specified String instance. The result also returns a String object. The String Insert() method returns a new String in which a specified String is inserted at a specified index position in the instance. So using the String Insert(...

c# - How to check whether a string contains any special character

Check whether a String contains any special character 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 check whether a String object contains any special character. In this .net c# tutorial code we will determine whether a String instance contains any special character. Here we do this using regular expressions. The Regex represents an immutable regular expression. The Regex() initializes a new instance of the Regex class. The Regex(String, RegexOptions) constructor initializes a new instance of the Regex class for the specified regular expression with options that modify the pattern. So, using the Regex(String pattern, RegexOptions options) method we can check whether a String object contains any special character or no...

c# - How to check whether a string contains numbers only

Check whether a String contains numbers only 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 determine whether a String instance contains numbers only. So, in this .net c# tutorial code we will determine whether a String object’s all characters are digits/numbers or not. The Enumerable All() method determines whether all elements of a sequence satisfy a condition. So using the Enumerable All() method we can check all the characters of a String instance are digits or not. If all the characters of the String object are digits then we can determine that the String instance contains numbers only. The Enumerable All() returns a Boolean value. It returns true if every element of the source sequence passes the test in the spec...

c# - How to check whether a string contains at least one number

Check String contains at least a number 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 check whether a String object contains at least a number. In this .net c# tutorial code we will determine whether a String instance contains at least a number or digit. Here we check this using the Enumerable Any() method. The Enumerable Any() method determines whether any element of a sequence exists or satisfies a condition. So using this Any() method we test each character of the String object that which character is a digit. If it finds an element is a digit then it returns true otherwise it returns false. In this way, we can determine whether a String object contains at least a number/digit or not. The Enumerable Any() method re...

c# - How to check whether a string contains a specific character

Check whether a String contains a specific character 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 check whether a String instance contains a specific character. In this .net c# tutorial code we will determine whether a String instance contains a specific character or not. Here we will check the specific character’s existence within a String instance by using the String Contains() method. The String Contains(Char) method overload returns a value indicating whether a specified character occurs within this string. The String Contains(char value) method has a required parameter named value. The value parameter is the character to seek. The String Contains() method returns a Boolean value. This method returns true if the ...

c# - How to replace first occurrence of a substring within a string

Replace the first occurrence of a substring within 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 replace the first occurrence of a substring within a String instance. So, in this .net c# tutorial code we will find the first occurrence of a substring within a String instance and replace this substring with another specified String. Such as we have a String object where the ‘white’ substring exists multiple times in it. We have to replace only the first occurrence of the substring ‘white’ with another specified String ‘pink’ within the source String instance. The Regex represents an immutable regular expression. The Regex() initializes a new instance of the Regex class. The Regex(String, RegexOptions) constructor...

c# - How to format a string to a fixed length string

Format a String to a fixed length 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 format a String to a fixed-length String. In this .net c# tutorial code, we will format a small String instance to a fixed-length big String instance. Such as if the String length is five then we will format it to fifteen characters length. For the remaining extra length, we put specified characters. The String PadLeft() method returns a new String of a specified length in which the beginning of the current String is padded with spaces or with a specified Unicode character. The String PadLeft(int totalWidth, char paddingChar) method returns a new String that right-aligns the characters in the instance by padding them on the left wit...

c# - How to format a string with leading zeros

Format a String with leading zeros 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 format a String object with leading zeros. In this .net c# tutorial code we will add a specified number of zeros at the beginning of a String instance. The String PadLeft() method allows us to achieve this. The String PadLeft() method returns a new String of a specified length in which the beginning of the current String is padded with spaces or with a specified Unicode character. The String PadLeft(int totalWidth, char paddingChar) method overload returns a new String that right-aligns the characters in this instance by padding them on the left with a specified Unicode character for a specified total length. So we can pass zero ‘0’ ...

c# - How to format a string as currency without decimal

String format currency no decimal the following asp.net c# example code demonstrate us how can we format a string (containing numeric value)to currency with no decimal digit programmatically at run time in an asp.net application. In this example, we format a stringobject containing a decimal number to its equivalent number by rounding it a nearest integer value with dollar currency sign. A decimal number is a floating-point value that consists of a sign. .Net framework's Decimal.ToString() method convert the numeric valueof this instance to its equivalent string representation. Here we also format the decimal digit using ToString() method. ToString("C") method format specifier "C" or "c" name is currency. This format specifier return a currency value. We can also use a precisionspecifier to specify number of decimal digit such as ToString("C2"). This ToString("C2") method format a numeric val...