Strings

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/49

flashcard set

Earn XP

Description and Tags

2Y2 | Midterms

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

50 Terms

1
New cards

String

is a sequential collection of characters that is used to represent text.

2
New cards

String

In C#, a _ is an object of class string in the System namespace.

3
New cards

In C#, a string variable can be initialized by using either of the following:

4
New cards

• directly assigning a string literal; or

5
New cards

• using the new keyword and calling the String class constructor.

6
New cards

The most commonly used method for creating a string object is by assigning a string literal to a string variable.

7
New cards

The following example use assignment operator t assign the string literal to a string variable:

string strMessage = "Welcome to this course!";

string strPath = @"C:\Documents\Report.docx";

8
New cards

In the given example, the backslash () is used to escape characters with special use, such as newline (\n).

9
New cards

@

The @ symbol before the string literal is used to ignore the special use of backslashes.

10
New cards

Calling the String class constructor is the other method used to create a string object in C#.

11
New cards

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);

12
New cards

In the given example, the String class constructor is used to create a string from an array of characters named word.

13
New cards

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.

14
New cards

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:

15
New cards

• 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'

16
New cards

• Length

This property is used to get the total number of characters in the string.

17
New cards

For example:

string word = "Computer";

int total = word.Length;

//the value of variable total is 8

18
New cards

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#.

19
New cards

Table 1. Methods in the class String (Harwani, 2015) Method Description

20
New cards

bool Contains(string value)

This returns true if the specified substring occurs within the string object; otherwise, it returns false.

For example:

string sentence = "The quick brown fox jumps.";

bool isContain = sentence.Contains("fox"); //returns true

21
New cards

bool Equals(string value, StringComparison comparisonType)

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 version of the characters.

Example 1:

string word = "Computer";

bool isSame = word.Equals("computer", StringComparison.CurrentCulture); //this

returns false

Example 2:

string word = "Computer";

string strMessage = "Welcome to this course!";

string strPath = @"C:\Documents\Report.docx";

char[] word = { 'H', 'e', 'l', 'l', 'o', '!' };

string strGreet = new string(word);

string word = "Computer";

char letter = word[2];

//the value of variable letter is character 'm'

string word = "Computer";

int total = word.Length;

//the value of variable total is 8

Method Description

bool isSame = word.Equals("computer", StringComparison. CurrentCultureIgnoreCase);

//this returns true

22
New cards

int IndexOf(char value)

This returns the index position value of the first occurrence of the specified character within the string object if that character is found; otherwise, it returns -1 if not found.

For example:

string word = "Computer";

int index = word.IndexOf('p'); //returns integer 3

23
New cards

string Replace(char oldValue, char newValue)

This returns a copy of the string object except that all of the occurrences of an old character are replaced with a new character.

For example:

string word = "Color";

string strChanged = word.Replace('o', '#'); //returns a copy of string "C#l#r"

24
New cards

string ToString()

This returns a converted copy of object as a string.

For example:

double value = 105.25;

string strValue = value.ToString(); //converted 105.25 into string

25
New cards

string ToLower()

This returns a copy of the string object converted to lowercase.

For example:

string word = "COMPUTER";

string strConverted = word.ToLower(); //returns a copy of string "computer"

26
New cards

string ToUpper()

This returns a copy of the string object converted to uppercase.


For example:

string word = "computer";

string strConverted = word.ToUpper(); //returns a copy of string "COMPUTER"

27
New cards

The String class methods only create a copy of the string object and return a new string containing the result of the method operation.

28
New cards

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.

29
New cards

For example:

string strComputer = "Computer";

strComputer = strComputer + " is a great invention.";

30
New cards

Figure 1. Memory allocation for String class

31
New cards

In Figure 1, the variable strComputer of String class will allocate a space in the memory with the string "Computer". When the content of this variable is modified by concatenating the string "is a great invention.", the compiler will allocate new memory space for the modified string instead of modifying the initial string at the same memory address. This behavior will delay the performance of the application if the same string is modified multiple times.

32
New cards

To solve this problem, C# provides the StringBuilder class that represents a mutable string of characters.

33
New cards

StringBuilder class

represents a mutable string of characters that allows the user to expand the number of characters in the string object without allocating additional memory space.

34
New cards

The following shows the way of creating a string using the StringBuilder class and using the new keyword and StringBuilder constructor:

35
New cards

The following syntax shows how to use Append() method to concatenate a string to the string object of StringBuilder class.

StringBuilder strComputer = new StringBuilder("Computer");

36
New cards

The example below will return the string ("Computer is a great invention.".

strComputer.Append(" is a great invention.");

37
New cards

Figure 2 shows how a StringBuilder class allocates a memory space when modifying a string object.

38
New cards

Figure 2. Memory allocation for StringBuilder class

knowt flashcard image
39
New cards

In the figure, the content "Computer" of the variable strComputer of StringBuilder class is allocated to a memory space.

40
New cards

When the Append() method is used to modify the content of the string variable, the content "Computer is a great invention." is allocated to the same memory address.

41
New cards

The StringBuilder class also contains two (2) main properties that can get and manipulate the information on a string. These properties are char and Length.

42
New cards

The following example shows how to use these properties:

StringBuilder word = new StringBuilder("Computer");

word[0] = '#'; //change the character at index 0 to '#'

for (int index = 0; index < word.Length; index++) {

Console.Write(word[index] + " + "); //gets the character at the specified index

}

Output:

# + o + m + p + u + t + e + r +

43
New cards

StringBuilder class

contains useful methods that can be used to perform operations on the content of the StringBuilder object.

44
New cards

Table 2 lists some of the methods of StringBuilder class.

45
New cards

Table 2. Some methods in the class String (Harwani, 2015)

46
New cards

Append(string value)

This appends a copy of the specified substring to the StringBuilder object.

For example:

StringBuilder word = new StringBuilder("Computer");

word.Append(" Ethics"); //returns the string "Computer Ethics"

47
New cards

Equals(string value)

This returns true if the specified substring is equal to the StringBuilder object; otherwise, it returns false.

For example:

StringBuilder wordA = new StringBuilder("computer");

StringBuilder wordB = new StringBuilder("computer");

wordA.Equals(wordB); //returns true because same content

48
New cards

Clear()

This removes all characters from the current StringBuilder object.

For example:

StringBuilder word = new StringBuilder("Computer");

word.Clear();

word.Append("Ethics"); //returns the string "Ethics" only

49
New cards

Replace(char oldValue, char newValue)

This replaces all occurrence of a specified old character of the StringBuilder object with a new character.

For example:

StringBuilder word = new StringBuilder("Color");

word.Replace('o', '*'); //returns the string "C*l*r"

50
New cards

ToString()

This converts the value of the StringBuilder object to a string.

For example:

StringBuilder word = new StringBuilder("computer");

string strWord = word.ToString();