Ch 7 String Handling
In Java, a string is a sequence of characters.
For example, "hello" is a string containing a sequence of characters 'h', 'e', 'l', 'l',
and 'o'.
Unlike other programming languages, strings in Java are not primitive types
(like int, char, etc).
Instead, all strings are objects of a predefined class named String
//1.Simple String Example
public class StringEx{
public static void main(String args[]){
//creating a string by java string literal
String str1 = "Welcome to java";
//creating another java string str2 by using new keyword
String str2 = new String("Java using Bluej");
//Displaying the two strings
System.out.println(str1);
System.out.println(str2);
}
}
Java String Methods
String trim () Removes white space from both ends of this String
String toLowerCase() Converts all the characters in this String to
lowercase
String toUpperCase() Converts all the characters in this String to
uppercase
int length( ) returns the length of the specified string
char charAt (int n) returns the character at the specified index
int indexOf(char ch) returns the index within the String of the first
occurrence of the specified character
int lastIndexOf(char ch) returns the index within the String of the last
occurrence of the specified character
String concat(String str) concatenates the specified string to the end of
this String
boolean equals (String str) compares the value of two strings
boolean equalsIgnoreCase(String str) compares the value of two
strings ignoring case
int compareTo(String str) comparing two strings lexicographically.
Each character of both the strings is converted into a Unicode value for
comparison.
If both the strings are equal then this method returns 0 else it returns
positive or negative value.
The result is positive if the first string is lexicographically greater than the
second string else the result would be negative.
int compareToIgnoreCase(String str)
Same as compareTo except case is ignored
String replace (char oldChar,char newChar)
replaces the specified old character with the specified new character
String substring (int beginIndex) returns the substring of the string
String substring (int beginIndex, int endIndex) returns the substring of
the string
boolean startsWith(String str) is used for checking prefix of a String. It
returns a boolean value true or false based on whether the given string
begins with the specified letter or word.
boolean endsWith(String str) This method returns a boolean value true
or false. If the specified suffix is found at the end of the string then it
returns true else it returns false.
String valueOf(all types) returns the string representation of the
specified data
//2. String Example for string methods
public class StringFunctionEg
{
public static void main(String args[])
{
String s1="COMPUTER",s2="applications",s3="asha",s4="esha",s5,s6;
// Length of string
System.out.println("Length of s1= "+s1.length());
// Character at index n
System.out.println("At index of s1 "+s1.charAt(3));
// Index of character
System.out.println("First index of 'i' in s2 "+s2.indexOf('i'));
System.out.println("Last index of 'i' in s2 "+s2.lastIndexOf('i'));
System.out.println("First index of 'ei' in s2 "+s2.indexOf('e'));
s3="asha" s4="esha" A=65 B=66.... a=97 b=98 c=99 d=100 e=101
//Compare strings returns 0 if both equal
System.out.println("Compare s3 and s4 " + s4.compareTo(s3));
System.out.println("Check equality " + s4.equals(s3));
//Concatenation
s5=s3.concat(" ");
s5=s5.concat(s4);
System.out.println("Concatenation 1 :" + s5);
s6=s1 + " " + s2;
System.out.println("Concatenation 2 :" + s6);
// Case conversion
s1=s1.toLowerCase();
System.out.println("Upper to lower case of s1 "+s1);
s2=s2.toUpperCase();
System.out.println("Lower to upper case of s2 "+s2);
// Substrings
s1=s1.substring(2,5);
System.out.println("Substring of s1 "+s1);
//replace character s4="bhor"
s4=s4.replace('o','a');
System.out.println("Replace characters "+s4);
}
}
//3.String Example for startsWith and endsWith method
public class StringFunctionEg1
{
public static void main(String args[]){
//given string
String s = "This is just a sample string";
//checking whether the given string starts with "This"
System.out.println(s.startsWith("This")); //true
//checking whether the given string starts with "Hi"
System.out.println(s.startsWith("Hi")); //false
System.out.println(s.endsWith("string")); // true
//checking whether the given string starts with "Hi"
System.out.println(s.endsWith("Hi")); //false
}
}
//4. String Example for ValueOf Method
public class StringValueOfExample
{
public static void main(String args[])
{
int i = 10; //int value
float f = 10.10f; //float value
long l = 111L; //long value
double d = 2222.22; //double value
char ch = 'A'; //char value
//converting int to String
String str1 = String.valueOf(i);
//converting float to String
String str2 = String.valueOf(f);
//converting long to String
String str3 = String.valueOf(l);
//converting double to String
String str4 = String.valueOf(d);
//converting char to String
String str5 = String.valueOf(ch);
System.out.println(str1);
System.out.println(str2);
System.out.println(str3);
System.out.println(str4);
System.out.println(str5);
}
}