0% found this document useful (0 votes)
31 views16 pages

Lecture1 Strings

Uploaded by

Foaina
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views16 pages

Lecture1 Strings

Uploaded by

Foaina
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 16

Review

Content
Package java.lang
Program Layout
The constructor and main Methods
The String Class
String Class Methods
Exception Handling
Package java.lang
Provides classes that are fundamental to the design of
the Java programming language.
The most important classes are Object, which is the
root of the class hierarchy, and Class, instances of
which represent classes at run time.
Package java.lang automatically gets called
Java program layout
Typical java program layout
Package
Class
Constructor method
Main method
example Package name

1. package forloopexercise;
2. /**
3. * Title: forLoopExercise
4. * Description: for loop to output a multiplication table of 2
5. * Copyright: Copyright (c) 2009
6. * Company: nus
7. * @author hobert
8. * @version 1.0
9. */ Class name

10. public class applicationMain {

11. public applicationMain() { Constructor method


12. }
main method
13. public static void main(String[] args) {
14. applicationMain applicationMain1 = new applicationMain();
15. }
16. }
The String class
The class String includes methods for examining
individual characters of the sequence

Comparing strings
Searching strings
Extracting substrings
Translating to uppercases and lowercases
String concatenation
String concatenation
Used to combine two or more Strings.
This is done by using the + symbol

Example

String tempString1=“Hello”;
String tempString2=“World”;
String tempString3;

tempString3=tempString1+tempString2
System.out.println(tempString3)
String.length() method
Used to return the number of characters in the String
Example Expected Output

String temp1=“A”; len1 is 1


String temp2=“John; len2 is 4
String temp3=“j.w. h”; len3 is 6

int len1=temp1.length();
System.out.println(“len1 is “+ len1);

int len2=temp2.length()’;
System.out.println(“len2 is “+ len2);

int len3=temp3.length()’;
System.out.println(“len3 is “+ len3);
char String.charAt(int) method
Returns the char (character ) at the given element
position of the String

The given element position is usually called the index


Java coding example
String ts=“Edna”; Expected Output
Char ch=ts.charAt(3); ch=4
System.out.println(“ch=“+ch)

String ts=“Life is good”; Expected Output


Char ch=ts.charAt(8); ch=g
System.out.println(“ch=“+ch)
int String.indexOf(String, int) method
Returns the index within this String of the first
occurrence of the specified subString (first
parameter), starting at the specified index (second
parameter) including whitespace
Java coding example
String ts=“Life is good good”; Expected Output
int a = ts.indexOf(“ ”, 0); 8
int b = ts.indexOf(“ ”, 5); a is 4
b is 7
System.out.println(txt.indexOf(“good"));
System.out.println(“a is:”+a)
System.out.println(“b is:”+b)
String String.substring(int, int) method
Returns a String from the begin index (first
parameter) to the end index (second parameter)

Java coding example


String ts=“J Hersh”; Expected Output
rs0=J
String rs0 = ts.substring(0, 1); rs1=Hersh
String rs1 = ts.substring(2, 7);

System.out.println(“rso = ”+rs0);
System.out.println(“rs1 = ”+rs1);
int String.compareTo(String) method
Used to compare two Strings

Given returnValue=String1.compareTo(String2), then


the returnValue will be zero, greater than zero, or less
than zero
returnValue Description
o String1 is equal to String2
Any number less than zero String1 is alphabetically less than
String2
Any number greater than zero String1 is alphabetically greater than
String2
Java coding example
String temp1=“Apple”; Expected Output
String temp2=“Bed”; cv=0
String temp3=“Car”; cv=-1
String temp4=“Apple”; cv=1
int cv=0;

cv=temp1.compareTo(temp4);
System.out.println(“cv=“+cv);

cv=temp1.compareTo(temp2);
System.out.println(“cv=“+cv);

cv=temp3.compareTo(temp31;
System.out.println(“cv=“+cv);
Uppercase to Lowercase
Example
String txt = "Hello World";
System.out.println(txt.toUpperCase()); // Outputs
"HELLO WORLD"
System.out.println(txt.toLowerCase()); // Outputs
"hello world"

You might also like