2. Arrays & Loops
• Arrays in Java
• Dimensions of arrays
• Enhanced for loop
• Passing arrays to methods
• Java is strictly pass-by-value, exactly as in C.
• Array as objects
Instructor: Tanzila Kehkashan
2
3. String Class
• The String class represents character strings. All string literals in Java programs, such as "abc", are
implemented as instances of this class.
• String str = "abc";
is equivalent to:
• char data[] = {'a', 'b', 'c'};
String str = new String(data);
• String()
• String(String)
• char charAt(int) //index
• int codePointAt(int)// index
• int compareTo(String) //returns 0 if true
• int compareToIgnoreCase(String)
• Stringconcat(String)
Instructor: Tanzila Kehkashan
3
4. String Class
• boolean contains(String)
• boolean startsWith(String)
• boolean endsWith(String)
• int indexOf(char)
• int indexOf(char, int) // int is index from where to start search
• int indexOf(String)
• int indexOf(String, int)
• int lastIndexOf(char) //starting backward
• int lastIndexOf(char, int)
• int lastIndexOf(String)
• int lastIndexOf(String, int)
• int length()
Instructor: Tanzila Kehkashan
4
5. String Class
• boolean isEmpty()
• String replace(char, char) //replaces all occurances
• String replace(String, String)
• String replaceFirst(String, String)
• String substring(int) // starting index to end
• String substring(int beginIndex, int endIndex) // starting index, ending index
• char[] toCharArray()
• String toLowerCase()
• String toUpperCase()
• String trim()
Instructor: Tanzila Kehkashan
5
6. Assignment
• Write a program in Java which
• Takes a string from user
• Asks which option to perform from following:
• Cut & paste
• Copy & paste
• Replace All
• And then displays updated string.
• At the end program displays following statistics of updated string
• Character count excluding spaces
• Word count
Instructor: Tanzila Kehkashan
6