Computer Programming ||

Slides:



Advertisements
Similar presentations
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
Advertisements

1 Strings and Text I/O. 2 Motivations Often you encounter the problems that involve string processing and file input and output. Suppose you need to write.
Java Programming Strings Chapter 7.
Strings An extension of types A class that encompasses a character array and provides many useful behaviors Chapter 9 Strings are IMMUTABLE.
Mahmoud Rafeek Alfarra Computer Programming || Chapter 2: Exception handling.
©2004 Brooks/Cole Chapter 7 Strings and Characters.
1 A Balanced Introduction to Computer Science, 2/E David Reed, Creighton University ©2008 Pearson Prentice Hall ISBN Chapter 17 JavaScript.
Strings and Arrays The objectives of this chapter are:  To discuss the String class and some of its methods  To discuss the creation and use of Arrays.
An Introduction to Programming with C++ Fifth Edition Chapter 12 String Manipulation.
Strings In Java, strings are contained in an object that is an instance of the String class. String in java is the name of a class. That’s why it starts.
1 Strings and String Operations  What is a Strings?  Internal Representation of Strings  Getting Substrings from a String  Concatenating Strings 
Chapter 5 new The Do…Loop Statement
Strings Representation and Manipulation. Objects Objects : Code entities uniting data and behavior – Built from primitive data types.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
Subroutines and Files Bioinformatics Ellen Walker Hiram College.
String Manipulation Chapter 15 This chapter explains the String facilities. You have already seen some of the main methods of the String class.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 24 The String Section.
Chapter 12: String Manipulation Introduction to Programming with C++ Fourth Edition.
Project 1: Using Arrays and Manipulating Strings Essentials for Design JavaScript Level Two Michael Brooks.
Chapter 3: Classes and Objects Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved Java’s String Class.
Vladimir Misic: Characters and Strings1Tuesday, 9:39 AM Characters and Strings.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Eight String Manipulation.
C++ String Class nalhareqi©2012. string u The string is any sequence of characters u To use strings, you need to include the header u The string is one.
CSC Programming I Lecture 9 September 11, 2002.
Mahmoud Rafeek Alfarra Computer Programming || Chapter 1: Introduction & OOP.
Java String 1. String String is basically an object that represents sequence of char values. An array of characters works same as java string. For example:
© 2006 Lawrenceville Press Slide 1 Chapter 6 The Post-Test Do…Loop Statement  Loop structure that executes a set of statements as long as a condition.
An Introduction to Programming with C++ Sixth Edition Chapter 13 Strings.
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
1 CSE 2337 Chapter 7 Organizing Data. 2 Overview Import unstructured data Concatenation Parse Create Excel Lists.
Computer Programming 2 Lab (1) I.Fatimah Alzahrani.
INLS 560 – S TRINGS Instructor: Jason Carter. T YPES int list string.
Chapter 23 The String Section (String Manipulation) Clearly Visual Basic: Programming with Visual Basic nd Edition.
Strings, Characters, and Regular Expressions Session 10 Mata kuliah: M0874 – Programming II Tahun: 2010.
Chapter 5: Arrays in Java. The objectives of this chapter are:  1. To discuss the creation and use of Arrays.   2. To continue to use the String class.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
© 2010 Lawrenceville Press Slide 1 Chapter 5 The Do…Loop Statement  Loop structure that executes a set of statements as long as a condition is true. 
Strings A string is a sequence of characters that is treated as a single value. Strings are objects. We have been using strings all along. For example,
Strings Chapter 7 CSCI CSCI 1302 – Strings2 Outline Introduction The String class –Constructing a String –Immutable and Canonical Strings –String.
1 Chapter 8 – Character Arrays and Strings Outline 8.1Introduction 8.2Declaring and Initializing String 8.3Input/output of strings 8.4String-handling Functions.
The Methods and What You Need to Know for the AP Exam
Strings A String is a sequence of letters
Strings, Characters and Regular Expressions
String Processing Upsorn Praphamontripong CS 1110
Primitive Types Vs. Reference Types, Strings, Enumerations
Modern Programming Tools And Techniques-I Lecture 11: String Handling
Chapter 7: Strings and Characters
MSIS 655 Advanced Business Applications Programming
Chapter 5 The Do…Loop Statement
Advanced String handling
Strings A string is a sequence of characters that is treated as a single value. Strings are objects. We have been using strings all along. Every time.
String Manipulation and More Controls
Representation and Manipulation
16 Strings.
Basic String Operations
Microsoft Visual Basic 2005: Reloaded Second Edition
Microsoft Visual Basic 2005: Reloaded Second Edition
Topics Introduction to File Input and Output
Topics Basic String Operations String Slicing
String Processing 1 MIS 3406 Department of MIS Fox School of Business
Introduction to Computer Science
String methods 26-Apr-19.
Visual Programming COMP-315
Chapter 17 JavaScript Arrays
Topics Basic String Operations String Slicing
Switch, Strings, and ArrayLists in Java
String Manipulation.
Topics Basic String Operations String Slicing
Unit-2 Objects and Classes
Presentation transcript:

Computer Programming || Chapter 3: String manipulation

Connect applications with DBMS Streams-Based Sockets and Datagrams Syllabus www.cst.ps/staff/mfarra www.facebook.com/mahmoudRfarra Revision of OOP Exception Handling String manipulation Regular expression Files and Streams Connect applications with DBMS Streams-Based Sockets and Datagrams

Contents string vs. System.String Immutability of String Objects www.cst.ps/staff/mfarra www.facebook.com/mahmoudRfarra 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

string vs. System.String www.cst.ps/staff/mfarra www.facebook.com/mahmoudRfarra 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.

Immutability of String Objects www.cst.ps/staff/mfarra www.facebook.com/mahmoudRfarra 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.

Immutability of String Objects www.cst.ps/staff/mfarra www.facebook.com/mahmoudRfarra 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.

Immutability of String Objects www.cst.ps/staff/mfarra www.facebook.com/mahmoudRfarra 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.

Substrings www.cst.ps/staff/mfarra www.facebook.com/mahmoudRfarra 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.

Substrings www.cst.ps/staff/mfarra www.facebook.com/mahmoudRfarra 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.

Example: Substrings String input = textBox1.Text; www.cst.ps/staff/mfarra www.facebook.com/mahmoudRfarra 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));

Split string with delimiter www.cst.ps/staff/mfarra www.facebook.com/mahmoudRfarra 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].

Split string with delimiter www.cst.ps/staff/mfarra www.facebook.com/mahmoudRfarra string MainString = textBox1.Text; string[] Split = MainString.Split(new Char[] { ' ', ',', '.' }); for(int i =0; i<Split.Length; i++) MessageBox.Show(Convert.ToString(Split[i]));

The trim function has three variations: www.cst.ps/staff/mfarra www.facebook.com/mahmoudRfarra 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.

Trim Function string Name = textBox1.Text; www.cst.ps/staff/mfarra www.facebook.com/mahmoudRfarra 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 + ")");

Strip “Remove” string www.cst.ps/staff/mfarra www.facebook.com/mahmoudRfarra 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.

Strip “Remove” string string MainString = textBox1.Text; www.cst.ps/staff/mfarra www.facebook.com/mahmoudRfarra 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);

Tour with C# String Variables www.cst.ps/staff/mfarra www.facebook.com/mahmoudRfarra

Practice www.cst.ps/staff/mfarra www.facebook.com/mahmoudRfarra

Thank You !