Professional Documents
Culture Documents
2 5 1 P 603c5f3d0be8a File
2 5 1 P 603c5f3d0be8a File
<<MSBTE LEAD>>
Unit 02 :
Derived Syntactical
Constructs in Java
Written by
► The String class is commonly used for holding and manipulating strings of text in Java programs.
► In java strings are class objects and implemented using two classes namely, String and StringBuffer.
► String Concatenation
► String Comparison
► Substring
► Length of string and etc.
Concatenate two
or more strings
Using concate()
Using + Operator
method
String Comparison
class stringexample
{ String s = “Hell”;
String s1 = “Hello”; Output
String s2 = “Hello”;
String s3 = “Java”; true
System.out.println(s1.equals(s2)); //true false
System.out.println(s.equals(s1)); //false true
String s4 = new String(“Java”) -1
System.out.println(s1==s2); //true 0
1
System.out.println(s3==s4); //false
System.out.println(s.compareTo(s2)); //returns-1 because s<s2
System.out.println(s1.compareTo(s2)); //returns 0 because s1=s2
System.out.println(s2.compareTo(s)); //returns 1 because s2>s
}
}
Page 16 Maharashtra State Board of Technical Education
Substring in Java
► A part of String is called substring. In other words, substring is a subset of another string.
► We can get substring from the given string object by one of the two methods:
► The java string length() method find the length of the string. It returns the count of total number of
characters.
Example:
public class lengthexample{
public static void main(String args[])
{
String s1 = “Hello World”;
System.out.println(“String length is:” + s1.length());
}
Output:
}
► The java.lang.String class provides a lot of methods to work on string. By the help pf these methods, we
can perform operations on Strings.
► We will learn the following methods:
1)charAt()
2)contains()
3)getChars()
4)indexOf()
5)replace()
6)toLowerCase()
7)toUpperCase
► charAt(): Returns a char value at the given index number. The index number starts from 0.
► Contains(): searches the sequence of characters in this string. It returns true if sequence of char values
are found in this string otherwise returns false.
Example:
class method
{
public static void main(String args[])
String name=“Hello World!”; 0
char ch=name.charAt(4); true
System.out.println(ch); false
System.out.println(name.contains(“Hello”));
System.out.println(name.contains(“hello”));
}
}
Example:
Class methods
{
Public static void main(String args[]) Output:
{ String s1=“Hello World”;
int index1 = s1.indexOf(‘o’);
System.out.println(index1); 4
int index2 = s1.indexOf(‘l’,4); 8
System.out.println(index2);
}
}
► toLowerCase(): it converts all the characters of the string into lowercase letter.
► toUpperCase(): it converts all the characters of the string into uppercase letter.
Example:
class methods
{ public static void main(String args[])
{ Output:
String s1 = “Hello World”;
String lowercase = s1.toLowerCase(); hello world
HELLO WORLD
System.out.println(lowercase);
String uppercase = s1.toUpperCase();
System.out.println(uppercase);
}
}
1) What is the value returned by function compareTo() if the invoking string is less than the string
compared?
a) Zero
b) value less than zero
c) value greater than zero
d) none of the mentioned
<<MSBTE LEAD>>
Unit 02 :
Derived Syntactical
Constructs in Java
Written by
► The String class is commonly used for holding and manipulating strings of text in Java programs.
► In java strings are class objects and implemented using two classes namely, String and StringBuffer.
Example:
Class a
{
public static void main(String args[])
Output:
{
StringBuffer s1 = new StringBuffer(“Hello");
StringBuffer s2 = new StringBuffer(“ World");
Hello World
s1.append(s2);
System.out.println(s1);
}
}
Example:
Class a
{
public static void main(String args[])
{ Output:
StringBuffer s1 = new StringBuffer(“Hello");
StringBuffer s2 = new StringBuffer(“ World"); Hel World lo
S1.insert(3,s2);
System.out.println(s1);
}
}
► This is the delete() function is used to delete multiple character at once from n position to m position
(n and m are will be fixed by you.) in the buffered string.
Example:
Class a
{
public static void main(String args[]) Output:
{
StringBuffer s1 = new StringBuffer(“Hello"); Hlo
s1.delete(1,3);
System.out.println(s1);
}
}
► This is the deleteCharAt() function which is used to delete the specific character from the buffered
string by mentioning that's position in the string.
Example:
class a
{
public static void main(String args[])
{
StringBuffer s1 = new StringBuffer(“Hello"); Output:
s1.deleteCharAt(2);
System.out.println(s1); Helo
}
}
► This is the reverse() function used to reverse the string present in string buffer.
Example:
Class a
{
public static void main(String args[])
{ Output:
StringBuffer s1 = new StringBuffer(“Hello");
s1.reverse();
System.out.println(s1);
olleH
}
}
Example:
Class a
{
public static void main(String args[]) Output:
{
StringBuffer s1 = new StringBuffer(“Hello"); Heleo
s1.setCharAt(3,’e’);
System.out.println(s1);
}
}
1) Which of this method of class StringBuffer is used to concatenate the string representation to the end of invoking
string?
a) concat()
b) append()
c) join()
d) concatenate()