Introduction to programming in java

Slides:



Advertisements
Similar presentations
Java
Advertisements

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
Strings in Java 1. strings in java are handled by two classes String &
MSc IT Programming Methodology (2). number name number.
Chapter 7 Strings F To process strings using the String class, the StringBuffer class, and the StringTokenizer class. F To use the String class to process.
1 Working with String Duo Wei CS110A_ Empty Strings An empty string has no characters; its length is 0. Not to be confused with an uninitialized.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
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.
Programming 2 CS112- Lab 2 Java
J.43 ARRAYS  A Java array is an Object that holds an ordered collection of elements.  Components of an array can be primitive types or may reference.
CSM-Java Programming-I Spring,2005 String Handling Lesson - 6.
23-Jun-15 Strings, Etc. Part I: String s. 2 About Strings There is a special syntax for constructing strings: "Hello" Strings, unlike most other objects,
Strings Reading for this Lecture, L&L, 3.2. Strings String is basically just a collection of characters. Thus, the string “Martyn” could be thought of.
String StringBuffer. class StringExample { public static void main (String[] args) { String str1 = "Seize the day"; String str2 = new String(); String.
Lab session 3 and 4 Topics to be covered Escape sequences Escape sequences Variables /identifiers Variables /identifiers Constants Constants assignment.
28-Jun-15 String and StringBuilder Part I: String.
CS 180 Recitation 8 / {30, 31} / Announcements Project 1 is out –Due 10:00pm –Start early! –Use the newsgroup for your questions –LWSN B146.
COMP 110 Introduction to Programming Mr. Joshua Stough September 10, 2007.
1 Strings and String Operations  What is a Strings?  Internal Representation of Strings  Getting Substrings from a String  Concatenating Strings 
Copyright 2006 by Pearson Education 1 Building Java Programs Chapters 3-4: Using Objects.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Characters In Java, single characters are represented.
From C++ to Java A whirlwind tour of Java for C++ programmers.
EXAM 1 REVIEW. days until the AP Computer Science test.
Working with string In java Four classes are provided to work with String:1) String 2)String Buffer 3)String Tokenizer 4)String Builder Note: An object.
Characters and Strings. Characters  New primitive char  char letter; letter = ‘a’; char letter2 = ‘C’;  Because computers can only represent numbers,
1 Textual Data Many computer applications manipulate textual data word processors web browsers online dictionaries.
Jaeki Song JAVA Lecture 07 String. Jaeki Song JAVA Outline String class String comparisons String conversions StringBuffer class StringTokenizer class.
Chapter 3: Classes and Objects Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved Java’s String Class.
More Strings CS303E: Elements of Computers and Programming.
Coding Bat: Ends in ly Given a string of even length, return a string made of the middle two chars, so the string "string" yields "ri". The string.
CSC Programming I Lecture 9 September 11, 2002.
CSI 3125, Preliminaries, page 1 String. CSI 3125, Preliminaries, page 2 String Class Java provides the String class to create and manipulate strings.
Strings in Java. What data types have we seen so far?
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:
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.
17-Feb-16 String and StringBuilder Part I: String.
Strings CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Computer Programming 2 Lab (1) I.Fatimah Alzahrani.
2 Arrays Array is a data structure that represents a collection of the same type of data. A "fixed length list".
Chapter 8 String Manipulation
String and StringBuffer classes
String class.
EKT 472: Object Oriented Programming
Maha AlSaif Maryam AlQattan
Programming in Java Text Books :
String and String Buffers
Methods Chapter 4: Methods Asserting Java ©Rick Mercer.
String Handling in JAVA
Modern Programming Tools And Techniques-I Lecture 11: String Handling
String and StringBuilder
Chapter 7: Strings and Characters
Object Oriented Programming
MSIS 655 Advanced Business Applications Programming
Advanced String handling
CSS 161 Fundamentals of Computing Introduction to Computers & Java
String and StringBuilder
Tonga Institute of Higher Education
String and StringBuilder
Lecture 07 String Jaeki Song.
String and StringBuilder
Introduction to Computer Science
String methods 26-Apr-19.
String Methods.
Strings in Java.
Exam Prep.
More on iterations using
In Java, strings are objects that belong to class java.lang.String .
Strings in Java Strings in Java are also a reference type.
What We Want To Do User enters: Mary Smith
Presentation transcript:

