Microsoft Visual C Sharp: Working with Strings
If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!
Working with Strings
Strings are special data types that can hold textual information. In the Microsoft Visual C# language, string is a keyword. It is simply a different name from the String class in the System namespace. Therefore, string has all the properties and methods of the String class. A value in a string cannot be modified.
String Methods
When string methods are called or when you make changes to a string, a new string is actually created. If you try to change a character in a string, you get an error. The following sample program tries to replace the 4th character of the str1 string ABCDEF with X, which cannot be done. Compiling this program should give you an error message about this.
//———————————————————-
using System;
class Str_Err
{
public static void Main ()
{
string str1 = “ABCDEF”;
str1[3] = ‘X’;
Console.WriteLine(str1);
}
}
//———————————————————
So, if strings cannot be modified, then is their usefulness limited? No, because string methods allow you to make string comparisons, which come in handy as you develop your program. A number of extremely useful methods can be used with string comparisons. The following list shows some of the more prominent string methods, along with descriptions of their use.
Static Methods
Compare - compares the values of two strings.
CompareOrdinal - compares the values of two strings without compensating for language or other internationalization issues.
Concat - concatenates (joins) two or more strings into one string.
Copy - creates a new string from an existing string.
Equals - compares two strings to determine whether they contain the same value. Returns true if the two values are equal. Otherwise, returns false.
Format - replaces format specifiers with their corresponding string values. Specifiers were covered earlier today.
Join - Concatenates two or more strings. A specified “separator string” is placed between each of the original strings.
Methods and Properties of Each Instance
Char – returns the character at a given location.
Clone – returns a copy of the stored string.
CompareTo – compares the value of the string with the value of another string. This method returns a negative number if this string is less than the compared string, 0 if equal, and a positive number if the value of this string is greater.
CopyTo – copies a portion of or all of a string to a new string or character array.
EndsWith – determines whether the end of the value stored in the string is equal to a string value. If they are equal, true is returned; otherwise, false is returned.
Equals – compares two strings to determine whether they contain the same value. Returns true if the two values are equal. Otherwise, false is returned.
IndexOf – returns the index (location) of the first match for a character or string. Returns -1 if the value is not found.
Insert – inserts a value into a string. This is done by returning a new string.
LastIndexOf – returns the index (location) of the last match for a character or a string. Returns -1 if the value is not found.
Length – returns the length of the value stored in the string. The length is equal to the length number of characters contained.
PadLeft – right-justifies the value of a string and then pads any remaining spaces with a specified character (or space).
PadRight – left-justifies the value of a string and then pads any remaining spaces with a specified character (or space).
Remove – deletes a specified number of characters from a specified location within a string.
Split – the opposite of join. Breaks a string into substrings based on a specified value. The specified value is used as a breaking point.
StartsWith – determines whether the value stored in a string starts with a specified character or set of characters. Returns true if there is a match and false if not. If specified character is null, true is also returned.
Substring – returns a substring from the original string starting at a specified location. The number of characters for the substring might also be specified but is not required.
ToCharArray – copies the characteristics in the current string value to a char array.
ToLower – returns a copy of the current value in all lowercase letters.
ToUpper – returns a copy of the current value in all uppercase letters.
Trim – removes copies of a specified string from the beginning and end of the current string.
TrimEnd – removes copies of a specified string from the end of the current string.
TrimStart – removes copies of a specified string from the beginning of the current string.
Building Strings
The StringBuilder class is provided in the System.Text namespace to create an object that can hold a string value that can be changed. An object created with the StringBuilder class operates similarly to a string. The difference is that methods of a StringBuilder can directly manipulate the value stored. The methods and properties for a StringBuilder object are displayed in the following list:
Append – appends an object to the end of the current StringBuilder.
AppendFormat – inserts objects into a string based on formatting specifiers.
Capacity – sets or gets the number of characters that can be held. Capacity can be increased up to the value of MaxCapacity.
Chars – sets or gets the character at a given index position using indexer notation.
EnsureCapacity – ensures that the capacity of StringBuilder is at least as big as a provided value. If the value is passed to EnsureCapacity, the value of the Capacity property is set to this new value. If MaxCapacity is less than the value passed, then an exception is thrown.
Questions? Drop me an e-mail at great.documents@gmail.com and I’ll do my very best to answer your question or concern. All The Best, Keith
Inspired Source: Learn C# in 21 Days by Bradley Jones
















Share your thoughts, leave a comment!