String Handling
String Handling
Java String Class is immutable, i.e. Strings in java, once created and initialized, cannot
be changed on the same reference. The String class exist in the java.lang package.
String objects can be used with the += and +operators for concatenation(ie. to combine
the strings).
A String object can be created in many ways .
Given below are some of the different ways of creating String objects
The String object can also be created using the new operator as
String str5 = new String(“My name is bob”);
Example :
Output
charStr : STUV
A simple way to convert any primitive value to its string representation is by concatenating it with
the empty string (“”), using the string concatenation operator (+).
Example
int i=10;
char c='a';
String str="";
STRINGS [CELINE BIJU – SISHYA] Page 1
String str1=i+str;
System.out.println(str1);
System.out.println(i+i);
System.out.println(i+str+i);
System.out.println(i+i+str);
System.out.println(i+i+""+i+i);
Output
10
20
1010
20
201010
Example :
String str = "Car insurance";
System.out.println(str.length());
Output is 13
Note : length is used to check the length of an array and length() is used to check the length of a
String object.
Example:
String str = "insurance";
if ( str.equalsIgnoreCase("INSURANCE") )
{
System.out.println("insurance strings are equal");
}
else
{
System.out.println("insurance Strings are not equal");
}
Output : insurance strings are equal
This function will return an integer. It will return a positive value if the first string is greater than
the second string. It will return a negative value if the first string is less than the second string. It
will return 0 if both the strings are equal.
Example :
//Ascii value –‘ A’ to ‘Z’ 65 to 90
// Ascii value –‘ a’ to ‘z’ 97 to 122
Output :
0
10
-10
This method works like the compareTo() function but compares two strings lexicographically,
ignoring case differences.
This function after ignoring the case of the letter will return an integer . It will return a positive
value if the first string is greater than the second string. It will return a negative value if the first
string is less than the second string. It will return 0 if both the strings are equal.
Example:
String str1 = "Strings are immutable";
String str2 = "strings are immutable";
String str3 = "Integers are not immutable";
//Ascii value of ‘S’ is 84 ‘I’ is 74 ‘i’ is 106 and ‘s’ is 116
Output :
0
10
Note : The return value ignores the case difference. Hence ‘s’ compared with ‘I’ returns 10 and not
42(116 -74)
This function will compare the character sequence of the suffix of the String object with the
character sequence of the suffix passed as a parameter.
This method will return a boolean value i.e either true or false depending on whether the suffix
passed as a parameter is the same as the suffix of the string .
Note : the result will be true if the parameter is an empty string or is equal to the String object.
Example:
String Str = new String("This is really not immutable!!");
boolean retVal;
retVal = Str.endsWith("immutable!!");
System.out.println("Returned Value = " + retVal );
retVal = Str.endsWith("immu");
System.out.println("Returned Value = " + retVal );
retVal = Str.endsWith("");
System.out.println("Returned Value = " + retVal );
Output
Returned Value = true
Returned Value = false
Returned Value = true
Returned Value = true
7. Finding whether the prefix of the string is the same as the prefix passed as parameter -
startsWith() .
This method will return a boolean value i.e. either true or false depending on whether the prefix
passed as a parameter is the same as the prefix of the string .
Note : the result will be true if the parameter is equal to the String object.
Example:
String Str = new String("This is really not immutable!!");
boolean retVal;
retVal = Str.startsWith("This");
System.out.println("Returned Value = " + retVal );
retVal = Str.startsWith("immutable!!");
System.out.println("Returned Value = " + retVal );
Output
Returned Value = true
Returned Value = false
Returned Value = true
8. Finding the index Position of a String/character in another String object- from the first index/
the specified index - indexOf()
This function either takes single or two parameters and will return an integer.
The indexOf() method is used to find the index of a string or a character in another string.
This function will return the index position if the string or the character looked for in another
String object exists . If it does not exist then this method will return -1.
Example
String Str = new String("Hello Hello Hello");
String Str1 = new String("Hello");
String Str2 = new String("Sello");
//will check for occurrence of 'o' from index 0
System.out.println(Str.indexOf('o'));
Output
4
0
10
0
12
-1
9. Finding the last index Position of a string/character in another String object– lastIndexOf( )
This function either takes single or two parameters and will return an integer.
The lastIndexOf() method is used to find the last Index position of a character in another String
object.
This function will return the last index position if the character looked for in another String object
exists. If it does not exist this method will return -1.
Example:
String Str = new String("Hello Hello Hello");
String Str1 = new String("Hello");
//will check for the last occurrence of ‘o’ only up to the 12 th index
System.out.println(Str.lastIndexOf( 'o',12 ));
//will check for the last occurrence of Str1 only up to the 8th index
System.out.println(Str.lastIndexOf( Str1,8 ));
Output
F
Example:
String Str = new String("Welcome_to_Learning_Java");
//will extract characters from the 10th position till the end of the string
System.out.println(Str.substring(10) );
//will extract characters from the 10th positon till the index (15-1)
System.out.println(Str.substring(10, 15) );
Output
-Learning Java
-Lear
Output
CHEAP CAR INSURANCE
Cheap Car Insurance
CHEAP CAR INSURANCE
15. concat()
Output
Computer Applications
Football
This function is used to remove the blanks from either side of the string. Other blanks which are
present in between words will remain unchanged.
Syntax
String str1=String str2.trim();
Example
String str=” Computer Applications “;
String str1=str.trim();
System.out.println(str1);
Output
Computer Applications(no leading and trailing space)
Syntax
This method is a static function and it returns the string representation of the primitive data type
passed as a parameter.
Example :
double d = 1029.939;
boolean b = true;
long l = 1232874;
char[] arr = {'a', 'b', 'c', 'd', 'e', 'f','g' };
String str1=String.valueOf(d);
String str2=String.valueOf(b);
System.out.println(str1);
System.out.println(str2);
System.out.println(String.valueOf(l));
System.out.println(String.valueOf(arr));
1029.939
true
1232874
Abcdefg