Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to programming in java

Similar presentations


Presentation on theme: "Introduction to programming in java"— Presentation transcript:

1 Introduction to programming in java
Lecture 12 String class

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

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

4 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.)

5 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.");

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

7 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

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

9 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" .

10 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

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

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

13 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

14 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

15 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 = " "; String fixed; fixed = userData.trim(); System.out.println(fixed); Output: 745

16 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." ); }

17 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." ); }

18 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

19 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

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


Download ppt "Introduction to programming in java"

Similar presentations


Ads by Google