Lab 07: Java Strings
Objectives:
After the completion of this lab, students should be able to:
Declare and create Strings.
Apply some operations on Java Strings
Lab Exercises:
Lab Exercise 1 – String Operations
Problem Description:
Write a Java program (StringOperations1.java) that performs some operations on
the following string literal: "Welcome! This is "CS110" Course".
Create an object of class String called str that hold the above string literal,
Print out the string str,
Convert all alphabets to upper-case letters and print out the result,
Convert all alphabets to lower-case letters and print out the result,
Print out the length of the string,
Print out the index of the word Course,
Remove the 1st three occurrences of the char 'c' or 'C' and print out the result,
Search for CS110 and replace it with CE211.
Sample output:
The original string: Welcome! This is "CS110" Course
Upper Case: WELCOME! THIS IS "CS110" COURSE
Lower Case: welcome! this is "cs110" course
Length: 31
Index of 'Course': 25
Removing the first 3 [c or C] chars: Welome! This is "S110" ourse
Replacing 'CS110' with 'CE211': Welcome! This is "CE211" Course
©2011 Dr. Abdulbaset Gaddah, < Computer Programming > [ 1]
Lab 07: Java Strings
Lab Exercise 2 – More String Operations
Problem Description:
Write a Java program (StringOperations2.java) that performs some operations on
the following strings (String s1 = "Ali", String s2 = "Magdi", String s3 = "[[]]").
Compare s1 and s2 to find out which one comes first in alphabetical order,
Create a new string that is string s2 in the middle of the string s3,
Create a new string that is the first char of s1 and the last char of s2,
Print out the string of length 3 from the middle of string s2.
Sample output:
Magdi comes before Ali
[[Magdi]]
AM
agd
©2011 Dr. Abdulbaset Gaddah, < Computer Programming > [ 2]