Write a Java program to experiment String and StringBuffer classes. See their do
ID: 3675854 • Letter: W
Question
Write a Java program to experiment String and StringBuffer classes. See their documentation at http://java.sun.com/j2s e/1.4.2/docs/api/index.html . Have a class named StringTest with a main function. The main function should expect exactly two command - line arguments. If the number of command - line arguments is not two, the main function should return immediately. Otherwis e, it should print out the command - line arguments and continue as follows:
Create two String objects, say s1 and s2 , initializing them with the first and the second command - line arguments. Experiment and print out the results of the following: s1.length() , s1.charAt(i) for all i for String s1 , s1.equals(s2), s1.equalsIgnoreCase(s2), s1.compareTo(s2), s 1.regionMatches (int toffset, s2, int offset, int len) for some offset and len, s1.regionMatches(boolean ignoreCase, int toffset, s2, int offset, int len) for some offset and len, s1.indexOf(c, i) for some c and i, s1.concat(s2), s1.replace(c1, c2), s1.upperCase(), s1.lowerCase(). You can prompt the user to enter the parameter values (e.g. offset, len .. etc) .
Explanation / Answer
System.out.println( " The length of String s1 : "+ s1.length());
for( int i=0; i<s1.length(); i++)
System.out.println(s1.charAt(i));
System.out.println( " Both Strings are Equal ");
if(s1.equalsIgnoreCase(s2)) // it does not lower or upper case letters
System.out.println( " Both Strings are Equal ");
System.out.println( s1.compareTo(s2));
StringBuffer strB = new StringBuffer(s1);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.