Introduction to programming in java Lecture 12 String class

Topics Length() method equals() method charAt() method substring() method toLowerCase() method toUpperCase() method Trim() method StartsWith() method EndsWith() method

The Length of a String The length of a string is the number of characters it contains, including space characters and punctuation. For example:

The Length of a String (Cont…) An empty string has length zero. The length() method of a String returns its length: Example: String stringA = "Alphabet " ; String stringB = "Soup" ; String stringC = stringA + stringB; System.out.println( stringC.length() ) ; What does the code print? Answer: 13 ( Don't forget to count the space.)

equals() Method Example: String stringA = "Random Jottings"; String stringB = "Lyrical Ballads"; if ( stringA.equals( stringB ) ) System.out.println("They are equal."); else System.out.println("They are different.");

charAt() Method The charAt(int index) method returns a single character at the specified index.

Practice Question What is the output of the following fragment: String singleA = "A" ; String singleB = new String( "B" ); System.out.println( singleA.length() ); Answer: 1

Practice Question What is wrong with the following fragment: char oneC = 'C' ; char oneD = 'B’); System.out.println( oneC.length() ); Answer: length() method is used for String not char.

Substrings() Method A substring of a string is a sequence of characters from the original string. public String substring(int beginIndex ) This method creates a new substring that consists of the characters from character number beginIndex to the end of the string. For example String str = "applecart“; str.substring(5) contains the characters "cart" .

Substrings() Method (Cont…) What is printed by the following code: String source = "applecart"; String sub = source.substring(6); System.out.println( sub ); System.out.println( source ); Answer: art applecart

Substrings() Method (Cont…) Expression Result Comment String snake = "Rattlesnake"; Rattlesnake Create original String snake.substring(6) snake Characters starting at character 6 to the end snake.substring(0) The new substring contains all the characters of the original string snake.substring(10) e snake.substring(11) If beginIndex==length, an empty substring is created. snake.substring(12) Error If beginIndex is greater than length, an exception is thrown.

Another substring() Here is another method of String objects: substring(int beginIndex, int endIndex)

toLowerCase() Method toLowerCase() converts all characters in the string in lower case. Example String n1 = “Ahmed Ali” String n2 = n1.toLowerCase(); System.out.println(n1 + “\n” + n2); Output: Ahmed Ali ahmed ali

toUpperCase() Method toUpperCase() converts all characters in the string in upper case. Example String n1 = “Ahmed Ali” String n2 = n1.toLowerCase(); System.out.println(n1 + “\n” + n2); Output: Ahmed Ali AHMED ALI

Trim() Method Trim() method removes whitespace characters (blanks, tabs, and several other non-printing characters) from both ends (but not from the middle). Example: String userData = " 745 "; String fixed; fixed = userData.trim(); System.out.println(fixed); Output: 745

The StartsWith() Method startsWith(): This method tests if this string start with the specified prefix. class PrefixTest { public static void main ( String args[] ) { String major = “computer science"; if ( burns.startsWith( “computer" ) ) System.out.println( "Prefix matches." ); else System.out.println( "Prefix fails." ); }

The EndsWith() Method The endsWith This method tests if this string ends with the specified suffix. class SuffixTest { public static void main ( String args[] ) { String major = “computer science"; if ( burns.endsWith( “science" ) ) System.out.println( “suffix matches." ); else System.out.println( " suffix fails." ); }

Practice Question # 1 Write a program that asks for the user's name. If user name starts with letter ‘A’ or ‘a’ then program prints the user’s name in upper case else program prints the user’s name in lower case. Example: > Java test Enter your name: Abdullah Mohammad ABDULLAH MOHAMMAD

Practice Question # 2 Write a program that asks user to submit password. Program will check the length of the password. If the length of a password is less than 6 then program prints message “user has provided weak password.” else program prints “user has provided strong password”. Example: java test Enter password: abc1234 User has provided strong password

Submission deadline of Assignment No. 2 is 8th Oct 2013 For details: See Lecture 11