Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Programming ||

Similar presentations


Presentation on theme: "Computer Programming ||"— Presentation transcript:

1 Computer Programming ||
Chapter 3: String manipulation

2 Connect applications with DBMS Streams-Based Sockets and Datagrams
Syllabus Revision of OOP Exception Handling String manipulation Regular expression Files and Streams Connect applications with DBMS Streams-Based Sockets and Datagrams

3 Contents string vs. System.String Immutability of String Objects
string vs. System.String Immutability of String Objects Substrings and Split string with delimiter Trim Function Strip specified number of characters from string Tour with C# String Variables Practice

4 string vs. System.String
A string is an object of type String whose value is text. Internally, the text is stored as a sequential read-only collection of Char objects. The Length property of a string represents the number of Char objects it contains.

5 Immutability of String Objects
String objects are immutable; they cannot be changed after they have been created. All of the String methods and C# operators that appear to modify a string actually return the results in a new string object.

6 Immutability of String Objects
In the following example, when the contents of s1 and s2 are concatenated to form a single string, the two original strings are unmodified. The += operator creates a new string that contains the combined contents.

7 Immutability of String Objects
That new object is assigned to the variable s1, and the original object that was assigned to s1 is released for garbage collection because no other variable holds a reference to it.

8 Substrings A substring is any sequence of characters that is contained in a string. Use the Substring method to create a new string from a part of the original string. You can search for one or more occurrences of a substring by using the IndexOf method.

9 Substrings Use the Replace method to replace all occurrences of a specified substring with a new string. Like the Substring method, Replace actually returns a new string and does not modify the original string.

10 Example: Substrings String input = textBox1.Text;
String input = textBox1.Text; MessageBox.Show("number of characters is: "+ input.Length); String input = textBox1.Text; int beging = Convert.ToInt16(textBox2.Text); int count = Convert.ToInt16(textBox3.Text); MessageBox.Show("The Sub string is: " + input.Substring(beging, count)); String input = textBox1.Text; String find = textBox4.Text; string rep = textBox5.Text; MessageBox.Show("The Sub string after replacement is: " + input.Replace(find, rep)); String input = textBox1.Text; String str = textBox7.Text; MessageBox.Show("The index of Sub string is: " + input.IndexOf(str));

11 Split string with delimiter
The example below shows how to split the string into separate parts via a specified delimiter. The results get put into the Split array and called back via Split[0].

12 Split string with delimiter
string MainString = textBox1.Text; string[] Split = MainString.Split(new Char[] { ' ', ',', '.' }); for(int i =0; i<Split.Length; i++) MessageBox.Show(Convert.ToString(Split[i]));

13 The trim function has three variations:
The trim function has three variations: Trim: It strips all white spaces from both the start and end of the string. TrimStart: It strips all white spaces from the start of the string. TrimEnd: It strips all white spaces from the end of the string.

14 Trim Function string Name = textBox1.Text;
string Name = textBox1.Text; string NewName = Name.Trim(); MessageBox.Show("("+NewName+")"); string Name = textBox1.Text; string NewName = Name.TrimStart(); MessageBox.Show("(" + NewName + ")"); string Name = textBox1.Text; string NewName = Name.TrimEnd(); MessageBox.Show("(" + NewName + ")");

15 Strip “Remove” string How you can strip a number of characters from a specified starting point within the string. The first number is the starting point in the string and the second is the amount of chars to strip.

16 Strip “Remove” string string MainString = textBox1.Text;
string MainString = textBox1.Text; int begin = Convert.ToInt16(textBox2.Text); int count = Convert.ToInt16(textBox3.Text); string NewString = MainString.Remove(begin, count); MessageBox.Show(NewSring);

17 Tour with C# String Variables

18 Practice

19 Thank You !


Download ppt "Computer Programming ||"

Similar presentations


Ads by Google