Presented By
Nishant Gupta
• A linear sequence of symbols (characters
or words or phrases)
• A string is a data type used in
programming, such as an integer and
floating point unit.
• For Ex- “HCL is $5 Billion Enterprise”
STRING
Declaring Strings in Java
• String str=“HCL”(declaring by reference)
• String str=new String(“Presentation”);
(declaring by constructer)
• String[] name={“Lokesh",“
Sunny",“Rosie",“Aarushe","deepak"};
(declaring explicitly)
String Manipulation
• It includes all the operations performed on
strings.
• String manipulation forms the basis of
many algorithms and utilities. Input
validation, text analysis and file
conversions are several direct applications
of string manipulation.
Constructors
•String class provides various types of constructors to create String objects.
Some of them are,
String() Creates a new String object whose content is empty i.e. “”.
•String(String s)
Creates a new String object whose content is same as the String object passed
as an argument.
•String also provides constructors that take byte and char array as argument
and returns String object.
•String equality
String class overrides the equals() method of the Object class. It compares the
content of the two string object and returns the boolean value accordingly.
For example,
String str1=”Hello”;
String str2=”Hello”;
If(str1 == str2)
System.out.println(“Equal 1”);
Else
System.out.println(“Not Equal 1”);
•String Compare
Apart from these methods String class also provides compareTo methods.
int compareTo(String str2)
This method compares two Strings and returns an int value. It returns value 0, if
this string is equal to the string argument a value less than 0, if this string is
less than the string argument
a value greater than 0, if this string is greater than the string argument
Reading characters from String:
char charAt(index i)
Returns char at specified index. An index ranges from 0 to length() -1.
Searching characters in String
String class provides indexOf method which searches for the specified character inside
the string object. This method has been overloaded. If the search is successful, then
it returns the index of the char otherwise -1 is returned.
1. int indexOf(int c)
Returns the index of first occurrence of the argument char.
2. int indexOf(int c, int fromIndex)
Finds the index of the first occurrence of the argument character in a string, starting at
the index specified in the second argument.
3. int indexOf(String str)
Finds the start index of the first occurrence of the substring argument in a String.
4. int lastIndexOf(int ch)
The String class also provides methods to search for a character or string in backward
direction.
Replacing characters in String
The replace method of String can be used to replace all occurrences of the specified
character with given character.
String replace(char oldChar, int newchar)
Eg.
L
ROCK
Getting substrings
String class provides substring method to extract specified portion of the given String.
This method has been overloaded.
String substring(int startIndex)
The code given below specifies the starting and the ending index of the string
String substring(int startIndex, int endIndex)
Eg. RockMan String SubString
Here Man substring is removed from RockMan
Finding the length of string
The String class defines the length() method that determines the length of a string. The
length of a string is the number of characters contained in the string.
Str.length();
String str=“Hello”
System.out.println(str.length());
Output
5
String Concatenation using the + operator
The + operator is used to concatenate two strings, producing a new String object as the
result. For example,
String sale = "500";
String s = "Our daily sale is" + sale + "dollars";
System.out.println(s);
This code will display the string "Our daily sale is 500 dollars".
Manipulating Character Case
String class provides following methods to manipulate character case in String.
The toUpperCase() method creates a new copy of a string after converting all the
lowercase letters in the invoking string to uppercase. The signature of the method is
given below:
String toUpperCase();
Eg JAVA
java
The toLowerCase() method creates a new copy of a string after converting all the
uppercase letters in the invoking string to lowercase. The signature of the method is
given below:
String toLowerCase() ;
Eg java
JAVA
Trimming
This method removes white space from the front and the end of a String.
String trim();
Eg “ Hello ”
“Hello” (After trimming)
Concatenation
This method returns a string with the value of string passed in to the method appended
to the end of String which used to invoke the method.
String.concat(str);
“Sun” +” java” = “Sunjava”