Java Record 9
Java Record 9
Ex no: 9
STRING OPERATIONS
Date:
Question
Let s1 be "Welcome" and s2 be "welcome". Write a Java program for the following statements:
(a) Check whether s1 is equal to s2 and assign the result to a boolean variable ‘isEqual’.
(b) Check whether s1 is equal to s2, ignoring case, and assign the result to a boolean variable
‘isEqual’.
(c) Compare s1 with s2 and assign the result to an int variable x.
(d) Compare s1 with s2, ignoring case, and assign the result to an int variable x.
(e) Check whether s1 has the prefix AAA and assign the result to a boolean variable b.
(f) Check whether s1 has the suffix AAA and assign the result to a boolean variable b.
(g) Assign the length of s1 to an int variable x.
(h) Assign the first character of s1 to a char variable x.
(i) Create a new string s3 that combines s1 with s2.
(j) Create a substring of s1 starting from index 1.
(k) Create a substring of s1 from index 1 to index 4.
(l) Create a new string s3 that converts s1 to lowercase.
(m) Create a new string s3 that converts s1 to uppercase.
(n) Create a new string s3 that trims whitespace characters on both ends of s1.
(o) Assign the index of the first occurrence of the character e in s1 to an int variable x.
(p) Assign the index of the last occurrence of the string abc in s1 to an int variable x.
Aim
To write a program to perform string operations.
Code
package string; public class
StringOperations { public static
void main(String[] args) { String
s1="Welcome"; String
s2="welcome"; boolean
isEqual=s1.equals(s2);
System.out.println("ROLL NO:717823P106");
System.out.println("NAME:ASHWANTH");
System.out.println("s1 equals s2:"+isEqual);
isEqual=s1.equalsIgnoreCase(s2);
System.out.println("Equals ignoer case:"+isEqual);
int x=s1.compareTo(s2);
System.out.println("s1 compare to s2:"+x);
23 717823P106
DEPARTMENT OF COMPUTERSCIENCE AND ENGINEERING 23CSR306 – JAVA PROGRAMMING
x=s1.compareToIgnoreCase(s2);
System.out.println("s1 compareToIgnoreCase s1:"+x); boolean
b=s1.startsWith("AAA"); System.out.println("s1 starts with
'AAA':"+b); b=s1.endsWith("AAA");
System.out.println("s1 ends with 'AAA':"+b);
x=s1.length();
System.out.println("Length of s1:"+x);
char c=s1.charAt(0);
System.out.println("First character of s1:"+c);
String s3=s1+s2;
System.out.println("Combined s1 and s2:"+s3);
String substringIndex1=s1.substring(1);
System.out.println("Sustring of s1 from index 1:"+substringIndex1);
String substringIndex1to4=s1.substring(1, 4);
System.out.println("Substring of s1 from index 1 to 4:"+substringIndex1to4);
s3=s1.toLowerCase();
System.out.println("s1 lowercase:"+s3);
s3=s1.toUpperCase();
System.out.println("s1 uppercase:"+s3);
s3=s1.trim();
System.out.println("s1 with whitespace trimmed:"+s3);
x=s1.indexOf('e');
System.out.println("Index of first occurenece of 'e' in s1:"+x);
x=s1.lastIndexOf("abc");
System.out.println("Index of last occurence of 'abc' in s1:"+x);
}}
24 717823P106
DEPARTMENT OF COMPUTERSCIENCE AND ENGINEERING 23CSR306 – JAVA PROGRAMMING
Output
Result
Thus, the Java program to perform string operations has been successfully developed and the
output was verified.
25 717823P106
DEPARTMENT OF COMPUTERSCIENCE AND ENGINEERING 23CSR306 – JAVA PROGRAMMING
26 717823P106