IP 07 Handout 1
IP 07 Handout 1
Strings
Strings
A string is a sequential collection of characters that is used to represent text. In C#, a string is an object of class string in the
System namespace.
In C#, a string variable can be initialized by using either of the following:
• directly assigning a string literal; or
• using the new keyword and calling the String class constructor.
The most commonly used method for creating a string object is by assigning a string literal to a string variable. The following
example use assignment operator to assign the string literal to a string variable:
string strMessage = "Welcome to this course!";
string strPath = @"C:\Documents\Report.docx";
In the given example, the backslash (\) is used to escape characters with special use, such as newline (\n). The '@' symbol before
the string literal is used to ignore the special use of backslashes.
Calling the String class constructor is the other method used to create a string object in C#. The following statements show
an example of how to use new keyword and String class constructor:
char[] word = { 'H', 'e', 'l', 'l', 'o', '!' };
string strGreet = new string(word);
In the given example, the String class constructor is used to create a string from an array of characters named word. Note
that in C#, there is no String class constructor that takes a string literal as an argument. The String constructors in C# only
creates a string from characters and arrays.
The String class has two (2) properties that can be used to get some information of the string and can be used to manipulate
strings. The following are the properties of the String class:
• char – This property is used to get the character at a specified index position of a string. For example:
string word = "Computer";
char letter = word[2];
//the value of variable letter is character 'm'
• Length – This property is used to get the total number of characters in the string. For example:
string word = "Computer";
int total = word.Length;
//the value of variable total is 8
The String class provides several methods that are used to perform operations on strings. Table 1 shows some of the methods
of String class in C#.
Table 1. Methods in the class String (Harwani, 2015)
Method Description
This returns true if the specified substring occurs within the string object; otherwise, it returns false.
bool
Contains(string For example:
value) string sentence = "The quick brown fox jumps.";
bool isContain = sentence.Contains("fox"); //returns true
This determines whether the specified substring has the same value with the string object. The
comparisonType parameter specifies the rules to use in comparing strings, such as ignoring the case
bool version of the characters.
Equals(string Example 1:
value, string word = "Computer";
StringComparison bool isSame = word.Equals("computer", StringComparison.CurrentCulture); //this
comparisonType) returns false
Example 2:
string word = "Computer";
Method Description
bool isSame = word.Equals("computer", StringComparison. CurrentCultureIgnoreCase);
//this returns true
This returns the index position value of the first occurrence of the specified character within the string
int IndexOf(char object if that character is found; otherwise, it returns -1 if not found. For example:
value) string word = "Computer";
int index = word.IndexOf('p'); //returns integer 3
string This returns a copy of the string object except that all of the occurrences of an old character are replaced
Replace(char with a new character. For example:
oldValue, char string word = "Color";
newValue) string strChanged = word.Replace('o', '#'); //returns a copy of string "C#l#r"
This returns a converted copy of object as a string. For example:
string
ToString() double value = 105.25;
string strValue = value.ToString(); //converted 105.25 into string
This returns a copy of the string object converted to lowercase. For example:
string ToLower() string word = "COMPUTER";
string strConverted = word.ToLower(); //returns a copy of string "computer"
This returns a copy of the string object converted to uppercase. For example:
string ToUpper() string word = "computer";
string strConverted = word.ToUpper(); //returns a copy of string "COMPUTER"
The String class methods only create a copy of the string object and return a new string containing the result of the method
operation.
Strings are immutable. This means that when a string object is created, its contents cannot be changed. Every instance of a
string that has been modified is actually creating a new string in the computer’s memory. For example:
string strComputer = "Computer";
strComputer = strComputer + " is a great invention.";
Output:
# + o + m + p + u + t + e + r +
REFERENCES:
Deitel, P. and Deitel, H. (2015). Visual C# 2012 how to program (5th Ed.). USA: Pearson Education, Inc.
Gaddis, T. (2016). Starting out with visual C# (4th Ed.). USA: Pearson Education, Inc.
Harwani, B. (2015). Learning object-oriented programming in C# 5.0. USA: Cengage Learning PTR.
07 Handout 1 *Property of STI
[email protected] Page 4 of 4