Java StringManipulation -SS
Java StringManipulation -SS
A String is basically an object that represents sequence of char values. An collection of characters (upper or
lower case alphabets, numerals, special characters, spaces etc ) make up a Java string. For example:
A String is always enclosed in double quotes as shown above. The ‘S’ of String is always capital or upper
case. Strings are represented as objects of String class, which is defined in the package named – java.lang.
That is in a Strings program, this line should be written on the top:
import java.lang.*
Declaring Strings:
The new operator is used to create a String object, or simply saying to declare a String. For example if your
string name is ‘s’, then we will declare as:
String s = new String( );
Remember from Class IX, the usage of the ‘new’ operator. How an object is created:
The position of each character of a String is numbered. It is called INDEX NUMBER. REMEMBER: The
index numbering starts with zero, and goes on as 1, 2, 3 and so on. So if my String is “ICSE”, then the index
number of I is 0(zero), index number of C is 1, of S is 2, and of E is 3.
For Example we have a String “COMPUTER”. Then Index numbers of its characters is as:
We need to learn some pre-defined functions which will help us to work with strings easily.
Program:
import java.io.*;
import java.util.*;
import java.lang.*;
public class strprog
{
public static void main(String[] args) throws IOException
{
String str =new String();
Scanner sc = new Scanner(System.in);
System.out.println( str );
}
}
NOTE: In the below examples, to understand the functions, only the main() block is written.
In class 9, you have studied how a function top line is written. Please revise these terms of
functions : return value, return data type, argument, parameter etc, especially from pages 111 to
115 of your book.
------------------------------------------------------------------------------------------------------------------------------------------------------
import java.io.*;
import java.util.*;
public class …………..
{
public static void main(String args[]) throws IOException
{
xxxxx xxxxx xxxxx xxxxx
}
}
But in future I will not write these lines every time (written in blue). It is understood. I will write only the
‘main’ block, the area which is shown above as xxxxx xxxxx xxxxx xxxxx . But you have to write the whole
program every time.
In some of the below functions and programs we have two Strings called ‘s1’ and ‘s2’.
To check if the two Strings are similar, that is if they have the same characters in the same order.
boolean equals( )
Usage : if ( s1.equals(s2) )
Description : This function is used to compare the two strings for similarity. It will also
check if their case is same ie. upper / lower case.
------------------------------------------------------------------------------------------------------------------------------------------------------
boolean equalsIgnoreCase( )
Usage : if ( s1.equalsIgnoreCase (s2) )
Description : This function is used to compare the two strings for similarity, without taking
the case of the Strings into account.
The above two functions will always give a boolean result, that is either true or false. They
are generally used with the if statement.
------------------------------------------------------------------------------------------------------------------------------------------------------
Q.: Write a program to input a word and print whether it is a palindrome or not.
Program:
public static void main(String arg[]) throws IOException
{
String s=new String();
String n=new String();
char ch;
int len, x;
Scanner sc = new Scanner(System.in);
if(n.equals(s))
System.out.println("\nString is Pallindrome");
else
if(n.equalsIgnoreCase(s))
System.out.println("\nString is Pallindrome if case is Ignored");
else
System.out.println("\nString is NOT Pallindrome");
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------
String concat( )
Usage : String s = s1.concat(s2);
Description : This function is used to join two Strings back to back. The + sign
can also be used instead of concat( ).
---------------------------------------------------------------------------------------------------------------------------
Concatenate, concatenation, or concat is a word that is used to combine or
join a string or text end – to – end without any space.
As written above, + sign can be used instead of the concat( ) function.
Try to understand how the + sign works :
For example: If you have two numbers and you write 45 + 78, the + sign will
add the values of the two numbers to give answer as 123.
But, if you have two Strings as “TEXT” and “BOOK”, and you write
“TEXT” + “BOOK”, then the answer will be a new string as “TEXTBOOK”. You
see, the two Strings are joined end to end.
If you write any numerals in double quotes, then it will be treated as String.
eg if you write “123” + “789” the answer will be a new String as “123789”. Here
the values are not added.
eg if you write “456” + “GOOD” the answer will be a new String as “456GOOD”.
---------------------------------------------------------------------------------------------------------------------------
int indexOf( char )
Usage : int i = s.indexOf( ‘ T ’ );
Description : It returns the position of the first occurrence of the character in
the given String.
int indexOf( char, int )
Usage : int i = s.indexOf( ‘T’, 3 );
Description : It returns the position of the first occurrence of the character in
the given String starting the search from the specified int position.
---------------------------------------------------------------------------------------------------------------------------
The indexOf( ) function also works with String as argument just like with char as
argument.
int indexOf( String )
int indexOf(String, int )
---------------------------------------------------------------------------------------------------------------------------
int lastIndexOf( char )
Usage : int i = s.indexOf( ‘T’ );
Description : It returns the position of the last occurrence of the character in
the given String. ( Index position counting always starts from the left side ).
---------------------------------------------------------------------------------------------------------------------------
int lastIndexOf( char, int )
Usage : int i = s.indexOf( ‘T’ );
Description : It returns the position of the last occurrence of the character in
the given String before the specified int position.
---------------------------------------------------------------------------------------------------------------------------
The lastIndexOf( ) function also works with String as argument just like with
char as argument.
int lastIndexOf( String )
int lastIndexOf(String, int ) } Similar to above, but searches for Strings
---------------------------------------------------------------------------------------------------------------------------
String trim( )
Usage : String s = s.trim( );
Description : trim( ) removes all leading and trailing spaces from a string.
Space(s) between words remain as they are.
---------------------------------------------------------------------------------------------------------------------------
String replace( char, char )
Usage : String s = s.replace ( ‘P’, ‘T’ );
Description : This function replaces all occurrences of the first char with the
second char.
---------------------------------------------------------------------------------------------------------------------------
The startsWith( ) function works in two variants.
boolean startsWith( char )
boolean startsWith( String )
Usage : if ( s.startsWith( ‘P’ ) )
OR if ( s.startsWith( “COM” ) )
Description : Checks if the first character of the String is as specified by the
char.
Checks if the first few characters of the String is as specified by the String.
---------------------------------------------------------------------------------------------------------------------------
The endsWith( ) function works in two variants.
boolean endsWith( char )
boolean endsWith( String )
Usage : if ( s.endsWith( ‘P’ ) )
OR if ( s.endsWith( “TER” ) )
Description : Checks if the last character of the String is as specified by the
char.
Checks if the last few characters of the String is as specified by the String.
---------------------------------------------------------------------------------------------------------------------------
String toLowerCase( )
Usage : String s1 = s.toLowerCase( );
Description : Converts all characters of the string to lower case letters, and
returns a new String.
---------------------------------------------------------------------------------------------------------------------------
String toUpperCase()
Usage : String s1 = s.toUpperCase( );
Description : Converts all characters of the string to upper case letters, and
returns a new String.
---------------------------------------------------------------------------------------------------------------------------
STRING CLASS : This class represent a group of characters represented in double quotes. The values of String class are
immutable ie. they cannot be modified once they are created.
STRING BUFFER CLASS : StringBuffer class is a class of String where an object of StringBuffer class is mutable ie. they
can be modified as and when required.